system.invalidoperationexception:填充:selectCommand.Connection属性尚未初始化

时间:2014-02-09 06:03:00

标签: visual-studio ms-access database-connection

我正在使用Visual Basic连接MS Access数据库。不幸的是,我无法将我的数据库中的记录链接到visual basic。

我的数据库位于C:\ Users \ lenovo \ Desktop \ GUI references \ WindowsApplication1 \ WindowsApplication1 \ bin \ Debug

数据库名称为 smsenabler.mdb

要连接的表是 ProfessorListTable

该表包含

字段
  

ID |姓氏| FirstName | MI |部门|就业年份

我表格上的代码如下所示:

    Imports System.Data.OleDb

Public Class ProfessorList
    Dim con As OleDbConnection

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

    End Sub

    Private Sub ProfessorList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim con As OleDbConnection = New OleDbConnection
            con.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = ..\smsenabler.mdb"
            con.Open()

            showRecords()
            con.Close()
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub
    Public Sub showRecords()
        Dim dt As New DataTable
        Dim ds As New DataSet
        ds.Tables.Add(dt)

        Dim da As New OleDbDataAdapter("Select * from ProfessorListTable", con)
        da.Fill(dt)

        Dim myRow As DataRow

        For Each myRow In dt.Rows
            ListView1.Items.Add(myRow.Item(0))
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myRow.Item(1))
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myRow.Item(2))
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myRow.Item(3))
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myRow.Item(4))
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myRow.Item(5))
        Next

    End Sub
  

错误消息

system.invalidoperationexception:填充:selectCommand.Connection属性尚未初始化。在System.Data.Common.DbDataAdapter.GetConnection3(DbDataAdapter适配器,IDbCommand命令String方法)System.Data.Common.DbDataAdapter.FillInternal(DataSet数据集,DataTable [] datatables,Int32 startRecord,Int32 maxRecords,String srcTable,IDbCommand命令, System.Data.Common.DbDataAdapter.Fill(DataTable [] dataTables,Int32 startRecord,Int32 maxRecords,IDbCommand命令,CommandBehavior行为)在THESIS_GUI.ProfessorList的System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)处的CommandBehavior行为。 C:\ Users \ lenovo \ Desktop \ GUI中的ProfessorList_Load(Object sender,EventArgs e)引用\ WindowsApplication1 \ WindowsApplication1 \ ProfessorList.vb:line17

第17行

  

showRecords()

它给了我一个根本没有记录的输出。它只显示一个带有写在其上的字段的GUI。谢谢..

1 个答案:

答案 0 :(得分:0)

一个建议是确保您的连接('con')可用于所有子例程。我之前使用过全局变量来防止打开和关闭连接的需要(这会导致过去出现问题)。我们都采用了类似的方法,但是这里有一些代码我已经剥离了(删除了错误陷阱,调试辅助工具等):

Global cnLocalData  As ADODB.Connection
Public Sub Get_Connection()
    Set cnLocalData = New ADODB.Connection
    With cnLocalData
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .Properties("Data Source") = "C:\data\SomeDB.mdb"
        .Open
    End With
End Sub