特定のクエリ文字列でデータベースを更新するには、次の 2 つの方法のどちらをお勧めしますか?
オプション1:
Dim query As String = "INSERT INTO employee VALUES (@Name, @Age)"
Dim command As New SqlClient.SqlCommand(query, sqlConnection)
Dim params As SqlParameter() = {
New SqlParameter("@Name", txtName.Value),
New SqlParameter("@Age", txtAge.Value))
}
Call UpdateDatabase(command, params, NumError, DescError)
Public Sub UpdateDatabase(ByVal command As SqlCommand, ByVal parameters() As SqlParameter, ByRef NumError As Double, ByRef DescError As String)
Try
For Each parameter In parameters
command.Parameters.Add(parameter)
Next
command.ExecuteNonQuery()
command.Dispose()
NumError = 0
DescError = ""
Catch ex As Exception
NumError = Err.Number
DescError = Err.Description
End Try
End Sub
オプション 2:
Dim query As String = "INSERT INTO employee VALUES (@Name, @Age)"
Dim command As New SqlClient.SqlCommand(query, sqlConnection)
command.Parameters.AddWithValue("@Name", txtName.Value)
command.Parameters.AddWithValue("@Age", txtAge.Value)
Call UpdateDatabase(command, NumError, DescError)
Public Sub UpdateDatabase(ByVal command As SqlCommand, ByRef NumError As Double, ByRef DescError As String)
Try
command.ExecuteNonQuery()
command.Dispose()
NumError = 0
DescError = ""
Catch ex As Exception
NumError = Err.Number
DescError = Err.Description
End Try
End Sub
または、これを行うための他の良い方法はありますか?