C# 中的更新命令不更新数据库
我找到了解决这个特定问题的解决方法:将数据库中的数据加载到一组控件中,并将更新信息输入到一组单独的控件中。感谢您的评论;该应用程序正在运行。
尝试更新数据库行。我根据在几个论坛/网站上找到的内容编写了以下代码。他的代码没有更新数据库。
在尝试修复此错误后,我发现了以下内容:如果 @editFirstName 的值是文字,数据库将正确更新,就像
UpdateCmd.Parameters["@editFirstName"] = "George";
我也尝试过的
UpdateCmd.Parameters["@editFirstName"] = "'" + editFirstNameContent.Text + "'";
那样,但不起作用。
我还尝试将 TextBox 数据放入变量中并使用变量:
string firstNameValue = editFirstNameContent.Text;
UpdateCmd.Parameters["@editFirstName"] = firstNameValue;
但这不起作用。
因此,UPDATE命令显然是在更新数据库。但是,显然Parameters.Value行没有读取TextBox中的数据,因此它没有使用更改后的值进行更新。
protected void editCustomerButton_Click(object Sender, EventArgs e)
{
string connString = WebConfigurationManager.ConnectionStrings["proofreadThePlanetConnectionString"].ConnectionString;
using (SqlConnection connection = new SqlConnection(connString))
{
connection.Open();
string updateString = "UPDATE tblCustomerInfo SET customerID=@editCustomerID, customerFirstName=@editFirstName, customerLastName=@editLastName, customerEmail=@editEmail WHERE customerID=" + Request.QueryString["EID"];
SqlCommand UpdateCmd = new SqlCommand(updateString, connection);
UpdateCmd.Parameters.Add("@editCustomerID", SqlDbType.Int);
UpdateCmd.Parameters["@editCustomerID"].Value = editCustomerIDContent.Text;
UpdateCmd.Parameters.Add("@editFirstName", SqlDbType.VarChar, 25);
UpdateCmd.Parameters["@editFirstName"].Value = editFirstNameContent.Text;
UpdateCmd.Parameters.Add("@editLastName", SqlDbType.VarChar, 50);
UpdateCmd.Parameters["@editLastName"].Value = editLastNameContent.Text;
UpdateCmd.Parameters.Add("@editEmail", SqlDbType.VarChar, 75);
UpdateCmd.Parameters["@editEmail"].Value = editEmailContent.Text;
UpdateCmd.ExecuteNonQuery();
}
}
I have found a work around that solved this particular problem: the data from the database is loaded into one set of controls, and the update information is enetred into a separate set of controls. Thanks for the comments; the application is working.
Trying to update a database row. I wrote the following code based on what I found on several fora/websites. he code is not updating the database.
After trying anything to fix this error I have discovered the following: the database will update properly if the value of the @editFirstName is a literal, as in
UpdateCmd.Parameters["@editFirstName"] = "George";
I also tried
UpdateCmd.Parameters["@editFirstName"] = "'" + editFirstNameContent.Text + "'";
which did not work.
I also tried to put the TextBox data into a variable and use the variable:
string firstNameValue = editFirstNameContent.Text;
UpdateCmd.Parameters["@editFirstName"] = firstNameValue;
which did not work.
Therefore, the UPDATE command is apparently updating the databade. However, apparently the Parameters.Value line is not reading the data in the TextBox, and so it is not updating with the changed value.
protected void editCustomerButton_Click(object Sender, EventArgs e)
{
string connString = WebConfigurationManager.ConnectionStrings["proofreadThePlanetConnectionString"].ConnectionString;
using (SqlConnection connection = new SqlConnection(connString))
{
connection.Open();
string updateString = "UPDATE tblCustomerInfo SET customerID=@editCustomerID, customerFirstName=@editFirstName, customerLastName=@editLastName, customerEmail=@editEmail WHERE customerID=" + Request.QueryString["EID"];
SqlCommand UpdateCmd = new SqlCommand(updateString, connection);
UpdateCmd.Parameters.Add("@editCustomerID", SqlDbType.Int);
UpdateCmd.Parameters["@editCustomerID"].Value = editCustomerIDContent.Text;
UpdateCmd.Parameters.Add("@editFirstName", SqlDbType.VarChar, 25);
UpdateCmd.Parameters["@editFirstName"].Value = editFirstNameContent.Text;
UpdateCmd.Parameters.Add("@editLastName", SqlDbType.VarChar, 50);
UpdateCmd.Parameters["@editLastName"].Value = editLastNameContent.Text;
UpdateCmd.Parameters.Add("@editEmail", SqlDbType.VarChar, 75);
UpdateCmd.Parameters["@editEmail"].Value = editEmailContent.Text;
UpdateCmd.ExecuteNonQuery();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定 MS SQL Server 如何处理自动提交,但如果自动提交模式未打开,您需要手动提交事务 (
UpdateCmd.Transaction.Commit
) 。I'm not sure how MS SQL Server handles auto-commit, but in case the auto-commit mode is not turned on, you'll need to commit your transaction manually (
UpdateCmd.Transaction.Commit
).SQL Profiler 随 SQL Management Studio 一起提供。在执行代码时运行此命令以查看实际执行的命令。
这是有关如何使用分析器的教程。
http://sqlserverpedia.com/wiki/Using_SQL_Server_Profiler
SQL Profiler comes with SQL Management Studio. Run this while executing your code to see what command is actually being executed.
Here's a tutorial on how to use the profiler.
http://sqlserverpedia.com/wiki/Using_SQL_Server_Profiler
我会检查
Request.QueryString["EID"]
确实包含正确的 customerId 值,因为这可能会导致语句运行但没有数据更新,如果您正在尝试更新不存在的行。
为了确保参数值设置正确,您可以尝试以下格式;
command.Parameters.Add("@editCustomerID", SqlDbType.Int).Value = editCustomerIDContent.Text;
正如 Ash 建议的那样,准确了解发生情况的最佳方法是运行 Profiler执行命令时的数据库(注意:这可能会对性能产生影响,应该在测试/开发系统上或在非工作时间完成)
I would check that
Request.QueryString["EID"]
does indeed contain a correct customerId value, as this could cause the statement to run but no data to be updated, if you are trying to update a row which does not exist.
To ensure the parameters values are being set correctly you could try the following format;
command.Parameters.Add("@editCustomerID", SqlDbType.Int).Value = editCustomerIDContent.Text;
As suggested by Ash, the best way to find out exactly what is happening would be to run Profiler against the database while the command is being executed (Note: This can have a performance impact and should be done on a Test/Dev system or out-of-hours)