2013-05-13 47 views
1

問題:VB.NET插入DataGridView的內容複製到數據庫

我需要我的DataGridView的內容轉儲到一個SQL Server數據庫表。我已經加載了datagridview,沒有問題。我對VB.NET不夠熟悉,無法理解如何將這些數據存入數據庫表。

代碼:(到目前爲止)

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 

缺少什麼我在這裏?什麼都沒有插入到我的表中。任何幫助,將不勝感激。就像我說的,我對VB不是很熟悉,所以對我來說很簡單。

+0

'SelectCommand'和'InsertCommand'牽住了選擇的文本,插入查詢/命令,分別。您應該將'command'設置爲'dataAdapter'的'InsertCommand',然後執行該命令(不確定SQL Server如何工作)。 – 2013-05-13 20:42:32

+0

谷歌你的問題的標題是我的答案。 – RandomUs1r 2013-05-13 20:54:57

回答

5

那麼,你需要執行命令,不能簡單地添加到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

' 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) 
一個SqlCommandBuilder的那一刻使用的適配器
+0

史蒂夫,你是最棒的!非常感謝你的幫助。你的建議和方向正是我所需要的。 – 2013-05-13 21:19:20

0

您可以撥打:command.ExecuteNonQuery將數據插入數據庫。

更多信息here

0

對於GridView中的每個對象,都需要找到它並確定它的值。因此,例如,

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

然後,確定ddlToLoc的的SelectedValue,並插入到數據庫

相關問題