-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 オブジェクトを複数回破棄しないでください オブジェクト 'connection' は、メソッド 'OrderConfirm.GetOrderEntry()' で複数回破棄できます。System.ObjectDisposedException の生成を回避するには、オブジェクトで Dispose を複数回呼び出さないでください"

カーソルは最後の "End Using" 行にあります。これはどのようにオブジェクトの二重処分と見なされますか? 「Using」ブロックの両方をこの方法で終了する必要はありませんか?

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 に答える