当我针对以下代码运行代码分析时:
Protected Function GetOrderEntry() As IList(Of OE)
Dim results As IList(Of OE) = New List(Of OE)()
Using connection As IDbConnection = DbProviderFactories.GetFactory("System.Data.SqlClient").CreateConnection()
connection.ConnectionString = ConfigurationManager.AppSettings("OrderEnterConnection")
Using command As IDbCommand = connection.CreateCommand()
command.CommandTimeout = 120
command.CommandText = "Exec up_ViewOrderDetail_2012_Order '" & MemberNo1.Value & "','" & CustNo.Value & "','DEFAULT','" & OrderNo.Value & "','" & DeliveryDate.SelectedDate & "','','','','1'"
connection.Open()
Try
Dim reader As IDataReader = command.ExecuteReader()
results = PopulateGrid(reader)
Catch ex As SqlException
results.Clear()
connection.Close()
End Try
End Using
End Using
Return results
End Function
......我明白了,
" CA2202多次弃置物品对象'连接'可以在方法' OrderConfirm.GetOrderEntry()'中多次处理。为避免生成System.ObjectDisposedException,不应在对象上多次调用Dispose "
光标位于最终"结束时使用"线;这被视为双重处置对象?并不是"使用"块需要以这种方式终止吗?
答案 0 :(得分:0)
我尝试删除connection.createcommand
和connection.Close()
。
它运作良好
Protected Function GetOrderEntry() As IList(Of OE)
Dim results As IList(Of OE) = New List(Of OE)()
Using connection As IDbConnection = DbProviderFactories.GetFactory("System.Data.SqlClient").CreateConnection()
connection.ConnectionString = ConfigurationManager.AppSettings("OrderEnterConnection")
Using command As New IDbCommand
Command.connection = connection
command.CommandTimeout = 120
command.CommandText = "Exec up_ViewOrderDetail_2012_Order '" & MemberNo1.Value & "','" & CustNo.Value & "','DEFAULT','" & OrderNo.Value & "','" & DeliveryDate.SelectedDate & "','','','','1'"
connection.Open()
Try
Dim reader As IDataReader = command.ExecuteReader()
results = PopulateGrid(reader)
Reader.close()
Catch ex As SqlException
results.Clear()
End Try
End Using
End Using
Return results
End Function