使用DataAdapter通过对VB.NET中的DataGridView所做的更改来更新SQL Server

时间:2013-12-02 21:44:23

标签: sql-server vb.net datagridview

我正在尝试创建一个表单,使用DataAdapter(代码中)而不是TableAdapter(拖放)来编辑SQL Server表中的数据。我在表单加载并填充datagridview时定义了连接,数据集,dataapapter和datatable,但是我无法使用按钮触发Update,因为DataAdapter在load事件完成后到期。使用TableAdapter,它以某种方式持久化,以便我可以在按钮的代码中引用它。我怎么能这样做?

Imports System.Data.SqlClient
Public Class frmGroceries2


Private Sub frmGroceries2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim oCon As New SqlConnection
    oCon.ConnectionString = "Data Source=IPS-03042013\sqlexpress;Initial Catalog=SqlToVbExamples;Integrated Security=True"
    Dim dsSupplies As New DataSet
    Dim daLocalGroceries As New SqlDataAdapter("SELECT * FROM GROCERIES", oCon)
    Dim tblLocalGroceries As DataTable
    Try
        daLocalGroceries.FillSchema(dsSupplies, SchemaType.Source, "LocalGroceries")
        daLocalGroceries.Fill(dsSupplies, "LocalGroceries")
        tblLocalGroceries = dsSupplies.Tables("LocalGroceries")
        dgvLocalGroceries.DataSource = tblLocalGroceries
    Catch ex As Exception
        MsgBox("Something has gone wrong..." & vbNewLine & ex.Message)

    End Try



End Sub

Private Sub cmdSaveChanges_Click(sender As Object, e As EventArgs) Handles cmdSaveChanges.Click
    'I want to put the update method in here, but can't


End Sub
End Class

1 个答案:

答案 0 :(得分:2)

您是对的,因为对象是在子中定义的,所以不会保存数据。您可以使用表单级别定义来克服此问题。我在下面提供了一个实际可行的示例。

Imports System.Data.SqlClient

Public Class Form1
'*** Define form level variables so that they are visible from other methods
    Dim tblLocalGroceries As DataTable
    Dim daLocalGroceries As SqlDataAdapter
    Dim dsSupplies As New DataSet
    Dim oCon As SqlConnection

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        oCon = New SqlConnection
        oCon.ConnectionString = "Data Source=IPS-03042013\sqlexpress;Initial Catalog=SqlToVbExamples;Integrated Security=True"
        dsSupplies = New DataSet
        daLocalGroceries = New SqlDataAdapter("SELECT * FROM GROCERIES", oCon)
'*** Define command builder to generate the necessary SQL
        Dim builder As SqlCommandBuilder = New SqlCommandBuilder(daLocalGroceries)
        builder.QuotePrefix = "["
        builder.QuoteSuffix = "]"

        Try
            daLocalGroceries.FillSchema(dsSupplies, SchemaType.Source, "LocalGroceries")
            daLocalGroceries.Fill(dsSupplies, "LocalGroceries")
            tblLocalGroceries = dsSupplies.Tables("LocalGroceries")
            dgvLocalGroceries.DataSource = tblLocalGroceries
        Catch ex As Exception
            MsgBox("Something has gone wrong..." & vbNewLine & ex.Message)

        End Try

    End Sub

    Private Sub pbUpdate_Click(sender As System.Object, e As System.EventArgs) Handles pbUpdate.Click

        '*** Sub responds to event of button 'update' is clicked. It is intended to reflect
        '*** grid changes back to db

        Dim tblChanges As DataTable = tblLocalGroceries.GetChanges()
        Try
            If Not (tblChanges Is Nothing) Then
                daLocalGroceries.Update(tblChanges)
            End If
        Catch ex As Exception
            MsgBox("Something has gone wrong..." & vbNewLine & ex.Message)

        End Try

    End Sub 
End Class