大家好,希望有人能帮助我。
我有一个 vb.net 控制台应用程序,最近开始让我头疼。
我有下一个代码:
Imports System.Data.SqlClient
Dim SqlQuery = New StringBuilder
Dim LstResults As New List(Of StoreDate)
SqlQuery.Append("searh something ")
Using Conn As New SqlConnection(CONSTR)
Conn.Open()
Using Cmd As New SqlCommand(SqlQuery.ToString, Conn)
Cmd.Parameters.AddWithValue("@param1", Param1)
Cmd.Parameters.AddWithValue("@param2", Param2)
Cmd.Parameters.AddWithValue("@Param3", Param3)
Cmd.CommandTimeout = 0
Using Reader As SqlDataReader = Cmd.ExecuteReader()
While Reader.Read()
LstResults.Add(New StoreDate(Reader!Store, Reader!Date))
End While
End Using
End Using
Conn.Close()
End Using
If LstResults.Count > 0 Then
Try
Using Conn As New SqlConnection(CONSTR)
Conn.Open()
For Each Result As StoreDate In LstResults
Store = Result.Store
Date = Result.Date
Using Cmd = New SqlCommand("STORE_PROCEDURE", Conn)
Cmd.CommandType = CommandType.StoredProcedure
Cmd.CommandTimeout = 0
Cmd.Parameters.AddWithValue("@Date", DateStore)
Cmd.Parameters.AddWithValue("@Store", Tienda)
Cmd.ExecuteNonQuery()
End Using
Next
Conn.Close()
End Using
Catch ex As Exception
log("Error: " & ex.Message, alog)
End Try
End If
SqlQuery.Clear()
LstResults.Clear()
Try
SqlQuery.Append("do another search ")
Using Conn As New SqlConnection(CONSTR)
Conn.Open()
Using Cmd = New SqlCommand(SqlQuery.ToString, Conn)
Cmd.Parameters.AddWithValue("@param1", Param1)
Cmd.Parameters.AddWithValue("@param2", Param2)
Cmd.Parameters.AddWithValue("@Param3", Param3)
Cmd.CommandTimeout = 0
Using Reader As SqlDataReader = Cmd.ExecuteReader()
While Reader.Read()
LstResults.Add(New StoreDate(Reader!Store, Reader!Date))
End While
End Using
End Using
Conn.Close()
End Using
Catch ex As Exception
log("Error: " & ex.Message , alog)
Environment.Exit(ex.HResult)
End Try
但是当它到达第二个数据读取器时,我收到错误:“已经有一个与此命令关联的数据读取器必须首先关闭”,这对我来说没有意义,“使用”应该处理对象完成后,不是吗?我在这里错过了一些愚蠢的事情吗?我希望有人能帮助我,我自己已经走到了死亡的尽头。
我所做的唯一更改是从数据表切换到列表,StoreDate是下一个简单的类,但与错误无关,它在点击第二个“Using Reader As SqlDataReader”后出现
Public Class StoreDate
Private _Store As String
Private _Date As DateTime
Public Property Store As String
Get
Return _Store
End Get
Set(value As String)
_Store = value
End Set
End Property
Public Property Date As DateTime
Get
Return _Date
End Get
Set(value As DateTime)
_Date = value
End Set
End Property
Public Sub New(ByVal Store As String, ByVal Date As DateTime)
_Store = Store
_Date = Date
End Sub
End Class
按照建议,我只打开一个连接,为了确定,切换到这样的数据表:
'Using Reader As SqlDataReader = Cmd.ExecuteReader()
' While Reader.Read() ' LstStoreDate.Add(New StoreDate(Reader!Store, Reader!DATE)) ' 结束同时 '结束使用 TblStoreDate.Load(Cmd.ExecuteReader())
但在进入“TblStoreDate.Load(Cmd.ExecuteReader()”后不断收到相同的错误...我想这给了我要搜索什么的线索,只需弄清楚那是什么线索