开发者

UPDATE Query : Incorrect Syntax

开发者 https://www.devze.com 2023-01-29 18:50 出处:网络
I have a button in my windows forms which UPDATES every table. However, I am getting error SQLException was unhandled. Incorrect syntax near \'=\'.

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开发者_JAVA百科 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!


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


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.


  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.


Please check your conditions thus:

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

精彩评论

暂无评论...
验证码 换一张
取 消