使用OleDb.OleDb ??? vb代码ExecuteReader:尚未初始化Connection属性

时间:2014-11-27 11:50:36

标签: database vb.net datagridview oledb

我正在尝试使用我的数据库的信息加载我的datagridview调用dgvInfo。我对vb很陌生,我现在不明白我的代码中的问题所在。

Private Sub frmProductInformation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim conn As New OleDb.OleDbConnection
    Dim dt As New DataTable
    Dim cmd As New OleDb.OleDbCommand()
    Dim readit As OleDb.OleDbDataReader
    conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=N:\data\GameHQ.accdb"
    conn.Open()
    readit = cmd.ExecuteReader()
    readit.Read()
    dt.Load(readit)
    dgvInfo.DataSource = dt
    dgvInfo.Refresh()
    conn.Close()
End Sub

这是我的代码,我希望有人能够指出我是否错过了什么。

1 个答案:

答案 0 :(得分:0)

您必须正确初始化cmd。假设您不使用交易,只需将您的查询和连接放到您的cmd即可。顺便说一下 - 你还没有指定任何SQL查询:

Dim conn As New OleDb.OleDbConnection
Dim dt As New DataTable
Dim readit As OleDb.OleDbDataReader
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=N:\data\GameHQ.accdb"
conn.Open()
Dim cmd As New OleDb.OleDbCommand("select * from <whatever you select from>", conn)
readit = cmd.ExecuteReader()
readit.Read()
dt.Load(readit)
dgvInfo.DataSource = dt
dgvInfo.Refresh()
conn.Close()