3

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. I'm just not familiar enough with VB.NET to understand how to get that data into a DB table.

Code: (so far)

    Dim connection As New Data.SqlClient.SqlConnection
    Dim dataAdapter As New Data.SqlClient.SqlDataAdapter
    Dim command As New Data.SqlClient.SqlCommand
    Dim dataSet As New Data.DataSet

    connection.ConnectionString = "Server= server; Database= DB; integrated security=true"
    command.CommandText = "INSERT INTO <table> (Col1, Col2, Col3, Col4) VALUES (@Name, @Property, @Value, @Date)"

    dataAdapter.InsertCommand.Parameters.Add("@ServerName", SqlDbType.VarChar)
    dataAdapter.InsertCommand.Parameters.Add("@Property", SqlDbType.VarChar)
    dataAdapter.InsertCommand.Parameters.Add("@Value", SqlDbType.VarChar)
    dataAdapter.InsertCommand.Parameters.Add("@CaptureDate", SqlDbType.DateTime)

    For i As Integer = 0 To DataGridView.Rows.Count - 1
        dataAdapter.InsertCommand.Parameters(0).Value = dgvServerConfig.Rows(i).Cells(0).Value
        dataAdapter.InsertCommand.Parameters(1).Value = dgvServerConfig.Rows(i).Cells(1).Value
        dataAdapter.InsertCommand.Parameters(2).Value = dgvServerConfig.Rows(i).Cells(2).Value
        dataAdapter.InsertCommand.Parameters(3).Value = dgvServerConfig.Rows(i).Cells(3).Value
    Next

    connection.Open()
    command.Connection = connection
    dataAdapter.SelectCommand = command

What am I missing here? Nothing is getting inserted into my table. Any help would be appreciated. Like I said, I'm not very familiar with VB so take it easy on me.

4

4 回答 4

7

好吧,您需要执行命令,而不是简单地添加到 DataAdapter
此外,正如现在编码的那样,您根本不需要 DataAdapter。

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

但是,这会调用数据库一次插入一行。我认为最好看一下SqlDataAdapter.Update方法,该方法只需一次调用即可解决插入/更新工作。

使用 SqlDataAdapter.Update 方法,要求您将在填充 DataGridView 时使用的适配器保存在全局变量中,并添加为您生成 InsertCommand、UpdateCommand 和 DeleteCommand 的 SqlCommandBuilder

    ' 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)
于 2013-05-13T20:44:05.787 回答
0

对于 GridView 中的每个对象,您需要找到它并确定它的值。例如,

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

然后,确定 ddlToLoc 的 SelectedValue 并插入数据库

于 2013-05-13T20:46:36.090 回答
0

您可以调用: command.ExecuteNonQuery将数据插入数据库。

更多信息在这里

于 2013-05-13T20:43:16.010 回答
0

试试这个方法

 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
于 2018-12-18T09:09:51.210 回答