gpt4 book ai didi

c# - SQL 查询不工作

转载 作者:行者123 更新时间:2023-11-30 21:14:00 24 4
gpt4 key购买 nike

此代码不会更改列(我尝试将列 orderTillNow 的值增加一个...),表: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# 不更改值?

最佳答案

你忘了:

connection.Open();

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

using (SqlConnection connection = new SqlConnection("..."))
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = "...";

connection.Open();

command.ExecuteNonQuery();
} // will close the connection automatically

注意:

使用 block 对SqlConnection 比对SqlCommand 重要得多。

关于c# - SQL 查询不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6490628/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com