0

更推荐使用给定查询字符串更新数据库的这两种方法中的哪一种:

选项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

或者有没有其他更好的方法来做到这一点?

4

2 回答 2

1

由于与数据库访问无关的一个主要原因,这些都没有任何好处......

您正在吞噬任何和所有托管异常。仅当您可以适当地处理异常时才处理它们,几乎不需要处理异常并将其转换为返回值。我认为,如果您这样做,则不再是特殊情况。

我不太记得 VB.NETUsing语法,所以我还不能提供另一个示例。我也不会使用Call,我认为这不是必需的。

伪代码排序:

Using Dim command As New SqlCommand("INSERT...", conn)
    command.Parameters.AddWithValue()
    ....

    command.ExecuteNonQuery() // No need to call dispose, Using does that.
End Using
于 2011-09-05T08:18:57.357 回答
1

看起来您正在尝试创建一个可重用的 UpdateCommand,这一切都很好。除了不打开连接之外,我不确定您是否正在关闭连接(除非 command.Dispose 也关闭了连接。您最好将尽可能多的数据库连接移动到 Sub 中.

另外,我不确定你在哪里得到错误。从您的异常块中,但我建议设计一种更好的方法来处理任何异常 - 也许将它记录在某个地方。

最后,将 Sub 设为 Function 并返回一个表示成功/失败的 bool:

Public Function UpdateDatabase(ByVal sql As String, ByVal parameters() As SqlParameter) As Boolean

    Dim Successful As Boolean = False

    Try
        Using conn As SqlConnection = new SqlConnection(sqlConnection)
            Using command As New SqlCommand(sql, conn)

                command.CommandType = CommandType.Text        

                For Each parameter As SqlParameter In parameters
                    command.Parameters.Add(parameter)
                Next

                conn.Open()

                command.ExecuteNonQuery()
                Successful = True
            End Using       
        End Using        
    Catch ex As Exception
        Successful = False
        ' Do something with the exception
    End Try

End Function

然后你可以这样做:

Dim query As String = "INSERT INTO employee VALUES (@Name, @Age)"

Dim params As SqlParameter() = {
    New SqlParameter("@Name", txtName.Value),
    New SqlParameter("@Age", txtAge.Value))
}

Dim Updated As Boolean = UpdateDatabase(query, params)

此示例假设sqlConnection是一个保存连接字符串的类级别变量。如果需要,您也可以直接从配置文件中读取它。

如果该命令没有任何参数,则需要传入一个空数组(或修改函数中的代码以检查 params = Nothing):

Dim params As SqlParameter()
Dim Updated As Boolean = UpdateDatabase(query, params)  
于 2011-09-05T08:27:38.743 回答