は、次の例を見てみましょう...SqlCommandオブジェクト
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 - 実施例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と自動配置 - 手動
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)
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と同じですが、接続はcmdではなくcnで開きます。 1つのストアドプロシージャだけを実行する場合は、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
usingステートメントはより安全ですが、ほとんどの状況ですべての例外を無視するtry/catchはお勧めしません。だから、どのようにしてどこにキャッチしようとしているのかは、あなたが達成しようとしていることに依存します。あなたの質問は何ですか? –
この質問は、私にはcodereview.stackexchange.comにもっと適しているように見えます。 –
空のtry/catchは単なる例でしたが、この場合は安全のためだけにありました。これは私がチェックする予定のプロシージャの外部に、テーブルを含むデータセットです。それがなければ、私は適切に対処するだろう。質問に関しては、それが最上位にありました.SqlCommandの処分を処理する最良の方法は何かを尋ねていました。私は個人的には例5が正しいと思っていますが、他者からのフィードバックを知りたがっています。 –