private void button4_Click(object sender, System.EventArgs e)
{
string id;
string myConnectionString = "";
if(myConnectionString == "") 
{
         //myConnectionString = "Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;";
         myConnectionString = "Initial Catalog=Northwind;Data Source=lyj;user=sa;password=sa;";
}
try
{
SqlConnection myConnection = new SqlConnection(myConnectionString);
//string myInsertQuery = "DELECT FROM Customers WHERE;
SqlCommand myCommand = new SqlCommand();
id = ((DataTable)this.dataGrid1.DataSource).Rows[this.dataGrid1.CurrentRowIndex]["CustomerID"].ToString();
myCommand.CommandText =  "UPDATE Customers (CustomerID,CompanyName, ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Fax) Values ('"+textBox11.Text+"','"+textBox1.Text+"', '"+textBox2.Text+"', '"+textBox3.Text+"', '"+textBox4.Text+"', '"+textBox5.Text+"', '"+textBox6.Text+"', '"+textBox7.Text+"', '"+textBox8.Text+"', '"+textBox9.Text+"', '"+textBox10.Text+"') WHERE CustomerID ='"+id+"'";
myCommand.Connection = myConnection;
myConnection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close(); //label12.Visible = true;
}
catch(Exception err)
{
MessageBox.Show(err.ToString());
}
}主要是update语句,不用总是update…set…where…吧!我不知道错哪了!总是走到myCommand.ExecuteNonQuery();报错!

解决方案 »

  1.   

    insert into table (fieldlist) values (valuelist)
    update table set fld1 = val1, fld2 = val2... where fldn = valn
    如果Text里有单引号也会出错('附近有语法错误),用参数形式或者textBox.Text.Replace("'", "''")
      

  2.   

    设个断点,把myCommand.CommandText在查询分析器中执行一下就知道哪有错了
      

  3.   

    1、UPDATE语句应该这么写:"UPDATE Customers SET CustomerID = '" + textBox11.Text + "'"
    2、//string myInsertQuery = "DELECT FROM Customers WHERE;
    应该是:DELETE FROM Customers WHERE 
      

  4.   

    楼主你的应该是insert,不是update
      

  5.   

    是insert,不是update,要用update就要像古道热肠那样写!
      

  6.   

    楼主, update tablename1
           set columnname1=value1 [, columnname2=value2, ...]
           [where  conditions]需要加强基本SQL的学习
      

  7.   

    myCommand.CommandText =  "UPDATE Customers SET CustomerID = '"+textBox11.Text+"',CompanyName ='"+textBox1.Text+"',ContactName = '"+textBox2.Text+"',ContactTitle = '"+textBox3.Text+"',Address = '"+textBox4.Text+"',City = '"+textBox5.Text+"',Region = '"+textBox6.Text+"',PostalCode = '"+textBox7.Text+"',Country = '"+textBox8.Text+"',Phone = '"+textBox9.Text+"',Fax = '"+textBox10.Text+"' WHERE CustomerID ='"+id+"'";ok