除了更改没有保存到SQL数据库之外,完美地工作

时间:2014-10-12 06:34:04

标签: c# executenonquery

更改未保存到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);
        }

3 个答案:

答案 0 :(得分:1)

您可以在此处查看update命令的MSDN reference

使用参数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);
    }
}

答案 1 :(得分:0)

好像你的语法不正确。这是Update的语法:

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

所以,更新,设置什么,设置WHERE(你似乎缺少)。

如需更多信息,请查看here

答案 2 :(得分:-3)

检查您的更新查询

改变它

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