如何使用datagridview更新数据库?

时间:2012-06-26 03:12:38

标签: datagridview vb.net-2010

我们如何使用datagridview更新表?假设datagridview在运行时是可编辑的?任何形式的帮助表示赞赏。

 Dim con As New OleDb.OleDbConnection
    Dim dbProvider As String
    Dim dbSource As String
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
        dbSource = "Data Source = C:\record.accdb"

        con.ConnectionString = dbProvider & dbSource

        con.Open()

        sql = "SELECT * FROM StudentRecords"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "AddressBook")
        MsgBox("Database is now open")

        con.Close()

        MsgBox("Database is now Closed")

        DataGridView1.DataSource = ds
        DataGridView1.DataMember = "AddressBook"
    End Sub

1 个答案:

答案 0 :(得分:2)

请按以下步骤操作:

  1. 使用For Each或简单的For循环
  2. 迭代DataGridView的Rows集合
  3. 编写UPDATE语句以保存更改。