2012-09-19 7 views
8

は、次の例を見てみましょう...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 
+1

usingステートメントはより安全ですが、ほとんどの状況ですべての例外を無視するtry/catchはお勧めしません。だから、どのようにしてどこにキャッチしようとしているのかは、あなたが達成しようとしていることに依存します。あなたの質問は何ですか? –

+0

この質問は、私にはcodereview.stackexchange.comにもっと適しているように見えます。 –

+0

空のtry/catchは単なる例でしたが、この場合は安全のためだけにありました。これは私がチェックする予定のプロシージャの外部に、テーブルを含むデータセットです。それがなければ、私は適切に対処するだろう。質問に関しては、それが最上位にありました.SqlCommandの処分を処理する最良の方法は何かを尋ねていました。私は個人的には例5が正しいと思っていますが、他者からのフィードバックを知りたがっています。 –

答えて

7

DataAdapter.Fillコマンドが開き、接続自体を閉じるので、あなたはcmd.Connection.Open()を必要としないでしょう。 (Ref:http://msdn.microsoft.com/en-us/library/377a8x4t.aspxの備考欄)

UsingをSqlConnectionに使用すると、.Closeが呼び出されます。

変数cmdは、スコープ外になると(または.NETが再び使用されないと判断した場合は)、ガーベジコレクションに適格になります。

例2では、​​DataAdapterが使用する前にcmdを処分することをお勧めします。

+0

ありがとうございます。私はデータアダプタについてそれがそれに応じて変更されることは知らなかった。したがって、項目がガベージコレクションのために使用可能になる場合は、Usingステートメントは実際には必要ではないと言っていますか? –

+0

@cw_devはい。私をかわしてしまったことの一つは、 'cmdを使う... cmd.Connection = ... cmd.Connection.Open()...(実行して読む) cmd内の接続。しかし、それはあなたがそれをやろうとしているようには見えません。 SqlConnectionで 'Using'を使用すると、接続を.Close()にするのが便利です。 –

関連する問題