簡體   English   中英

如何使用dataGridView C#中的行更新數據庫中的行

[英]How to update rows in my database with rows from dataGridView C#

如何使用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);
            }
        }
    }

謝謝您的幫助。

字符串Query =“ Update TopShineDB.Table1(時間,CarColorNumber,內部,外部,CPlastic,MPlastic,SPlastic,PlasticB,WashExt,WashEng,WashTrunk,WashSeats,SeatsRmv,SeatsFit,注釋)值('” + 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語法應為:-

UPDATE TopShineDB.Table1 
SET Time = '" + dr.Cells[0] + "', 
Notes = '"+ dr.Cells[14] + "' 
WHERE Table1ID = ID from the grid

讓我知道那是行不通的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM