-1

当我对以下代码运行代码分析时:

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

光标位于最后的“结束使用”行;这如何被视为对象的双重处置?不是两个“使用”块都需要以这种方式终止吗?

4

1 回答 1

0

我尝试删除connection.createcommandAnd 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
于 2016-12-14T21:31:50.463 回答