oledbCnn未连接到Access数据库

时间:2014-05-16 13:42:09

标签: vb.net

我正在尝试连接数据库。当我做一个简单的Select *它工作,但是当我添加一个WHERE子句它不再正常工作并说它无法连接。列名是正确的,我确信数据库中有Lee的姓氏。为什么这会在一个简单的选择期间工作,而不是在有where子句时?

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim connetionString As String
    Dim oledbCnn As OleDbConnection
    Dim oledbAdapter As OleDbDataAdapter
    Dim ds As New DataSet
    Dim sql As String
    Dim i As Integer

    connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=S:\Reporting Database.mdb;"
    sql = "SELECT * FROM [extract1] WHERE [extract1].[PI First Name] = Lee"
    oledbCnn = New OleDbConnection(connetionString)
    Try
        oledbCnn.Open()
        oledbAdapter = New OleDbDataAdapter(sql, oledbCnn)
        oledbAdapter.Fill(ds)
        DataGridView1.DataSource = ds.Tables(0)
        oledbAdapter.Dispose()
        oledbCnn.Close()
    Catch ex As Exception
        MsgBox("Can not open connection ! ")
    End Try
End Sub

1 个答案:

答案 0 :(得分:1)

我想你应该在引号之间添加Lee,例如:

 sql = "SELECT * FROM [extract1] WHERE [extract1].[PI First Name] = 'Lee'"

提示:使用parameters来避免sql注入。