【问题标题】:Works perfectly except changes are not saved to the SQL database完美运行,但更改未保存到 SQL 数据库
【发布时间】:2014-10-12 06:34:03
【问题描述】:

更改不会保存到 SQL 数据库中

为什么我要在 sql 语句中使用 '@' 而不是我拥有语句的方式?

代码:

    private void button_Save_Customer_Click(object sender, EventArgs e)
    {
        sqlString = Properties.Settings.Default.ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(sqlString);

        try
        {
            string customer_Ship_ID = customer_Ship_IDTextBox.ToString();
            string customer_Ship_Address = customer_Ship_AddressTextBox.Text;
            SQL = "UPDATE Customer_Ship SET Customer_Ship_Address = customer_Ship_Address WHERE Customer_Ship_ID = customer_Ship_ID";
            SqlCommand sqlCommand = new SqlCommand(SQL, sqlConnection);
            sqlCommand.Parameters.AddWithValue("Customer_Ship_ID", customer_Ship_ID);
            sqlCommand.Parameters.AddWithValue("Customer_Ship_Address", customer_Ship_Address);
            sqlCommand.CommandText = SQL;

            sqlConnection.Open();
            sqlCommand.ExecuteNonQuery();
            sqlConnection.Close();

            MessageBox.Show("Record Updated");
        }
        catch (Exception err)
        {
            MessageBox.Show(err.Message);
        }

【问题讨论】:

标签: c# executenonquery


【解决方案1】:

您可以在这里查看MSDN reference 中的update 命令。

使用参数,Why?

还要检查您是否需要打开和关闭连接对象,而不是命令。

如果您想使用 Customer_ID = "something" 更新行,您可以这样做:

代码(在您更改后更新)

private void button_Save_Customer_Click(object sender, EventArgs e)
{
    string sqlString = Properties.Settings.Default.ConnectionString;
    SqlConnection sqlConnection = new SqlConnection(sqlString);
    try
    {
        int customer_Ship_ID;
        if(int.TryParse(customer_Ship_IDTextBox.Text, out customer_Ship_ID))
        {
            string customer_Ship_Address = customer_Ship_AddressTextBox.Text;
            // Customer_Ship: Database's table
            // Customer_Ship_Address, Customer_Ship_ID: fields of your table in database
            // @Customer_Ship_Address, @Customer_Ship_ID: parameters of the sqlcommand
            // customer_Ship_ID, customer_Ship_Address: values of the parameters
            string SQL = "UPDATE Customer_Ship SET Customer_Ship_Address = @Customer_Ship_Address WHERE Customer_Ship_ID = @Customer_Ship_ID";
            SqlCommand sqlCommand = new SqlCommand(SQL, sqlConnection);
            sqlCommand.Parameters.AddWithValue("Customer_Ship_ID", customer_Ship_ID);
            sqlCommand.Parameters.AddWithValue("Customer_Ship_Address", customer_Ship_Address);
            sqlCommand.CommandText = SQL;

            sqlConnection.Open();
            sqlCommand.ExecuteNonQuery();
            sqlConnection.Close();

            MessageBox.Show("Record Updated");
        }
        else
        {
            // The id of the textbox is not an integer...
        }
    }
    catch (Exception err)
    {
        MessageBox.Show(err.Message);
    }
}

【讨论】:

  • 感谢您的帮助。我已经更改了上面的原始代码,显示了我现在拥有的内容。我没有收到任何错误消息,但没有对数据库进行更改。另外,为什么使用'@'作为sql语句而不是我原来的方式?
  • 对于@:stackoverflow.com/questions/556133/…,您的表是否包含与更新语句中的 where 值相同的 customer_id?
  • 用我的回答再次检查你的代码。您缺少参数的“@”。 @ 告诉应该用值替换什么: @"UPDATE Customer_Ship SET Customer_Ship_Address = @Customer_Ship_AddressTextBox WHERE Customer_ID = @Customer_IDTextBox";同时取消注释 customer_id 的参数并将 open 更改为使用 sqlConnection.Open() 而不是命令
  • 我更新了我的答案。查看并验证您是否拥有相同的代码。
  • customer_id 问题是肯定的,数据表有Customer_ID,表单上的文本框名为customer_IDTextBox。我可能应该使用 Customer_Ship_ID,因为它是关键字段。 Customer_Ship 数据表中的 Customer_ID 是连接到 Customer 数据表中的客户名称。由于多个位置,最终我将拥有一对多。现在检查其他代码。非常感谢您的帮助
【解决方案2】:

您的语法似乎不正确。这是Update 的语法:

UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;

所以,更新,设置什么,设置在哪里(你似乎错过了)。

更多信息,请查看here

【讨论】:

    【解决方案3】:

    检查您的更新查询

    改成这样

    string SQL = string.format("UPDATE Customer_Ship SET Customer_Ship_Address='{0}'",putUrVaue); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-24
      • 2019-01-31
      • 2021-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多