2016-06-19 8 views
0

データベースの行をdataGridView1の行で更新するにはどうすればいいですか?データベースの行をdataGridViewの行で更新する方法C#

それはこのエラーを示しています。助けを

 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); 
      } 
     } 
    } 

感謝:

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,...) 

が、これは私がこれまで持っているコードです。

+1

どのようなエラーが動作しないのですか?あなたは質問にエラーを追加できますか? – scartag

+0

@scartagは既に更新されています –

+0

データバインディングの仕組みを学ぶことを検討してください。 UI要素からデータを抜き出していると、あまりにも多くの作業をしています。データが正しくバインドされていてデータアダプタを持っていれば、データを取得する複雑で注入が容易な方法ではなく、 'Update()'を呼び出すことができます。 – Crowcoder

答えて

3

string Query = "TopShineDB.Table1(時間、CarColorNumber、内部、外部、CPlastic、MPlastic、SPLastic、PlasticB、WashExt、WashEng、WashTrunk、WashSeats、SeatsRmv、SeatsFit、Notes)VALUES( '+ dr。セル[0] + "'、" + dr.Cells [1] + "'、" "+ dr.Cells [2] +" '、 "" + dr.Cells [3] + "'、 dr.Cells [5] + "'、+" dr.Cells [6] + "'、" "+ dr.Cells [7] +" '、 "+ dr.Cells [8] +" "、+" dr.Cells [9] + "'、" + dr.Cells [10] + "'、 '+ "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 

は私を知ってみましょうそれはまさに

+0

"グリッドからのID"とは何ですか? –

+0

と明瞭にするために、このように書かなければなりませんか? "TopShineDB.Table1の更新時刻= '" + dr.Cells [0] + "'、...........、Notes = '" + dr.Cells [14] + "' WHERE Table1ID =グリッドからのID " –

+0

IDは一意の識別子です。 – Kamamba

関連する問題