9

举个例子……

        Using cn As New SqlConnection(ConnectionString)
            Try
                Dim cmd As SqlCommand = New SqlCommand
                With cmd
                    .Connection = cn
                    .Connection.Open()
                    .CommandText = "dbo.GetCustomerByID"
                    .CommandType = CommandType.StoredProcedure
                    .Parameters.Add("@CustomerID", SqlDbType.Int, 4)
                    .Parameters("@CustomerID").Value = CustomerID
                End With

                da = New SqlDataAdapter(cmd)
                da.Fill(ds, "Customer")
            Catch ex As Exception

            End Try
        End Using

从我今天的研究来看,这听起来好像基本上没问题,但 SqlCommand 并没有被处理掉。

问题 -> 以下哪个示例是处理此问题的最佳方法?

示例 2 - 手动处置

        Using cn As New SqlConnection(ConnectionString)
            Try
                Dim cmd As SqlCommand = New SqlCommand
                With cmd
                    .Connection = cn
                    .Connection.Open()
                    .CommandText = "dbo.GetCustomerByID"
                    .CommandType = CommandType.StoredProcedure
                    .Parameters.Add("@CustomerID", SqlDbType.Int, 4)
                    .Parameters("@CustomerID").Value = CustomerID
                End With

                da = New SqlDataAdapter(cmd)
                cmd.Dispose()
                da.Fill(ds, "Customer")
            Catch ex As Exception

            End Try
        End Using

示例 3 - 使用 Using 语句自动处理

        Using cn As New SqlConnection(ConnectionString)
            Try
                Using cmd As New SqlCommand
                    With cmd
                        .Connection = cn
                        .Connection.Open()
                        .CommandText = "dbo.GetCustomerByID"
                        .CommandType = CommandType.StoredProcedure
                        .Parameters.Add("@CustomerID", SqlDbType.Int, 4)
                        .Parameters("@CustomerID").Value = CustomerID
                    End With

                    da = New SqlDataAdapter(cmd)
                    da.Fill(ds, "Customer")
                End Using
            Catch ex As Exception

            End Try
        End Using

示例 4 - 与示例 3 相同,但 Try/Catch 在 Using 中 - 这有什么不同吗?

        Using cn As New SqlConnection(ConnectionString)
            Using cmd As New SqlCommand
                Try
                    With cmd
                        .Connection = cn
                        .Connection.Open()
                        .CommandText = "dbo.GetCustomerByID"
                        .CommandType = CommandType.StoredProcedure
                        .Parameters.Add("@CustomerID", SqlDbType.Int, 4)
                        .Parameters("@CustomerID").Value = CustomerID
                    End With

                    da = New SqlDataAdapter(cmd)
                    da.Fill(ds, "Customer")
                Catch ex As Exception

                End Try
            End Using
        End Using

示例 5 - 与示例 4 相同,但在 Using 语句中指定了 CommandText 和 cn - 这有什么优势?

        Using cn As New SqlConnection(ConnectionString)
            Using cmd As New SqlCommand("GetCustomerByID", cn)
                Try
                    With cmd
                        .Connection.Open()
                        .CommandType = CommandType.StoredProcedure
                        .Parameters.Add("@CustomerID", SqlDbType.Int, 4)
                        .Parameters("@CustomerID").Value = CustomerID
                    End With

                    da = New SqlDataAdapter(cmd)
                    da.Fill(ds, "Customer")
                Catch ex As Exception

                End Try
            End Using
        End Using

示例 6 - 与示例 5 相同,但连接是在 cn 而不是 cmd 上打开的。如果只执行一个存储过程,在cmd上打开连接会更好吗?

        Using cn As New SqlConnection(ConnectionString)
            cn.Open()

            Using cmd As New SqlCommand("GetCustomerByID", cn)
                Try
                    With cmd
                        .Connection = cn
                        .CommandType = CommandType.StoredProcedure
                        .Parameters.Add("@CustomerID", SqlDbType.Int, 4)
                        .Parameters("@CustomerID").Value = CustomerID
                    End With

                    da = New SqlDataAdapter(cmd)
                    da.Fill(ds, "Customer")
                Catch ex As Exception

                End Try
            End Using
        End Using
4

1 回答 1

9

DataAdapter.Fill 命令将打开和关闭连接本身,因此您不需要cmd.Connection.Open(). (参考:http: //msdn.microsoft.com/en-us/library/377a8x4t.aspx中的备注部分。)

使用UsingSqlConnection 具有为您调用.Close它的效果。

cmd一旦超出范围(或更早,如果 .NET 确定它不会再次使用),该变量就有资格进行垃圾收集。

在您的示例 2 中,我不确定在 DataAdapter 使用它之前处理 cmd 是否是个好主意。


[我应该在 SQLCommand 对象上调用 Dispose 吗?来自用户“JefBar Software Services”的信息?] 在撰写本文时,由于其构造函数中的代码.Dispose,调用anSqlCommand无效:

public SqlCommand() : base() {
    GC.SuppressFinalize(this);
}
于 2012-09-19T18:18:24.577 回答