【问题标题】:Data is not updating into database [closed]数据未更新到数据库中[关闭]
【发布时间】:2015-05-08 15:02:52
【问题描述】:

我想将文本框的值更新到我的 SQL Server 数据库中。

代码没有显示任何语法错误,并且很容易重定向到我正在重定向的页面,但数据库仍然没有更新其中的新数据。

conn.Open();

string str_id = Session["userid"].ToString();

int id = Convert.ToInt32(str_id);
id = Int32.Parse(str_id);

string updatequery = "Update empdata set fname='" + updatename.Text + "',education='" + updateeducation.Text + "',position='" + updateposition.Text + "',email='" + updateemail.Text + "',address='" + updateaddress.Text + "',contact='" + updatecontact.Text + "',account='" + updateaccount.Text + "',postal='" + updatepostal.Text + "',password = '" + updatepwd.Text + "' Where id = '" +id.ToString()+ "'";

SqlCommand updateinfo = new SqlCommand(updatequery, conn);
updateinfo.ExecuteNonQuery();
updateinfo.Dispose();

updationmessage.Text="<p style='color:green;'>Information updated successfully</p>";

【问题讨论】:

  • xkcd.com/327这里是sql注入漏洞!
  • 如果在设置 updatequery 后的行设置断点,则可以得到正在尝试的实际查询。此外,您的代码容易受到 sql 注入的影响。
  • 文本框的初始值是否从数据库中加载?可能是回发问题
  • 你能显示实例化 conn 的代码吗?
  • 页面中是否启用了视图状态?

标签: c# sql asp.net sql-server database


【解决方案1】:

首先,切换到ParameterBinding,你的代码容易出现sql inction(而且速度较慢)

其次,查看ExecuteNonQuery的返回值。如果为0,则说明数据库没有变化,表示没有找到匹配的id

第三,检查您是否在需要提交事务的事务中 - 否则您将看不到数据库中的任何内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-20
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    相关资源
    最近更新 更多