如何防止此代码多次处理对象?
How can I prevent this code from disposing objects multiple times?
当我 运行 针对以下代码进行代码分析时:
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 Do not dispose objects multiple times Object 'connection' can be disposed more than once in method 'OrderConfirm.GetOrderEntry()'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object"
光标在最后"End Using"行;这如何被视为对象的双重处置? "Using" 两个块不需要以这种方式终止吗?
我尝试删除 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
当我 运行 针对以下代码进行代码分析时:
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 Do not dispose objects multiple times Object 'connection' can be disposed more than once in method 'OrderConfirm.GetOrderEntry()'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object"
光标在最后"End Using"行;这如何被视为对象的双重处置? "Using" 两个块不需要以这种方式终止吗?
我尝试删除 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