VB.NET Insert DataGridView contents into Database

前端 未结 4 1550
粉色の甜心
粉色の甜心 2021-02-03 16:16

Problem:

I need to dump the contents of my DataGridView into a SQL Server Database Table. I\'ve got the datagridview loading fine, no problems there.

相关标签:
4条回答
  • 2021-02-03 16:28

    You can call: command.ExecuteNonQuery to insert data to the DB.

    More info here

    0 讨论(0)
  • 2021-02-03 16:29

    Well, you need to execute the command, not simply add to the DataAdapter
    Also, as it coded now, you don't need the DataAdapter at all.

    Dim connection As New Data.SqlClient.SqlConnection
    Dim command As New Data.SqlClient.SqlCommand
    
    connection.ConnectionString = "Server= server; Database= DB; integrated security=true"
    command.CommandText = "INSERT INTO <table> (Col1, Col2, Col3, Col4) VALUES (@Name, @Property, @Value, @Date)"
    
    command.Parameters.Add("@ServerName", SqlDbType.VarChar)
    command.Parameters.Add("@Property", SqlDbType.VarChar)
    command.Parameters.Add("@Value", SqlDbType.VarChar)
    command.Parameters.Add("@CaptureDate", SqlDbType.DateTime)
    connection.Open()
    command.Connection = connection
    
    For i As Integer = 0 To DataGridView.Rows.Count - 1
        command.Parameters(0).Value = dgvServerConfig.Rows(i).Cells(0).Value
        command.Parameters(1).Value = dgvServerConfig.Rows(i).Cells(1).Value
        command.Parameters(2).Value = dgvServerConfig.Rows(i).Cells(2).Value
        command.Parameters(3).Value = dgvServerConfig.Rows(i).Cells(3).Value
        command.ExecuteNonQuery()
    Next
    

    However this calls the database to insert one row at a time. I think it is better to look at the SqlDataAdapter.Update method that resolves the insert/update work with just one call.

    Using the SqlDataAdapter.Update method, requires that you save in a global variable the Adapter used at the moment in which you have filled the DataGridView and add a SqlCommandBuilder that generates for you the InsertCommand, UpdateCommand and DeleteCommand

        ' At form loading'
        Dim adapter As New OleDbDataAdapter()
        adapter.SelectCommand = New OleDbCommand("SELECT COL1, COL2,COL3,COL4 FROM TABLE", connection)
        Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(adapter)
        connection.Open()
        Dim myTable As DataTable = New DataTable
        adapter.Fill(myTable)
        DataGridView.DataSource = myTable
        ....
    
        ' at grid save'
        Dim myTable = CType(DataGridView.DataSource, DataTable)
        adapter.Update(myTable)
    
    0 讨论(0)
  • 2021-02-03 16:32

    For each object in your GridView, you need to find it and determine its value. So for example,

    DropDownList ddlToLoc = (DropDownList)gvMovesMod.Rows[0].FindControl("ddlToLoc");
    

    Then, determine ddlToLoc's SelectedValue and insert to database

    0 讨论(0)
  • 2021-02-03 16:38

    TRY THIS METHOD

     For index As Integer = 0 To DataGridView1.RowCount - 1
    
                Dim connectionString = "Data Source=localhost;port=3306;Initial Catalog=hasna;User Id=root;password=''; Convert Zero Datetime=True"
                Dim query0 = "insert into itemledgerfinal(Date,BillDate,BillNo,ItemName,GST,Rate,MRP,TotalQty,PurchaseRate,WholesaleRate,Total,Type,OpeningBalance,Purchase,Sale,ClosingBalance,ID) values(@Date,@BillDate,@BillNo,@ItemName,@GST,@Rate,@MRP,@TotalQty,@PurchaseRate,@WholesaleRate,@Total,@Type,@OpeningBalance,@Purchase,@Sale,@ClosingBalance,@ID)"
    
    
                Dim connection0 As New MySqlConnection(connectionString)
                Dim command0 As New MySqlCommand(query0, connection0)
    
    
                command0.Parameters.AddWithValue("@Date", DataGridView1.Rows(index).Cells(0).Value)
                command0.Parameters.AddWithValue("@BillDate", DataGridView1.Rows(index).Cells(1).Value) 
                command0.Parameters.AddWithValue("@BillNo", DataGridView1.Rows(index).Cells(2).Value)
                command0.Parameters.AddWithValue("@ItemName", DataGridView1.Rows(index).Cells(3).Value)
                command0.Parameters.AddWithValue("@GST", DataGridView1.Rows(index).Cells(4).Value)
                command0.Parameters.AddWithValue("@Rate", DataGridView1.Rows(index).Cells(5).Value)
                command0.Parameters.AddWithValue("@MRP", DataGridView1.Rows(index).Cells(6).Value)
                command0.Parameters.AddWithValue("@TotalQty", DataGridView1.Rows(index).Cells(7).Value)
                command0.Parameters.AddWithValue("@PurchaseRate", DataGridView1.Rows(index).Cells(8).Value)
                command0.Parameters.AddWithValue("@WholesaleRate", DataGridView1.Rows(index).Cells(9).Value)
                command0.Parameters.AddWithValue("@Total", DataGridView1.Rows(index).Cells(10).Value)
                command0.Parameters.AddWithValue("@Type", DataGridView1.Rows(index).Cells(11).Value)
                command0.Parameters.AddWithValue("@OpeningBalance", DataGridView1.Rows(index).Cells(12).Value)
                command0.Parameters.AddWithValue("@Purchase", DataGridView1.Rows(index).Cells(13).Value)
                command0.Parameters.AddWithValue("@Sale", DataGridView1.Rows(index).Cells(14).Value)
                command0.Parameters.AddWithValue("@ClosingBalance", DataGridView1.Rows(index).Cells(15).Value)
                command0.Parameters.AddWithValue("@ID", DataGridView1.Rows(index).Cells(16).Value)
                connection0.Open()
                command0.Connection = Connection
                command0.ExecuteNonQuery()
    
            Next
    
    0 讨论(0)
提交回复
热议问题