的SqlCommand(using语句/处置问题)(SqlCommand (Using Statem

2019-07-31 17:34发布

看看下面的例子...

        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 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 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,但在CommandText和cn在using语句指定的 - 这是否有什么优势?

        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

Answer 1:

该DataAdapter.Fill方法命令将打开和关闭连接本身,所以你不需要cmd.Connection.Open() (参考文献:备注部分http://msdn.microsoft.com/en-us/library/377a8x4t.aspx 。)

使用Using的SqlConnection的有调用的影响.Close上它。

变量cmd符合垃圾收集一旦超出范围(或者,如果.NET决定了它不会被再次使用更早版本)。

在你的榜样2,我不知道这是一个好主意DataAdapter的使用了前处置CMD的。


[来自用户“JefBar软件服务”的信息,我应该调用Dispose SqlCommand对象吗? ]在写作的时候,调用.DisposeSqlCommand没有的,因为影响它的构造函数的代码 :

public SqlCommand() : base() {
    GC.SuppressFinalize(this);
}


文章来源: SqlCommand (Using Statement / Disposing issue)