我的Windows窗体中有一个按钮,可以更新每个表格。但是,我收到错误SQLException未处理。 '='附近的语法不正确。
这是我在更新按钮中的代码:
public void btnUpdate_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
try
{
//MessageBox.Show(row.Cells[7].FormattedValue.ToString());
System.Data.SqlClient.SqlConnection sqlConnection1 =
new System.Data.SqlClient.SqlConnection("server=Test\\Test; Integrated Security=true; Database=Testing;");
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "UPDATE dbo.JobStatus SET ShipTrackingNumber = '" + row.Cells[7].FormattedValue.ToString() + "' WHERE jobtableid = " + row.Cells[5].FormattedValue.ToString();
cmd.CommandText = "UPDATE dbo.JobStatus SET ShipMethodTransmitted = '" + row.Cells[8].FormattedValue.ToString() + "' WHERE jobtableid = " + row.Cells[5].FormattedValue.ToString();
cmd.CommandText = "UPDATE dbo.JobStatus SET DateShipTransmitProcessed = '" + row.Cells[9].FormattedValue.ToString() + "' WHERE jobtableid = " + row.Cells[5].FormattedValue.ToString();
cmd.CommandText = "UPDATE dbo.JobStatus SET ShipmentProcessedBy = '" + row.Cells[10].FormattedValue.ToString() + "' WHERE jobtableid = " + row.Cells[5].FormattedValue.ToString();
cmd.CommandText = "UPDATE dbo.JobStatus SET Critical = '" + row.Cells[11].FormattedValue.ToString() + "' WHERE jobtableid = " + row.Cells[5].FormattedValue.ToString();
cmd.CommandText = "UPDATE dbo.JobStatus SET ShipTransmitStatus = '" + row.Cells[13].FormattedValue.ToString() + "' WHERE jobtableid = " + row.Cells[5].FormattedValue.ToString();
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
}
catch (Exception e)
{
MessageBox.Show("Update Failed!!!");
}
}
}
有谁能告诉我这些陈述有什么问题?谢谢!
答案 0 :(得分:4)
为什么不在一个更新语句中简单地执行此操作。如下所示:
var sql = new StringBuilder();
sql.AppendLine( "UPDATE dbo.JobStatus" );
sql.AppendLine( "Set ShipTrackingNumber = @TrackingNumber" );
sql.AppendLine( ", DateShipTransmitProcessed = @DateShipTransmitProcessed" );
sql.AppendLine( ", ShipmentProcessedBy = @ShipmentProcessedBy" );
sql.AppendLine( ", Critical = @Critical" );
sql.AppendLine( ", ShipTransmitStatus = @ShipTransmitStatus" );
sql.AppendLine( "Where jobtableId = @jobTableId" );
cmd.Connection = sqlConnection1;
cmd.CommandText = sql.ToString();
cmd.Parameters.AddWithValue("@TrackingNumber", row.Cells[7].FormattedValue);
cmd.Parameters.AddWithValue("@DateShipTransmitProcessed", row.Cells[8].FormattedValue);
cmd.Parameters.AddWithValue("@ShipmentProcessedBy", row.Cells[9].FormattedValue);
cmd.Parameters.AddWithValue("@Critical", row.Cells[10].FormattedValue);
cmd.Parameters.AddWithValue("@ShipTransmitStatus", row.Cells[11].FormattedValue);
cmd.Parameters.AddWithValue("@jobTableId", row.Cells[5].FormattedValue);
答案 1 :(得分:0)
除了我的评论中提到的内容;我没有看到SQL的语法有什么问题。您的FormattedValue很可能在字符串本身中有一个无效字符,这会导致SQL错误。在构建字符串之后打印出CommandText本身的值,以查看它实际上是什么样的。
答案 2 :(得分:0)
请检查您的条件:
if(row.Cells[5].FormattedValue.ToString())
then execute update query
答案 3 :(得分:0)
您的UPDATE
声明不正确。 UPDATE
语法为:
UPDATE table
SET
column1 = 'value',
column2 = 'value2'
WHERE
condition;
每次分配到cmd
时,您都会覆盖您的陈述。你可能想要更像的东西:
cmd = "UPDATE table";
cmd += "SET column1 = '" + value + "',";
cmd += "SET column2 = " + intValue;
cmd += "WHERE idRow = '" + rowToUpdateValue + "'";
此外,如果这没有用,请检查您是否尝试使用'char'值检查INT列。