如何在c#按钮单击时将所有数据从datagridview插入数据库表

时间:2014-02-19 14:22:58

标签: c#

我想在按钮点击

上将所有datagridview数据插入数据库表
    private void cs_btnfinish_Click(object sender, EventArgs e)
    {
        string StrQuery;

        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            StrQuery = "INSERT INTO tblmedisale VALUES ('" + dataGridView1.Rows[i].Cells[0].Value + "', '" + Convert.ToDecimal(dataGridView1.Rows[i].Cells[1].Value) + "', '" + Convert.ToDecimal(dataGridView1.Rows[i].Cells[2].Value) + "')";
            con.Open();
            SqlCommand cmd = new SqlCommand(StrQuery, con);
            cmd.ExecuteNonQuery();
            con.Close();
        }

        MessageBox.Show("aaaa");
    }

enter image description here

4 个答案:

答案 0 :(得分:0)

试试这个:

private void  inserttoDatagridview()
        {
            try
            {
                con.Open()
                (foreach GridViewRow rw in DataGridView1.Rows)
                {
                    cmd = New SqlCommand("insert into tblEmp (empid,empname,empdesg,dob) values (" & rw.Cells(0).Value & ",'" & rw.Cells(1).Value & "','" & rw.Cells(2).Value & "','" & CDate(rw.Cells(3).Value.ToString()) & "') ", con);
                    cmd.ExecuteNonQuery();
                }

            }
            catch (Exception ex)
            {
                throw;
            }   
            finally
            {
                con.Close()
            }
        }

答案 1 :(得分:0)

    myconn = New SqlConnection("server=DESKTOP-49GM42J;DATABASE=BAC Billing_System;Trusted_Connection=true")
  myconn.Open()

    For Each rw As DataGridViewRow In DataGridEffectData.Rows

        mycmd = New SqlCommand("insert into AllProducts (ProductID,ProductName,UnitPrice,ItemsLeft) values ('" & rw.Cells(0).Value & "','" & rw.Cells(1).Value & "','" & rw.Cells(2).Value & "','" & (rw.Cells(3).Value) & "') ", myconn)
        mycmd.ExecuteNonQuery()

    Next
    myconn.Close()

这几乎与vb.net.my中完成的代码相同。问题是每次我使用此命令通过dataGridView将一行数据插入数据库时​​,它会在下面创建一个空白行。

答案 2 :(得分:0)

在插入之前检查循环内gridview中的空行:

Private void  inserttoDatagridview()
        {
            try
            {
                con.Open()
                (foreach GridViewRow rw in DataGridView1.Rows)

if rw.Cells(0) <> Nothing then
                {
                    cmd = New SqlCommand("insert into tblEmp (empid,empname,empdesg,dob) values (" & rw.Cells(0).Value & ",'" & rw.Cells(1).Value & "','" & rw.Cells(2).Value & "','" & CDate(rw.Cells(3).Value.ToString()) & "') ", con);
                    cmd.ExecuteNonQuery();

End If
                }

        }
        catch (Exception ex)
        {
            throw;
        }   
        finally
        {
            con.Close()
        }
    }

答案 3 :(得分:0)

private void button3_Click(对象发送者,EventArgs e)         {

        dt.Clear();
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\HEAT_STORE\HEAT_STORE\bin\Debug\db.accdb");

        if(dataGridView1.Rows.Count> 0)
        {   //                                                                                                        //values                                                                                                         
            string sql = "insert into SLAB(DATE,SHIFT,HEAT_NO,THICK,WIDTH,LENGTH,PCS,ST_GRADE,LOCATION,OPTION,SLAB_NO)values(@DATE,@SHIFT,@HEAT_NO,@THICK,@WIDTH,@LENGTH,@PCS ,@ST_GRADE,@LOCATION,@OPTION,@SLAB_NO) ";//
            OleDbCommand cmd = new OleDbCommand(sql, con);

            for (int i = 0; i < dataGridView1.Rows.Count -1; i++)
          {
              cmd.Parameters.AddWithValue("@DATE", dataGridView1.Rows[i].Cells[0].Value.ToString());
              cmd.Parameters.AddWithValue("@SHIFT", dataGridView1.Rows[i].Cells[1].Value.ToString());
              cmd.Parameters.AddWithValue("@HEAT_NO", dataGridView1.Rows[i].Cells[2].Value.ToString());
              cmd.Parameters.AddWithValue("@THICK", dataGridView1.Rows[i].Cells[3].Value.ToString());
              cmd.Parameters.AddWithValue("@WIDTH", dataGridView1.Rows[i].Cells[4].Value.ToString());
              cmd.Parameters.AddWithValue("@LENGTH", dataGridView1.Rows[i].Cells[5].Value.ToString());
              cmd.Parameters.AddWithValue("@PCS ", dataGridView1.Rows[i].Cells[6].Value.ToString());
              cmd.Parameters.AddWithValue("@ST_GRADE", dataGridView1.Rows[i].Cells[7].Value.ToString());
              cmd.Parameters.AddWithValue("@LOCATION", dataGridView1.Rows[i].Cells[8].Value.ToString());
              cmd.Parameters.AddWithValue("@OPTION", dataGridView1.Rows[i].Cells[9].Value.ToString());
              cmd.Parameters.AddWithValue("@SLAB_NO", dataGridView1.Rows[i].Cells[10].Value.ToString());


            }
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            da.Fill(dt);
            dataGridView1.DataSource = dt;  
            MessageBox.Show("SaccessFuly");


        }
    }