使用VB.NET操作Microsoft Access数据库

时间:2016-08-30 12:33:02

标签: vb.net ms-access

我该如何做到这一点?

Private Sub ListView_MouseClick(sender As Object, e As MouseEventArgs) Handles ListView.MouseClick

    conndb = New OleDbConnection
    conndb.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Database1.accdb"
    Try
        conndb.Open()
        Dim str As String
        str = "Select * FROM customer WHERE CustomerID = '" & ListView.FocusedItem.Text & "'"
        COMMAND = New OleDbCommand(str, conndb)
        dr = COMMAND.ExecuteReader

        If dr.Read = True Then
            txtID.Text = dr("CustomerID")
            txtFirstName.Text = dr("FirstName")
            txtSurname.Text = dr("Surname")
            txtAddress.Text = dr("Address")
            txtCN1.Text = dr("ContactNo1")
            txtCN2.Text = dr("ContactNo2")
            txtEmail.Text = dr("EmailAddress")
            txtRemarks.Text = dr("Remarks")
            txtDebtStatus.Text = dr("DebtStatus")
            txtDownPay.Text = dr("DownPayment")
            txtDebtBal.Text = dr("DebtBal")
            txtCustomerDate.Text = dr("Date")
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        conndb.Dispose()
    End Try
End Sub

我需要帮助我如何使这个运行没有错误,我使用ms访问作为我的数据库源。使用此代码似乎有一个错误,这段代码与mysql完全正常,但在ms访问时,它表示数据错误类型错误或类似的东西。需要你的帮助,谢谢

2 个答案:

答案 0 :(得分:3)

删除查询中字段CustomerID周围的'

str = "Select * FROM customer WHERE CustomerID = '" & ListView.FocusedItem.Text & "'"

成为:

str = "Select * FROM customer WHERE CustomerID = " & ListView.FocusedItem.Text

当您放入撇号时,MS Access会看到一个字符串,因此存在类型不匹配异常,因为它需要一个数字......

然而,这是一个非常糟糕的主意,因为参数化查询是一种更好的方法(参见:Why should I create Parametrized Queries ?

另外,使用Using

总而言之,它只是墙上的另一块砖:

Private Sub ListView_MouseClick(sender As Object, e As MouseEventArgs) Handles ListView.MouseClick

Using conndb As New OleDbConnection
    conndb.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Database1.accdb"
    Try
        conndb.Open()
        Dim str As String
        str = "Select * FROM customer WHERE CustomerID = @Customer"
        Using COMMAND As New OleDbCommand(str, conndb)
            COMMAND.Parameters.Add("@Customer", SqlDbType.Integer).Value = Integer.Parse(ListView.FocusedItem.Text)
            dr = COMMAND.ExecuteReader

            If dr.Read = True Then
                txtID.Text = dr("CustomerID")
                txtFirstName.Text = dr("FirstName")
                txtSurname.Text = dr("Surname")
                txtAddress.Text = dr("Address")
                txtCN1.Text = dr("ContactNo1")
                txtCN2.Text = dr("ContactNo2")
                txtEmail.Text = dr("EmailAddress")
                txtRemarks.Text = dr("Remarks")
                txtDebtStatus.Text = dr("DebtStatus")
                txtDownPay.Text = dr("DownPayment")
                txtDebtBal.Text = dr("DebtBal")
                txtCustomerDate.Text = dr("Date")
            End If
        End Using
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Using
End Sub

答案 1 :(得分:0)

看一下我放在一起的这个示例代码。你可以从中学到很多东西。

> $PATH_TO_TOMCAT/bin/tomcat6.exe //TS//Tomcat6 ++JvmOptions -Duser.language=en;-Duser.region=US