Access数据库不会更新

时间:2018-03-03 16:04:42

标签: vb.net

这是我目前的代码,我刚刚开始编码,所以我不确定这里问的格式,请原谅我。

我的问题是每次我尝试点击更新"记录成功更新"消息框将显示但数据不会更新。我不确定要改变什么,因为我已经确定一切都是正确的。  我还尝试在"'中添加实际的ID号码。其中CustomerID =" &安培; myid& " "它会更新,但我需要的是它不再需要它了。

打扰一下,如果我没有解释得很好,请告诉我是否有需要解释的内容。 [表格]:https://imgur.com/IKFO5jh

  Imports System.Data.OleDb

  Public Class Form3

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    Dim i As Integer
    Dim myid As Integer
    With DataGridView1
        If e.RowIndex >= 0 Then
            i = .CurrentRow.Index
            myid = .Rows(i).Cells("CustomerID").Value.ToString
            TextBox2.Text = .Rows(i).Cells("CustomerName").Value.ToString
            TextBox3.Text = .Rows(i).Cells("Address").Value.ToString
            TextBox4.Text = .Rows(i).Cells("Age").Value.ToString
            TextBox5.Text = .Rows(i).Cells("Gender").Value.ToString
            TextBox6.Text = .Rows(i).Cells("Birthday").Value.ToString
            TextBox7.Text = .Rows(i).Cells("CivilStatus").Value.ToString
            TextBox8.Text = .Rows(i).Cells("Religion").Value.ToString
            TextBox9.Text = .Rows(i).Cells("Citizenship").Value.ToString
            TextBox10.Text = .Rows(i).Cells("ContactNo").Value.ToString
        End If
    End With
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Try
        Dim conn As New OleDb.OleDbConnection
        Dim squery As New OleDb.OleDbCommand
        Dim connStr As String
        Dim myid As Integer
        connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\user\Desktop\temp2\Valenzuela10A\Valenzuela10A.mdb"
        conn.ConnectionString = connStr
        squery.Connection = conn
        conn.Open()
        squery.CommandText = "Update Customer set CustomerName='" & TextBox2.Text & "',Address='" & TextBox3.Text & "',Age='" & CInt(TextBox4.Text) & "',Gender='" & TextBox5.Text & "',Birthday='" & TextBox6.Text & "',CivilStatus='" & TextBox7.Text & "',Religion='" & TextBox8.Text & "',Citizenship='" & TextBox9.Text & "',ContactNo='" & TextBox10.Text & "' where CustomerID=" & myid & " "
        squery.ExecuteNonQuery()
        squery.Dispose()
        MsgBox("Record successfully updated.", MsgBoxStyle.Information, "Message")
        load_data()
        conn.Close()

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    load_data()
End Sub
Public Sub load_data()
    Try
        Dim conn As New OleDb.OleDbConnection
        Dim ds As New DataSet
        Dim dt As New DataTable
        Dim da As New OleDb.OleDbDataAdapter
        conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\user\Desktop\temp2\Valenzuela10A\Valenzuela10A.mdb"
        conn.Open()
        ds.Tables.Add(dt)
        da = New OleDbDataAdapter("Select * from Customer", conn)
        da.Fill(dt)
        DataGridView1.DataSource = dt
        conn.Close()

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub
End Class

1 个答案:

答案 0 :(得分:0)

如果要将DataGrid绑定到Access表,请这样做..

Imports System.Data.OleDb
Public Class Form1
    Dim connetionString As String
    Dim connection As OleDbConnection
    Dim oledbAdapter As OleDbDataAdapter
    Dim oledbCmdBuilder As OleDbCommandBuilder
    Dim ds As New DataSet
    Dim changes As DataSet
    Dim i As Integer
    Dim sql As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;"
        connection = New OleDbConnection(connetionString)
        Sql = "select * from tblUsers"
        Try
            connection.Open()
            oledbAdapter = New OleDbDataAdapter(Sql, connection)
            oledbAdapter.Fill(ds)
            DataGridView1.Data Source= ds.Tables(0)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            oledbCmdBuilder = New OleDbCommandBuilder(oledbAdapter)
            changes = ds.GetChanges()
            If changes IsNot Nothing Then
                oledbAdapter.Update(ds.Tables(0))
            End If
            ds.AcceptChanges()
            MsgBox("Save changes")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class

http://vb.net-informations.com/dataadapter/dataadapter-datagridview-oledb.htm

还有其他几种方法可以做到这一点。我会留给你探索其他选择。