代码错误 - dbCommand.ExecuteReader()
谁能告诉我如何解决这个错误。 错误: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 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
程序的一部分:
我创建一个模块:
Part of the programma:
I create a Module: