更新查询:语法不正确

发布于 2024-10-06 12:52:00 字数 1989 浏览 3 评论 0原文

我的 Windows 窗体中有一个按钮可以更新每个表。但是,我收到错误 SQLException was unhandled。 “=”附近的语法不正确。

这是我在更新按钮中的代码:

  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!!!");
    }

   }
  }

任何人都可以告诉我这些语句有什么问题吗?谢谢!

I have a button in my windows forms which UPDATES every table. However, I am getting error SQLException was unhandled. Incorrect syntax near '='.

This is my code in Update Button:

  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!!!");
    }

   }
  }

Can anyone tell me what is wrong with these statements? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

颜漓半夏 2024-10-13 12:52:00

为什么不简单地在单个更新语句中完成它呢?所以像这样:

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

Why not simply do it in a single update statement. So something like:

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);
我们的影子 2024-10-13 12:52:00

除了我评论中提到的内容之外;我没有发现你的 SQL 语法有什么问题。您的 FormattedValue 很可能在字符串本身中包含无效字符,例如 ' ,这会导致 SQL 错误。构建字符串后打印出 CommandText 本身的值,看看它实际是什么样子。

Aside from what was mentioned in my comment; I don't see anything wrong with the syntax of your SQL. It's quite possible that your FormattedValue has an invalid character like a ' in the string itself, which would lead to a SQL error. Print out the value of the CommandText itself after the string has been built to see what it actually looks like.

探春 2024-10-13 12:52:00
  1. 您的UPDATE语句不正确。 UPDATE语法是:

    更新表
       放
          第 1 列 = '值',
          列 2 = '值 2'
       在哪里
          健康)状况;
    
  2. 每次分配给 cmd 时都会覆盖您的语句。您可能想要更多类似的东西:

    cmd = "更新表";
    cmd += "SET 列 1 = '" + 值 + "',";
    cmd += "设置列2 = " + intValue;
    cmd += "WHERE idRow = '" + rowToUpdateValue + "'";
    

另外,如果这没有帮助,请检查您是否没有尝试使用“char”值检查 INT 列。

  1. Your UPDATE statement is incorrect. UPDATE syntax is:

    UPDATE table
       SET
          column1 = 'value',
          column2 = 'value2'
       WHERE
          condition;
    
  2. You are overwriting your statement every time you assign to cmd. You probably want something more like:

    cmd =  "UPDATE table";
    cmd += "SET column1 = '" + value + "',";
    cmd += "SET column2 = " + intValue;
    cmd += "WHERE idRow = '" + rowToUpdateValue + "'";
    

Also, if this doesn't help, check that you are not trying to check an INT column using a 'char' value.

清引 2024-10-13 12:52:00

请检查您的条件:

if(row.Cells[5].FormattedValue.ToString())
then execute update query

Please check your conditions thus:

if(row.Cells[5].FormattedValue.ToString())
then execute update query
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文