none
How to update data by using OleDbCommand? RRS feed

  • Question

  • I have a table named Customer and three columns: CustomerID (Autonumber), Name (text), Address (text). But when I used OleDbCommand to update data into Database, It updated wrong value. For example, users edited Name into Nick, and press update button, but the Name value in database is 0 (???). This is my code:

    string strSQL = "Update Customer Set Name = ? and Address = ? where CustomerID = ?";

    cmd = new OleDbCommand(strSQL, cn);
    cmd.Parameters.Add("@Name", OleDbType.WChar);
    cmd.Parameters.Add("@Address", OleDbType.WChar);
    cmd.Parameters.Add("@CustomerID", OleDbType.Integer);

    cmd.Parameters["@Name"].Value = txtName.Text;
    cmd.Parameters["@Address"].Value = txtAddress.Text;
    cmd.Parameters["@CustomerID"].Value = int.Parse(txtCustomerID.Text);
    cmd.ExecuteNonQuery();

    Anybody help me?, thank first.
    Tuesday, December 25, 2007 11:42 AM

Answers

  •  babychicken wrote:
    I already solve this problem that replace "and" in strSQL with ",". But anybody know what difference?.

     

    The comma is what supposed to be there, not and. And is what you made up. Comma is madated by the SQL language.

     

    Also I suggest you introduce a Parameter.Clear () statement before you began adding your parameters. If you use this piece of code in a loop you will get in trouble because the parameters will keep adding up. Also the statement Add ( ) and Value = can be combined in one.

     

    Parameters.Add ("parname", OleDbType.Char, 10).Value = "myName";

    Tuesday, December 25, 2007 3:42 PM

All replies

  • I already solve this problem that replace "and" in strSQL with ",". But anybody know what difference?.
    Tuesday, December 25, 2007 3:29 PM
  •  babychicken wrote:
    I already solve this problem that replace "and" in strSQL with ",". But anybody know what difference?.

     

    The comma is what supposed to be there, not and. And is what you made up. Comma is madated by the SQL language.

     

    Also I suggest you introduce a Parameter.Clear () statement before you began adding your parameters. If you use this piece of code in a loop you will get in trouble because the parameters will keep adding up. Also the statement Add ( ) and Value = can be combined in one.

     

    Parameters.Add ("parname", OleDbType.Char, 10).Value = "myName";

    Tuesday, December 25, 2007 3:42 PM