2016-06-19 9 views
0

データベースにフルテキストを転送しようとしていて、foreachループを使用できると考えました。しかし、私はエラーを得ることになります。データベースにdataGridViewの行テキストを挿入する方法#

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

 private void button1_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 set Time = '" + dr.Cells[0].Value + "', CarColorNumber = '" + dr.Cells[1].Value + "', Interior = '" + dr.Cells[2].Value + "', Exterior = '" + dr.Cells[3].Value + "', CPlastic = '" + dr.Cells[4].Value + "', MPlastic = '" + dr.Cells[5].Value + "', SPlastic = '" + dr.Cells[6].Value + "', PlasticB = '" + dr.Cells[7].Value + "', WashExt = '" + dr.Cells[8].Value + "', WashEng = '" + dr.Cells[9].Value + "', WashTrunk = '" + dr.Cells[10].Value + "', WashSeats = '" + dr.Cells[11].Value + "', SeatsRmv = '" + dr.Cells[12].Value + "', SeatsFit = '" + dr.Cells[13].Value + "', Notes = '" + dr.Cells[14].Value + "', where Time = '" + dr.Cells[0].Value + "' ;"; 
      MySqlConnection conn = new MySqlConnection(constring); 
      MySqlCommand command = new MySqlCommand(Query, conn); 
      MySqlDataReader myReader; 

      try 
      { 
       conn.Open(); 
       myReader = command.ExecuteReader(); 
       MessageBox.Show("Worker Successfully Added"); 
       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,...) 

私が間違って何をやっていますか? 助けてくれてありがとう。

+0

構文エラーの原因となるWHERE句の前にカンマがありますが、ここでは大きな問題があり、SQLインジェクションと呼ばれています。パラメータ化されたクエリの使用方法を学んでください – Steve

答えて

2

WHEREステートメントの前にカンマがあるので構文エラーがありますが、TIMEという単語が予約済みのため、このカンマを削除しても問題は解決しません。それはあなたの列名のためです。この問題は、キーワードの前後にバッククイックを追加して修正することができます。文字列を連結してSQL文を作成すると、入力値に一重引用符が含まれていると、コードで作成された文字列全体が無効なSQL文になります。 。

また、文字列連結アプローチの最悪の問題は、悪意のあるユーザーがSql Injectionというよく知られたハッキン​​グテクニックを使用できるようにすることです。一度解決し、すべてのあなたの問題のためにあなたが注意することは、この1

private void button1_Click(object sender, EventArgs e) 
{ 
    string constring = "Data Source = localhost; port = 3306; username = root; password = 0159"; 

    // Prepare a string where you insert parameter's placeholders instead of 
    // concatenating the grid values.... 
    string Query = @"Update TopShineDB.Table1 set CarColorNumber = @CarColorNumber, Interior = @Interior, 
        Exterior = @Exterior , CPlastic = @CPlastic, MPlastic = @MPlastic, SPlastic = @SPlastic, 
        PlasticB = @PlasticB, WashExt = @WashExt, WashEng = @WashEng, WashTrunk = @WashTrunk, 
        WashSeats = @WashSeats, SeatsRmv = @SeatsRmv, SeatsFit = @SeatsFit, Notes = @Notes 
        where `Time` = @Time"; 

    // Using statement around connection and command to destroy 
    // these objects at the end of the using block    
    using(MySqlConnection conn = new MySqlConnection(constring)) 
    using(MySqlCommand command = new MySqlCommand(Query, conn)) 
    { 
     conn.Open(); 

     // Create the list of parameters required by the query 
     // Notice that you should use the appropriate MySqlDbType 
     // for the field receiving the value. 
     command.Parameters.Add("@Time", MySqlDbType.VarChar); 
     command.Parameters.Add("@CarColorNumber", MySqlDbType.VarChar); 

     ..... create all the other parameters leaving the value null 

     try 
     { 

      foreach(DataGridViewRow dr in dataGridView1.Rows) 
      { 
       // Inside the loop update the parameters' values 
       // with data extracted by the current row... 
       command.Parameters["@Time"].Value = dr.Cells[0].Value; 
       command.Parameters["@CarColorNumber"].Value = dr.Cells[1].Value; 

       ..... set the value for all other parameters .... 

       // ExecuteNonQuery for INSERT/UPDATE/DELETE, 
       // ExecuteReader works but it is specific for reading 
       command.ExecuteNonQuery();  
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 
} 

もう一つのポイントのようなパラメータ化クエリを記述してみてください

は、Timeフィールド上のアップデートです。 where句で使用するのと同じ値で更新されるため、更新する必要はありません。

+0

スティーブさん、本当にありがとうございました。 –

+0

もう一度答えをありがとう、私はあなたがどのようにパラメータを使用して理解した。私は別のことがあります。アプリケーションはまったくエラーを表示しませんでしたが、テーブルをチェックしても変更はありませんでした。なぜこれが起こっているのか分かりますか? –

+0

このクエリは、その「時間」値を使用して更新するレコードを検索します。失敗した場合、更新は行われません。時間列のデータ型は何ですか?文字列aを渡しているので、テキストフィールドが必要です。また、ExecuteNonQueryの戻り値を読み取ってレコードが見つかったかどうかを確認できます。更新が成功した場合は1に、失敗した場合は0にする必要があります – Steve

関連する問題