0

UPDATE我在尝试通过子句更新表数据的语句时遇到问题WHERE,这给了我数据不匹配的错误。

sqL = "UPDATE Customer SET name= '" & txtName.Text & "', adress= '" & txtAdress.Text & "', contact = '" & txtContact.Text & "' WHERE Customer_ID = '" & txtCustomerID.Text & "'"

我也试过

sqL = "UPDATE Customer SET name= '" & txtName.Text & "', adress= '" & txtAdress.Text & "', contact = '" & txtContact.Text & "' WHERE Customer_ID = '" & Convert.ToInt32(txtCustomerID.Text) & "'"

没有运气。

4

3 回答 3

2

请使用更清洁、更安全的参数化查询:

如果你在 C# 上:

string sql = "UPDATE Customer SET name= @name, adress=@address, contact = @contact" +
             " WHERE Customer_ID = @id";
using(SqlConnection conn = new SqlConnection("yourConnectionString"))
{
   SqlCommand cmd = new SqlCommand(sql, conn);
   cmd.Parameters.AddWithValue("@name",txtName.Text);
   cmd.Parameters.AddWithValue("@address",txtAdress.Text);
   cmd.Parameters.AddWithValue("@contact",txtContact.Text);

   /*
     NOTE: Make sure Textbox value is convertible to Customer_ID data type 
           before executing the  query. It should be done before the using statement.
           Use string.Trim() method to remove any space characters at start/end
   */
   cmd.Parameters.AddWithValue("@id",txtCustomerID.Text.Trim());

   conn.Open();
   cmd.ExecuteNonQuery();

}
于 2013-09-20T13:13:42.247 回答
0

您的查询将无法编译:-

C# 中的字符串连接运算符是加号而不是 & 号。

但是,kaf建议始终使用参数化查询。 在此处输入图像描述

尝试使用加号而不是 & 号。

 "UPDATE Customer SET name= '" + txtName.Text + "', adress= '" + txtAdress.Text + "', contact = '" + txtContact.Text + "' WHERE Customer_ID = '" + txtCustomerID.Text + "'"

如果客户 ID 是 int ,则将其转换为 int 。

于 2013-09-20T15:42:37.327 回答
0

看起来 Customer_ID 的数据类型是 int。在这种情况下,请从您的转换语句中删除单引号。

sqL = "UPDATE Customer SET name= '" & txtName.Text & "', adress= '" & txtAdress.Text & "', contact = '" & txtContact.Text & "' WHERE Customer_ID = " & Convert.ToInt32(txtCustomerID.Text)

但是一定要仔细检查表中的数据类型。

于 2013-09-20T13:06:15.217 回答