从vb.net DataGridView更新Access数据库

时间:2017-03-22 09:53:53

标签: database vb.net datagridview access

所以基本上,我遇到的问题是我更改了值很好,当单击修改按钮时没有错误并更新显示的DataGridView罚款。但是它实际上并没有更新Access数据库。意味着如果程序被关闭或者其他什么并且重新打开,则返回到程序从数据库获取的原始值。显然没有更新。

    Dim dsConnectionM As OleDb.OleDbConnection
    Dim dsConnectionL As OleDb.OleDbConnection
    Dim dsConnectionE As OleDb.OleDbConnection
    Dim dsDataAdapterM As OleDbDataAdapter
    Dim dsDataAdapterL As OleDbDataAdapter
    Dim dsDataAdapterE As OleDbDataAdapter
    Dim dsDataSetM As DataSet
    Dim dsDataSetL As DataSet
    Dim dsDataSetE As DataSet

    Public Sub dsLoadMembers()
        dsConnectionM = New OleDbConnection
        dsConnectionM.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data source=DataSourceDB.accdb"
        dsDataAdapterM = New OleDbDataAdapter
        dsDataAdapterM.SelectCommand = New OleDbCommand
        dsDataAdapterM.SelectCommand.Connection = dsConnectionM
        dsDataAdapterM.SelectCommand.CommandText = "SELECT * FROM Member"
        dsDataAdapterM.SelectCommand.CommandType = CommandType.Text
        dsConnectionM.Open()
        dsDataSetM = New DataSet
        dsDataAdapterM.Fill(dsDataSetM, "dataSetMembers")
        dsConnectionM.Close()
        Form3.dgdMembers.AutoGenerateColumns = True
        Form3.dgdMembers.DataSource = dsDataSetM
        Form3.dgdMembers.DataMember = "dataSetMembers"
    End Sub

    Public Sub AmendMember()
        Form3.dgdMembers.Rows(Form3.m).Cells("MemberID").Value = Form3.tbid.Text
        Form3.dgdMembers.Rows(Form3.m).Cells("Forename").Value = Form3.tbfn.Text
        Form3.dgdMembers.Rows(Form3.m).Cells("Surname").Value = Form3.tbsn.Text
        Form3.dgdMembers.Rows(Form3.m).Cells("DOB").Value = Form3.dtpdob.Value
        Form3.dgdMembers.Rows(Form3.m).Cells("Section").Value = Form3.tbsr.Text
        Form3.dgdMembers.Rows(Form3.m).Cells("Postcode").Value = Form3.tbpc.Text
        Form3.dgdMembers.Rows(Form3.m).Cells("HomeTel").Value = Form3.tbht.Text
        Form3.dgdMembers.Rows(Form3.m).Cells("MobileTel").Value = Form3.tbmt.Text
        Form3.dgdMembers.Rows(Form3.m).Cells("AddressLine1").Value = Form3.tbal1.Text
        Form3.dgdMembers.Rows(Form3.m).Cells("AddressLine2").Value = Form3.tbal2.Text
        Form3.dgdMembers.Rows(Form3.m).Cells("City").Value = Form3.tbc.Text
        Form3.dgdMembers.Rows(Form3.m).Cells("EmailAddress").Value = Form3.tbea.Text
        Form3.dgdMembers.Rows(Form3.m).Cells("P/GForename").Value = Form3.tbpgfn.Text
        Form3.dgdMembers.Rows(Form3.m).Cells("P/GSurname").Value = Form3.tbpgsn.Text
        dsConnectionM.Open()
        dsDataAdapterM.Update(dsDataSetM, "dataSetMembers")
        dsDataSetM.AcceptChanges()
        dsConnectionM.Close()
    End Sub

1 个答案:

答案 0 :(得分:0)

首先,检查您的数据源是否是数据库的完整路径。 此外,还有更简单的方法来更新数据库;您可以使用UPDATE语句来执行此操作。 在我的公共课中,我声明了以下内容:

Dim provider As String
Dim datafile As String
Dim connString As String
Dim myconnection As OleDbConnection = New OleDbConnection

现在为代码的实际例子:

myconnection.open()
dim variable as string
variable = "UPDATE tablename SET columnname = value WHERE (enter criteria )"
Dim cmd as oledbcommand = New oledbcommand(variable, myconnection)
cmd.ExecuteNonQuery()
myconnection.close()

所以对于你的代码:

Public Sub AmendMember()
    myconnection.open()
    variable = "UPDATE dataSetMembers SET MemberID = '" & Form3.tbid.text &"', Forename = '" & Form3.tbfn.Text &"', Surname = '" & Form3.tbsn.Text &"', DOB = '" & Form3.dtpdob.text &"', Section = '" & Form3.tbsr.Text &"', Postcode = '" & Form3.tbpc.Text &"', HomeTel = '" & Form3.tbht.Text &"', MobileTel = '" & Form3.tbmt.Text &"', AddressLine1 = '" & Form3.tbal1.Text &"', AddressLine2 = '" & Form3.tbal2.Text &"', City = '" & Form3.tbc.Text &"', EmailAddress = '" & Form3.tbea.Text &"', P/GForename = '" & Form3.tbpgfn.Text &"', P/GSurname = '" & Form3.tbpgsn.Text &" "
    Dim cmd as oledbcommand = New oledbcommand(variable, myconnection)
    cmd.ExecuteNonQuery()
    myconnection.close()
End Sub

在此我从你之前的代码中假设了你的表名。