SQL查询不起作用

本文关键字:不起作用 查询 SQL | 更新日期: 2023-09-27 18:00:23

此代码不会更改列(我尝试将列orderTillNow的值增加一…),table:items。

SqlConnection connection = new SqlConnection("Data Source=***:*******.com;Initial Catalog=****;User ID=*****;Password=******;Integrated Security=False;");
using (SqlCommand command = new SqlCommand("UPDATE items SET ordersTillNow = ordersTillNow + 1 ")) 
{
    connection.Open();
    command.Connection = connection;
    command.ExecuteNonQuery();
    connection.Close();
}

我曾尝试将该语句放入SQL Server Management Studio中,这是有效的。为什么我的C#不更改值?

SQL查询不起作用

您忘记了:

connection.Open();

最后,你的代码应该是这样的:

using (SqlConnection connection = new SqlConnection("..."))
using (SqlCommand command = connection.CreateCommand())
{
    command.CommandText = "...";
    connection.Open();
    command.ExecuteNonQuery();
} // will close the connection automatically

注意:

对CCD_ 3的CCD_ 2-阻断比对SqlCommand的阻断重要得多。

在执行查询之前是否打开了连接?你得到了什么样的例外(messaage)?

干杯,Rok

您尚未打开和关闭连接。

connection.Open();
connection.Close();