2016-03-29 11 views
2

私はレコードをSQLデータベースに挿入しようとしていますが、以下はボタンクリックから挿入するためのコードです。C#のWindowsアプリケーションでデータグリッドビューのエントリをSQLデータベースに挿入するにはどうすればよいですか?

レコードを挿入できず、コードを実行すると常にエラーが発生しています.....コードに問題がありますが、問題がどこで発生しているのかわかりません.....

エラーメッセージは、「付近に正しくない構文 『』 ..」である

private void ADD_button_Click(object sender, EventArgs e) 
    { 
     try 
        { 
         using (SqlConnection con = new SqlConnection(sqlconn)) 
         { 
          con.Open(); 

          for (int i = 1; i < dataGridView.Rows.Count; i++) 
          { 
           string sql = @"INSERT INTO ERSBusinessLogic VALUES (" 
            + dataGridView.Rows[i].Cells["ERSBusinessLogic_ID"].Value + ", " 
            + dataGridView.Rows[i].Cells["ERSBusinessLogic_Formula"].Value + ", " 
            + dataGridView.Rows[i].Cells["ERSBusinessLogic_InputsCount"].Value + ", " 
            + dataGridView.Rows[i].Cells["ERSBusinessLogic_Inputs"].Value + ");"; 

           SqlCommand cmd = new SqlCommand(sql, con); 
           cmd.ExecuteNonQuery(); 

          } 
         } 
     } 

     catch (Exception ex) 
     { 
      MessageBox.Show("Error : " + ex.Message); 
     } 
     finally 
     { 
      con.Close(); 

     } 
+0

を試してみて、エラーメッセージが、それがクエリ内のすべての入力のデータベーステーブルのデータ型の用心 – simsim

+0

何を言うん表示:+ dataGridView.Rows [i]の.C値+ "、"dataGridView.Rows [i] .Cells ["ERSBusinessLogic_InputsCount"]。値 "" ERSBusinessLoginfo "。値+"、 "dataGridView.Rows [i] .Cells [" ERSBusinessLogic_Form "]。 + "、" + dataGridView.Rows [i] .Cells ["ERSBusinessLogic_Inputs"]。値+ "、" ");";一致する必要があります – Ian

+0

私の編集を確認してください –

答えて

3

この

private void button1_Click(object sender, EventArgs e) 
    { 
     // Getting data from DataGridView 
     DataTable myDt = new DataTable(); 
     myDt = GetDTfromDGV(dataGridView1); 

     // Writing to sql 
     WriteToSQL(myDt); 
    } 

    private DataTable GetDTfromDGV(DataGridView dgv) 
    { 
     // Macking our DataTable 
     DataTable dt = new DataTable(); 
     foreach (DataGridViewColumn column in dgv.Columns) 
     { 
      dt.Columns.Add(column.Name, typeof(string)); 
     } 
     // Getting data 
     foreach (DataGridViewRow dgvRow in dgv.Rows) 
     { 
      DataRow dr = dt.NewRow(); 
      for (int col = 0; col < dgv.Columns.Count; col++) 
      { 
       dr[col] = dgvRow.Cells[col].Value; 
      } 
      dt.Rows.Add(dr); 
     } 
     // removing empty rows 
     for (int row = dt.Rows.Count - 1; row >= 0; row--) 
     { 
      bool flag = true; 
      for (int col = 0; col < dt.Columns.Count; col++) 
      { 
       if (dt.Rows[row][col] != DBNull.Value) 
       { 
        flag = false; 
        break; 
       } 
      } 
      if (flag == true) 
      { 
       dt.Rows.RemoveAt(row); 
      } 
     } 
     return dt; 
    } 
    private void WriteToSQL(DataTable dt) 
    { 
     string connectionStringSQL = "Your connection string"; 
     using (SqlConnection sqlConn = new SqlConnection(connectionStringSQL)) 
     { 
      SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(sqlConn); 
      // Setting the database table name 
      sqlBulkCopy.DestinationTableName = "Table_1_temp"; 
      // Mapping the DataTable columns with that of the database table 
      sqlBulkCopy.ColumnMappings.Add(dt.Columns[0].ColumnName, "sql_col1"); 
      sqlBulkCopy.ColumnMappings.Add(dt.Columns[1].ColumnName, "sql_col2"); 
      sqlBulkCopy.ColumnMappings.Add(dt.Columns[2].ColumnName, "sql_col3"); 
      sqlBulkCopy.ColumnMappings.Add(dt.Columns[3].ColumnName, "sql_col4"); 
      sqlBulkCopy.ColumnMappings.Add(dt.Columns[4].ColumnName, "sql_col5"); 
      sqlBulkCopy.ColumnMappings.Add(dt.Columns[5].ColumnName, "sql_col6"); 
      sqlConn.Open(); 
      sqlBulkCopy.WriteToServer(dt); 
     } 
    } 
0
try 
     { 
     using (SqlConnection con = new SqlConnection(sqlconn)) 
     { 
      using (SqlCommand cmd = new SqlCommand()) 
      { 
       cmd.Connection = con; 
       con.Open(); 
       for (int i = 1; i < dataGridView.Rows.Count; i++) 
       {   
       string sql = @"INSERT INTO ERSBusinessLogic VALUES (" 
          +dataGridView.Rows[i].Cells["ERSBusinessLogic_ID"].Value + ", " 
           + dataGridView.Rows[i].Cells["ERSBusinessLogic_Formula"].Value + ", " 
           + dataGridView.Rows[i].Cells["ERSBusinessLogic_InputsCount"].Value + ", " 
           + dataGridView.Rows[i].Cells["ERSBusinessLogic_Inputs"].Value + ");"; 

          cmd.CommandText = sql; 
          cmd.ExecuteNonQuery(); 

       } 
     } 
    } 

    catch (Exception ex) 
    { 
     MessageBox.Show("Error : " + ex.Message); 
    } 
    finally 
    { 
     con.Close(); 

    } 

それでも動作しない場合は、右側の文字列の後にブレークポイントを置いて、コピー文字列の値とデータベース上でそれをテストします。たぶん、あなたの価値は問題であり、さまざまなタイプです。

+0

こんにちはあなたは正しいです......私はグリッドビューからも挿入する必要があるnull値があります...しかし、列はグリッドビューからnullキーワードを受け入れていません....どのようにすればよいでしょうか? –

+0

あなたのテーブルの中に空の値を追加することは許されないかもしれませんが、それらの値がNULLの場合は、dbカラムをnullにする必要があります。知っている –

0

各データ列に ''を追加してみてください。下記のように、

for (int i = 1; i < dataGridView.Rows.Count; i++) 
         { 
          string sql = @"INSERT INTO ERSBusinessLogic VALUES ('" 
           + dataGridView.Rows[i].Cells["ERSBusinessLogic_ID"].Value + "', '" 
           + dataGridView.Rows[i].Cells["ERSBusinessLogic_Formula"].Value + "', '" 
           + dataGridView.Rows[i].Cells["ERSBusinessLogic_InputsCount"].Value + "', '" 
           + dataGridView.Rows[i].Cells["ERSBusinessLogic_Inputs"].Value + "');"; 

          SqlCommand cmd = new SqlCommand(sql, con); 
          cmd.ExecuteNonQuery(); 

         } 
1

データ型の不一致のようです。テーブルの列が数値型(ERSBusinessLogic_IDが整数)の場合はデータテーブルをチェックし、コードは "+ Convert.ToInt32(dataGridView.Rows [i] .Cells [" ERSBusinessLogic_ID "] .Value)+"、

これはvar-char型の場合、値は '+ Convert.ToString(dataGridView.Rows [i] .Cells ["ERSBusinessLogic_Formula"] .Value)+ "'のように一重引用符( '') 、

関連する問題