vb.net 错误“ExecuteReader:连接属性尚未初始化。”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15595633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:57:35  来源:igfitidea点击:

Error "ExecuteReader: connection property has not been initialized."

asp.netvb.netoledb

提问by JStew

Here's my code. I can't seem to figure out what's causing this error. The error itself pointed to the Do While myReader.readline of code, but I'm not sure that's what's causing the problem.

这是我的代码。我似乎无法弄清楚是什么导致了这个错误。错误本身指向Do While myReader.read代码行,但我不确定这是导致问题的原因。

Here is a more detailed error message:

这是更详细的错误消息:

executereader: connection property has not been initialized. at system.data.oledb.oledbcommand.validateconnection(string method) at system.data.oledb.oledbcommand.validateconnectionandtransaction(string method)

executereader:连接属性尚未初始化。在 system.data.oledb.oledbcommand.validateconnection(字符串方法) 在 system.data.oledb.oledbcommand.validateconnectionandtransaction(字符串方法)

Code:

代码:

Imports System.Data.OleDb
Imports System.Data

Partial Class Customer_6_OrderHistory
    Inherits System.Web.UI.Page

    Private myDB As OleDbConnection
    Private sqlCmd As OleDbCommand
    Private myReader As OleDbDataReader
    Private myConnection As String = ConfigurationManager.ConnectionStrings("ConnString10").ToString
    Private myDataFile As String = ConfigurationSettings.AppSettings("DBFile")

    Private LegoNameList As New ArrayList
    Private ShipmentDateList As New ArrayList
    Private CostList As New ArrayList
    Private NumberPurchasedList As New ArrayList
    Private RecipientList As New ArrayList
    Private TotalCostList As New ArrayList

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        myDB = New OleDbConnection(myConnection)

        sqlCmd = New OleDbCommand("exec qry_OrderHistory")
        sqlCmd.Parameters.AddWithValue("@CustomerName", Session("myCart").GetCustomerName())

        Try
            myDB.Open()
            myReader = sqlCmd.ExecuteReader
            Do While myReader.Read
                LegoNameList.Add(myReader("Lego Name"))
                ShipmentDateList.Add(myReader("Date"))
                CostList.Add(myReader("Cost"))
                NumberPurchasedList.Add(myReader("Number Purchased"))
                RecipientList.Add(myReader("Recipient"))
                TotalCostList.Add(myReader("Total Cost"))
            Loop
            myReader.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        Finally
            myDB.Close()
        End Try

        Dim myDataTable As New DataTable
        myDataTable.Columns.Add("Lego Name")
        myDataTable.Columns.Add("Date")
        myDataTable.Columns.Add("Cost")
        myDataTable.Columns.Add("Number Purchased")
        myDataTable.Columns.Add("Recipient")
        myDataTable.Columns.Add("Total Cost")

        For i = 0 To LegoNameList.Count - 1
            Dim myRow As DataRow = myDataTable.NewRow
            myRow.Item("Lego Name") = LegoNameList.Item(i)
            myRow.Item("Date") = FormatDateTime(ShipmentDateList.Item(i), DateFormat.ShortDate)
            myRow.Item("Cost") = FormatCurrency(CostList.Item(i), 2, TriState.True, TriState.False, TriState.True)
            myRow.Item("Number Purchased") = NumberPurchasedList.Item(i)
            myRow.Item("Recipient") = RecipientList.Item(i)
            myRow.Item("Total Cost") = FormatCurrency(TotalCostList.Item(i), 2, TriState.True, TriState.False, TriState.True)
            myDataTable.Rows.Add(myRow)
        Next
        gvwOrderHistory.DataSource = myDataTable
        gvwOrderHistory.DataBind()
    End Sub
End Class

回答by Tim

You have created your connection, and then you create your command, but you never assign the connection to your command, which is why you're getting the error. You can either assign the connection to your OleDbCommand, like this:

您已经创建了连接,然后创建了命令,但是您从未将连接分配给您的命令,这就是您收到错误的原因。您可以将连接分配给您的OleDbCommand,如下所示:

sqlCmd.Connection = myDB

Or you can specify in the OleDbCommandconstructor, like this:

或者您可以在OleDbCommand构造函数中指定,如下所示:

sqlCmd = New OleDbCommand("exec qry_OrderHistory", myDB)

Then you can open the connection and execute the command.

然后就可以打开连接并执行命令了。

回答by Mido

You have to initialize your Connective instance myConnectionbefore use it

你必须myConnection在使用它之前初始化你的 Connective 实例