如何使用dataGridView1中的行更新数据库中的行?
显示此错误:
you have an error in your sql syntax check the manual that corresponds to your mysql server version for the right syntax to use near '(Time, CarColorNumber, Interior, Exterior, CPlastic,...)
这是我到目前为止的代码:
private void button2_Click(object sender, EventArgs e)
{
foreach ( DataGridViewRow dr in dataGridView1.Rows)
{
string constring = "Data Source = localhost; port = 3306; username = root; password = 0159";
string Query = " Update TopShineDB.Table1 (Time, CarColorNumber, Interior, Exterior, CPlastic, MPlastic, SPlastic, PlasticB, WashExt, WashEng, WashTrunk, WashSeats, SeatsRmv, SeatsFit, Notes) VALUES ('" + dr.Cells[0] + "','" + dr.Cells[1] + "','" + dr.Cells[2] + "','" + dr.Cells[3] + "','" + dr.Cells[4] + "','" + dr.Cells[5] + "','" + dr.Cells[6] + "','" + dr.Cells[7] + "','" + dr.Cells[8] + "','" + dr.Cells[9] + "','" + dr.Cells[10] + "','" + dr.Cells[11] + "','" + dr.Cells[12] + "','" + dr.Cells[13] + "','" + dr.Cells[14] + "')";
MySqlConnection conn = new MySqlConnection(constring);
MySqlCommand command = new MySqlCommand(Query, conn);
MySqlDataReader myReader;
try
{
conn.Open();
myReader = command.ExecuteReader();
MessageBox.Show("Table Successfully Updated");
while (myReader.Read())
{
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
感谢您的帮助。
答案 0 :(得分:3)
string Query ="更新TopShineDB.Table1(Time,CarColorNumber,Interior,Exterior,CPlastic,MPlastic,SPlastic,PlasticB,WashExt,WashEng,WashTrunk,WashSeats,SeatsRmv,SeatsFit,Notes)VALUES('" + dr.Cells [ 0] +"','" + dr.Cells [1] +"','" + dr.Cells [ 2] +"','" + dr.Cells [3] +"','" + dr.Cells [ 4] +"','" + dr.Cells [5] +"','" + dr.Cells [ 6] +"','" + dr.Cells [7] +"','" + dr.Cells [ 8] +"','" + dr.Cells [9] +"','" + dr.Cells [ 10] +"','" + dr.Cells [11] +"','" + dr.Cells [ 12] +"','" + dr.Cells [13] +"','" + dr.Cells [ 14] +"')&#34 ;;
上面的SQL不是有效的更新语句..正确的UPDATE语法应该是: -
UPDATE TopShineDB.Table1
SET Time = '" + dr.Cells[0] + "',
Notes = '"+ dr.Cells[14] + "'
WHERE Table1ID = ID from the grid
让我知道是不行的