如何使用 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);
}
}
}
感谢您的帮助。
最佳答案
string Query = "Update TopShineDB.Table1 (Time, CarColorNumber, Interior, Exterior, CPlastic, MPlastic, SPlastic, PlasticB, WashExt, WashEng, WashTrunk, WashSeats, SeatsRmv, SeatsFit, Notes) 值 ('"+ 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] + "')";
上面的 SQL 不是有效的更新语句。正确的更新语法应该是:-
UPDATE TopShineDB.Table1
SET Time = '" + dr.Cells[0] + "',
Notes = '"+ dr.Cells[14] + "'
WHERE Table1ID = ID from the grid
告诉我这是行不通的
关于c# - 如何使用 dataGridView C# 中的行更新数据库中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37907702/