【发布时间】:2017-09-06 07:00:56
【问题描述】:
我想更新我的 sql 表。我在这里搜索并找到了有关如何解决该问题的解决方案。但遗憾的是它只是不会更新数据库。我不知道问题是什么。 我检查了几次 sql 命令写错误,但找不到或修复它们,但遗憾的是仍然没有。我想这是 try 块中的东西,但找不到。
这是我的代码:
string connetionString = null;
SqlConnection connection;
SqlCommand command;
string sql = null;
SqlDataReader dataReader;
connetionString = "Data Source=xxx\\xxx;Initial Catalog=xxx;User ID=xxx;Password=xxx";
sql = "UPDATE Employees SET LastName = '" + Lnamestring + "', FirstName = '" + Fnamestring + "', Title = '" + Titelstring + "', TitleOfCourtesy = '" + ToCstring + "', BirthDate = '" + Birthdatestring + "', HireDate = '" + Hiredatestring + "', Address = '" + Adressstring + "', City = '" + Citystring + "', Region = '" + Regionstring + "', PostalCode = '" + Postalstring + "', Country = '" + Countrystring + "', HomePhone = '" + Phonestring + "', Extension = '" + Extensionsstring + "', Notes = '" + Notesstring + "', ReportsTo = '" + ReportTostring + "' WHERE EmployeeID = '" + IDstring + "'; ";
connection = new SqlConnection(connetionString);
try
{
connection.Open();
command = new SqlCommand(sql, connection);
SqlDataAdapter sqlDataAdap = new SqlDataAdapter(command);
command.Dispose();
connection.Close();
MessageBox.Show("workd ! ");
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection ! ");
}
我希望有人能帮我找出我的错误。
编辑:当我尝试它时,它似乎可以工作,因为窗口弹出“workd”但数据库没有改变。
【问题讨论】:
-
到底发生了什么?是否发生错误? P.S 利用Using Statement P.P.S 利用参数化
-
刚刚在底部编辑过
-
请使用 SqlCommandParameters 代替字符串 concat
-
你根本不执行命令。
-
在
command.Dispose();之前尝试command.ExecuteNonQuery();
标签: c# sql sql-server sql-update