【问题标题】:Trying to update SQL database with update command尝试使用 update 命令更新 SQL 数据库
【发布时间】:2020-04-27 14:06:27
【问题描述】:
// Find controls for on edit. 
DropDownList ddlLocation = (DropDownList)(sender as ListView).EditItem.FindControl("ddlLocation");
TextBox txtQuestion = (TextBox)(sender as ListView).EditItem.FindControl("txtQuestion");
Label lblLocationID = (Label)(sender as ListView).EditItem.FindControl("lblLocationID");

// Set strSQL to empty string.
String strSQL = "";

// Develop SQL call.
strSQL = "";
strSQL += "UPDATE Question ";
strSQL += "SET LocationID = '" + ddlLocation.SelectedValue + "', Question = '" + txtQuestion.Text + "' ";
strSQL += " WHERE LocationID = " + lblLocationID.Text ;

// Define the network connection to the SQL Server database.
SqlConnection objSqlConnection = new SqlConnection(WebConfigurationManager.ConnectionStrings["2020LJCDT"].ConnectionString);

// Set up the SQL command object
SqlCommand objSqlCommand = new SqlCommand();
objSqlCommand.Connection = objSqlConnection;
objSqlCommand.CommandType = CommandType.Text;
objSqlCommand.CommandText = strSQL;

// Define the input parameters
objSqlCommand.Parameters.AddWithValue("@LocationID", ddlLocation.SelectedValue);
objSqlCommand.Parameters.AddWithValue("@Question", txtQuestion.Text);

// Open the connection
objSqlConnection.Open();

// Execute sql. Get number of rows affected. 
numberOfRecords = objSqlCommand.ExecuteNonQuery();

// Close the data reader and the connection.
objSqlConnection.Close();

我的更新命令无法正常工作。由于某种原因,更新LocationID 会出现问题。我可以毫无问题地更新Question,只是不能更新LocationID。我在下拉列表中选择LocationID,它正确传递了值,但不会更新数据库。有没有人看到有什么不对?

这是我的数据库表的截图:

这些是被传递的值:

UPDATE Question 
SET LocationID = '2086', Question = 'Test123'  
WHERE LocationID = 2087

【问题讨论】:

  • 删除'2086' 周围的单引号 - 并使用参数!
  • 您是否收到任何错误消息?一切看起来都不错,但是阅读后有几个 FYI:1)您不需要在 2086 左右使用单引号,因为 LocationID 是一个 INT(但我认为 SQL 会翻译它),2)您正在创建参数,但您不是使用它们。 LocationID 上是否有外键或约束?
  • @jefftrotman LocationID 是外键。
  • SQL Injection alert - 您应该将您的 SQL 语句连接在一起 - 使用 参数化查询 来避免 SQL 注入 - 查看Little Bobby Tables
  • @marc_s 我将全部更改为参数化查询。你还是有问题。 SQL 成功,但未更新 LocationID。 Question 属性是唯一正在更新的属性。

标签: c# asp.net sql-server


【解决方案1】:

删除 '2086' 周围的单引号并使用字符串中的参数:

const string strSQL = "UPDATE Question " +
    "SET LocationID = @LocationID, Question = @Question " +
    " WHERE LocationID = @OldLocationID";

// Define the network connection to the SQL Server database.
SqlConnection objSqlConnection = new SqlConnection(WebConfigurationManager.ConnectionStrings["2020LJCDT"].ConnectionString);

// Set up the SQL command object
SqlCommand objSqlCommand = new SqlCommand();
objSqlCommand.Connection = objSqlConnection;
objSqlCommand.CommandType = CommandType.Text;
objSqlCommand.CommandText = strSQL;

// Define the input parameters
objSqlCommand.Parameters.AddWithValue("@LocationID", ddlLocation.SelectedValue);
objSqlCommand.Parameters.AddWithValue("@Question", txtQuestion.Text);
objSqlCommand.Parameters.AddWithValue("@OldLocationID", lblLocationID.Text);

【讨论】:

  • 你确定有一条记录的 id 与lblLocationID.Text 中的当前值匹配吗?我对此表示怀疑。你有例外吗?
  • 是的,有记录。也不例外。
  • 您没有遇到异常?那么你在寻找错误的数据库。
  • 它正在查找正确的数据库,因为当我更改 txtQuestion 框中的信息时,它会更新数据库。但是 LocationID 没有改变。
  • 什么是ddlLocation.SelectedValuelblLocationID.Text 在执行语句的行上放置断点?您是否尝试对这些值进行硬编码?
【解决方案2】:

在这一行放一个断点:

// Execute sql. Get number of rows affected. 
numberOfRecords = objSqlCommand.ExecuteNonQuery();

当你命中时,复制 objSqlCommand.CommandText 的值并将其粘贴到 SSMS 中并在那里执行。你得到了什么结果?

【讨论】:

  • 所做的只是获取数据库中有多少受影响的行。在本例中,它是 1。
  • 那么当您以这种方式运行查询时,它会正确更新行吗?
  • 我必须要更新的内容,LocationID 和 Question。因此,当更新发送到数据库时,Question 属性会更新,但 LocationID 属性不会更新。
  • 等等,如果你在 SSMS 中问题的最后执行查询,那么 Question 表的 Question 列会更新,但 Question 表的 LocationId 不会?
  • 我开始怀疑是某种约束或触发器之类的东西导致更新失败或被撤消。
猜你喜欢
  • 1970-01-01
  • 2014-08-17
  • 2013-02-05
  • 1970-01-01
  • 2012-12-07
  • 2016-12-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多