2016-03-29 2 views
2

Ich versuche, Datensätze in SQL-Datenbank einzufügen und unten ist mein Code für die Einfügung von einem Klick auf die Schaltfläche.Wie Datensätze aus Daten Gridview Eintrag in SQL-Datenbank in C# Windows-Anwendung einfügen?

Ich war nicht in der Lage, die Datensätze einzufügen, und es wirft einen Fehler die ganze Zeit, wenn ich den Code ausführen ..... Ich weiß, dass etwas im Code falsch ist, aber ich, nicht sicher, wo das Problem auftritt .....

die Fehlermeldung lautet „Falsche Syntax in der Nähe‚‘..“

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

die Fehlermeldung anzeigen, was es – simsim

+0

der Datentypen der Datenbanktabelle Beware sagt alle Eingänge in der Abfrage: + dataGridView.Rows [i] .C ells ["ERSBusinessLogic_ID"]. Wert + "," + dataGridView.Rows [i] .Cells ["ERSBusinessLogic_Formula"]. Wert + "," + dataGridView.Rows [i] .Cells ["ERSBusinessLogic_InputsCount"]. Wert + "," + dataGridView.Rows [i] .Cells ["ERSBusinessLogic_Inputs"]. Wert + "," ");"; Sie müssen übereinstimmen – Ian

+0

überprüfen Sie bitte meine Änderungen –

Antwort

3

Versuchen sie, diese

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

    } 

Wenn immer noch nicht funktioniert, direkt nach der Zeichenfolge einen Haltepunkt setzen, und kopieren den String-Wert und testen Sie es in Ihrer Datenbank. Vielleicht sind deine Werte das Problem, verschiedene Typen.

+0

Hallo, Sie haben Recht ...... Ich habe Null-Werte, die auch aus der Rasteransicht eingefügt werden müssen ... aber die Spalte akzeptiert kein Null-Schlüsselwort aus der Rasteransicht .... –

+0

Vielleicht sollten Sie nicht erlauben, leere Werte in Ihrem Grid hinzuzufügen, wenn in Ihrer Tabelle angefordert werden. Wenn diese Werte Null sein können, sollten Sie db-Spalten ändern, um Null zu akzeptieren. Ich weiß es –

0

Versuchen Sie, jeder Datenspalte '' hinzuzufügen. Wie unten,

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

Es scheint ein Datentyp Mismatch-Problem. Überprüfen Sie Ihre Datentabelle, wenn die Tabellenspalte vom numerischen Typ ist (zB: ERSBusinessLogic_ID ist Integer), dann sollte der Code wie "+ Convert.ToInt32 (dataGridView.Rows [i] .Cells [" ERSBusinessLogic_ID "]. Value) +",

sein

Und wenn es Var-Char-Typ ist, sollte Wert in Anführungszeichen ('') wie '"+ Convert.ToString (DataGridView.Rows [i] .Cells [" ERSBusinessLogic_Formula "]. Wert) +' '

,
Verwandte Themen