Nehmen Sie das folgende Beispiel ...SqlCommand (mit Statement/Entsorgung Ausgabe)
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
heute Aus meiner Forschung ist klingt, als ob dies grundsätzlich in Ordnung ist, aber die SqlCommand nicht entsorgt werden.
Frage -> Welches der folgenden Beispiele ist der beste Weg, um damit umzugehen?
Beispiel 2 - Entsorgen manuell
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
Beispiel 3 - Automatisch mit der Verwendung von Anweisung
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
Beispiel 4 Entsorgung - die gleichen wie Beispiel 3, aber die Try/Catch ist innerhalb der Verwendung - Macht das einen Unterschied?
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
Beispiel 5 - Das gleiche wie Beispiel 4, aber die Commandtext und cn sind in der Verwendung von Statement angegeben - Welchen Vorteil hat das?
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
Beispiel 6 - Das gleiche wie in Beispiel 5, aber die Verbindung ist auf cn anstelle von cmd geöffnet. Ist es besser, die Verbindung auf cmd zu öffnen, wenn nur eine gespeicherte Prozedur ausgeführt werden soll?
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
Die using-Anweisung ist sicherer, aber eine try/catch, die alle Ausnahmen ignoriert, wird in den meisten Situationen abgeraten. Wie und wo Sie Ihren Versuch setzen, hängt davon ab, was Sie erreichen wollen. Was ist deine Frage? –
Diese Frage scheint mir besser für codereview.stackexchange.com geeignet zu sein –
Der leere Versuch/Fang war nur ein Beispiel, aber in diesem Fall war es nur zur Sicherheit da. Außerhalb der Prozedur, in der ich bin, würde ich prüfen, ob der Datensatz irgendwelche Tabellen enthält. Wenn nicht, würde ich angemessen damit umgehen. In Bezug auf die Frage, es war an der Spitze - ich fragte, was ist der beste Weg, mit SqlCommand Verfügung zu bewältigen. Ich persönlich denke, dass Beispiel 5 das richtige ist, wollte aber Feedback von anderen wissen. –