OleDb连接字符串

时间:2016-02-17 09:44:58

标签: vb.net database-connection

我使用一个简单的小函数从数据库中读取数据,并在datagridview中显示数据。

Function ReadFromDataBase(query As String, Table As String, pName As String)

    Try
        Conn = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & pName & ";Persist Security Info=False;")

        Try
            Call Conn.Open()
        Catch ex As Exception
            MsgBox("Could not connect")
        End Try

        If Conn.State = ConnectionState.Open Then
            Dim SQL As New OleDb.OleDbCommand(query, Conn)
            Dim DataAdapter As New OleDb.OleDbDataAdapter(SQL)
            Dim DataTable As New DataTable(Table)
            DataAdapter.Fill(DataTable)
            Return DataTable
        End If

    Catch ex As Exception
        Return ex.ToString
    End Try

End Function

这段代码完全正常工作,直到我把它带回家,我在调用函数之前更改了数据源(pName)。我在这里调用这个函数:

Private Sub IpLoad() Handles MyBase.Load
    Dim query, table, pName As String
    query = "SELECT * FROM CONFIG_MonitoredServers WHERE sName = 'C-1' OR sName = 'C-2' OR sName = 'C-3'"
    table = "CONFIG_MonitoredServers"
    pName = "F:\!Computing\!Versions\service.accdb"
    DataGridView2.DataSource = (Helpers.ReadFromDataBase(query, table, pName))
    query = "SELECT * FROM CONFIG_MonitoredServers WHERE sName  <>'C-1' AND sName <> 'C-2' AND sName <> 'C-3'"
    DataGridView1.DataSource = (Helpers.ReadFromDataBase(query, table, query))
    LockDownDataGridView()
End Sub

我不知道我做错了什么,所以任何可以提供的帮助都会非常有帮助。 我没有得到任何实际错误,只是消息框说无法连接,当我删除try catch和消息框时,我根本什么都没有,表单加载空白数据网格视图。

1 个答案:

答案 0 :(得分:0)

可能听起来很愚蠢,但是开放的Access。有时候会出现'#34;'&#34;打开OleDB直到完成的安装对话框。