我收到以下错误:
已经有一个与此命令关联的打开DataReader,必须先关闭它。
异常详细信息:System.InvalidOperationException:已经有一个与此命令关联的打开的DataReader,必须先关闭它。
到目前为止,这是我的代码:
Public Function GetPrice() As Decimal
Dim aParams(1) As String
Dim dPrice As Decimal
aParams(0) = Me.ID
aParams(1) = Me.Date
With GetDb().GetRsWithSqlId("GET_PRICE", aParams)
If .Read() Then
dPrice = Convert.ToDecimal(IIf(TypeOf .Item("TS_PRICE") Is DBNull, 0.0, .Item("TS_PRICE")))
End If
.Close()
End With
Return dPrice
End Function
Public Function GetRsWithSqlId(ByVal sId As String, ByVal arrParams() As Object) As OleDbDataReader
Dim i As Integer
If m_oOleDbConnection.State <> ConnectionState.Open Then
If Not Connect() Then Throw New ArgumentException("Can't connect to database!" & Chr(13) & "LastError: " & LastError())
End If
m_oOleDbCommand.Parameters.Clear()
m_oOleDbCommand.CommandType = CommandType.Text
m_oOleDbCommand.CommandText = GetSql(sId)
If Not IsNothing(m_oOleDbTransaction) Then
m_oOleDbCommand.Transaction = m_oOleDbTransaction
End If
For i = 0 To arrParams.GetLength(0) - 1
m_oOleDbCommand.Parameters.Add(New OleDbParameter("?", arrParams(i)))
Next
Return m_oOleDbCommand.ExecuteReader()
End Function
错误消息表明DataReader已经在同一连接上使用另一个命令,但我无法弄清楚阅读器的打开位置。 是否有解决方案关闭DataReader,如果它是打开的或避免使用DataReader?我的理解是,如果没有DataReader,可以在同一个连接上同时使用多个命令,或者?