VB   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了vb.net – SqlCommand(使用Statement / Disposing问题)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
以下面的例子为例……
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",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",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","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","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","Customer")
                Catch ex As Exception

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

使用Using for SqlConnection可以为你调用.Close.

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

你的例子2中,我不确定在DataAdapter使用它之前处理cmd是个好主意.

大佬总结

以上是大佬教程为你收集整理的vb.net – SqlCommand(使用Statement / Disposing问题)全部内容,希望文章能够帮你解决vb.net – SqlCommand(使用Statement / Disposing问题)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。