Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试使用update命令更新SQL数据库_C#_Asp.net_Sql Server - Fatal编程技术网

C# 尝试使用update命令更新SQL数据库

C# 尝试使用update命令更新SQL数据库,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我有一个问题,我的更新命令不能正常工作。由于某种原因,更新LocationID时会出现问题。我可以毫无问题地更新问题,只是不能更新位置ID。我正在下拉列表中选择LocationID,它正确地传递了值,但不会更新数据库。有人看到什么不对劲吗 以下是我的数据库表的屏幕截图: 以下是正在传递的值: // Find controls for on edit. DropDownList ddlLocation = (DropDownList)(sender as ListView).EditItem.

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

以下是我的数据库表的屏幕截图:

以下是正在传递的值:

// 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();

删除“2086”下的单引号并使用字符串中的参数:

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

在此行上放置断点:

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);

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

删除
'2086'
下的单引号-并使用参数!您是否收到任何错误消息?看起来一切正常,但从阅读中可以得到一些启示:1)2086左右不需要单引号,因为LocationID是INT(但我认为SQL会翻译),2)您正在创建参数,但没有使用它们。LocationID上有外键或约束吗?@jefftrotman LocationID是外键。-您不应该将SQL语句连接在一起-使用参数化查询来避免SQL注入-签出@marc_s I changed all to parameterized queries。你仍然有问题。SQL已成功执行,但未更新LocationID。问题属性是唯一正在更新的属性。您是否确认存在id与
lblLocationID.Text
中的当前值匹配的记录?我对此表示怀疑。你得到豁免了吗?是的,有记录。没有例外,你没有例外吗?然后你在找错误的数据库。它在找正确的数据库,因为当我更改TXT问题框中的信息时,它会更新数据库。但是LocationID没有更改。在执行语句的行上放置断点时,什么是
ddlLocation.SelectedValue
lblLocationID.Text
?您是否尝试硬编码这些值?所做的只是获取数据库中受影响的行数。对于本例,它是1。因此,当您以这种方式运行查询时,查询正在正确更新行?我必须更新我试图更新的内容,LocationID和Question。因此,当更新发送到数据库时,将更新问题属性,但不会更新LocationID属性。等等,如果您在SSMS中问题的最后执行查询,问题表的“问题”列会更新,但问题表的LocationId不会更新?我开始怀疑是某种约束或触发器导致更新失败或撤消。
// Execute sql. Get number of rows affected. 
numberOfRecords = objSqlCommand.ExecuteNonQuery();