c# - 如何使用 dataGridView C# 中的行更新数据库中的行

标签 c# mysql database datagridview row

如何使用 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/

相关文章:

php - 如何一一管理上传操作

mysql - MySQL SELECT 的速度(在变化很小的列上索引是否值得)?

c# - 为什么不是 DbConnection 而不是 SqlConnection 或 OracleConnection?

php/sql AND 语句不起作用

c# - 类变量的默认值

c# - RestSharp 执行同步,而不是异步

c# - 简单的 asp.net 标记帮助器列表 <> 问题

c# - 无法使用 JustMock 模拟简单对象数组

mysql - 针对特定极慢查询的优化

database - 数据库如何存储和索引字符串?