代码错误 - dbCommand.ExecuteReader()

发布于 2024-11-19 13:30:30 字数 1410 浏览 2 评论 0原文

谁能告诉我如何解决这个错误。 错误:NullReferenceException 未处理 - “未将对象引用设置为对象的实例。” 行: Dim reader As OleDbDataReader = dbCommand.ExecuteReader() 数据库:MS Access IDE:VB 2010 Express

Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System


Public Class Form1       
Dim dbConnection As OleDbConnection        
Dim dbCommand As OleDbCommand       
Dim strInsert As String       
Dim dbDataAdapter As OleDbDataAdapter        
Dim ConnectString As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & "Data Source =atg.mdb"

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

 dbConnection = New OleDbConnection(ConnectString) 'here I use the new keyword to initialize connection  
 dbConnection.Open()
 Dim command As New OleDbCommand("SELECT XX FROM ABC", dbConnection) ' initialize command and pass query and open connection to its constructor  
 Dim reader As OleDbDataReader = dbCommand.ExecuteReader() 'ERROR IS HERE
 Do While (reader.Read()) ' loop here until reader finish reading and add every row to the list box  
        ListBox1.BeginUpdate()
        ListBox1.Items.Add(reader.Item("ABC"))             
        ListBox1.EndUpdate()
    Loop
    reader.Close()
    dbConnection.Close()
End Sub
End Class

Can anyone tell me How do I solve this error.
Error: NullReferenceException was Unhandled - "Object reference not set to an instance of an object."
Line: Dim reader As OleDbDataReader = dbCommand.ExecuteReader()
Database: MS Access
IDE: VB 2010 Express

Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System


Public Class Form1       
Dim dbConnection As OleDbConnection        
Dim dbCommand As OleDbCommand       
Dim strInsert As String       
Dim dbDataAdapter As OleDbDataAdapter        
Dim ConnectString As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & "Data Source =atg.mdb"

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

 dbConnection = New OleDbConnection(ConnectString) 'here I use the new keyword to initialize connection  
 dbConnection.Open()
 Dim command As New OleDbCommand("SELECT XX FROM ABC", dbConnection) ' initialize command and pass query and open connection to its constructor  
 Dim reader As OleDbDataReader = dbCommand.ExecuteReader() 'ERROR IS HERE
 Do While (reader.Read()) ' loop here until reader finish reading and add every row to the list box  
        ListBox1.BeginUpdate()
        ListBox1.Items.Add(reader.Item("ABC"))             
        ListBox1.EndUpdate()
    Loop
    reader.Close()
    dbConnection.Close()
End Sub
End Class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薔薇婲 2024-11-26 13:30:30

在 dbCommand.ExecuteReader() 语句中找到 dbCommand 的位置。请改用 command.ExecuteReader() 。

您没有启动 dbCommand。您只是声明了它。您所做的是创建另一个名为 command 的变量并启动它。所以尝试使用这个

Where you find dbCommand in dbCommand.ExecuteReader() statement.Use command.ExecuteReader() instead .

You didn't initiate your dbCommand.You just Decalared it.What you did is creating another variable called command and initiated it.So try with this

中二柚 2024-11-26 13:30:30

程序的一部分:

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'MajDgv()
        Module1.connect()
        Me.fillcombo()
End Sub
Sub fillcombo()

        strsql = "select from Liste des responsables"
        Dim acscmd As New OleDb.OleDbCommand
        Dim acsdr As OleDbDataReader
        acscmd.CommandText = strsql
        acsdr = acscmd.ExecuteReader()""THE PROBLEM IS HERRE he sayed that ExecuteReader: Connection property has not been initialized."""""""
        While (acsdr.Read())
            ComboBox1.Items.Add(acsdr("Nom"))

        End While
        acscmd.Dispose()
        acsdr.Close()

    End Sub
End Class

我创建一个模块:

Imports System.Data.OleDb
Module Module1
    Public acsconn As New OleDb.OleDbConnection
    Public acsdr As OleDbDataReader
    Public strsql As String
    Sub connect()
        acsconn.ConnectionString = "Provider=Microsoft.ace.oledb.12.0;data source=|datadirectory|\Base1.accdb;"
        acsconn.Open()
    End Sub
End Module

Part of the programma:

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'MajDgv()
        Module1.connect()
        Me.fillcombo()
End Sub
Sub fillcombo()

        strsql = "select from Liste des responsables"
        Dim acscmd As New OleDb.OleDbCommand
        Dim acsdr As OleDbDataReader
        acscmd.CommandText = strsql
        acsdr = acscmd.ExecuteReader()""THE PROBLEM IS HERRE he sayed that ExecuteReader: Connection property has not been initialized."""""""
        While (acsdr.Read())
            ComboBox1.Items.Add(acsdr("Nom"))

        End While
        acscmd.Dispose()
        acsdr.Close()

    End Sub
End Class

I create a Module:

Imports System.Data.OleDb
Module Module1
    Public acsconn As New OleDb.OleDbConnection
    Public acsdr As OleDbDataReader
    Public strsql As String
    Sub connect()
        acsconn.ConnectionString = "Provider=Microsoft.ace.oledb.12.0;data source=|datadirectory|\Base1.accdb;"
        acsconn.Open()
    End Sub
End Module
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文