【问题标题】:Looking for the best way to use ExecuteScalar()寻找使用 ExecuteScalar() 的最佳方式
【发布时间】:2012-03-17 00:14:17
【问题描述】:

此代码有效。它基于我在互联网上找到的一些代码。

您能告诉我编码是否是获得标量值的最佳方式吗?是否有更好的方式可以显示编码示例?

Dim objParentNameFound As Object

TextBoxParentsName.Text = ""

If TextBoxParentID.Text <> "" Then

    ' Display the parent's name using the parent ID. '
    Dim strSqlStatement As String = "Select FatherName " & _
                                      "From Parents " & _
                                     "Where ID = @SearchValue"

    ' Set up the sql command and lookup the parent. '
    Using objSqlCommand As SqlCommand = New SqlCommand(strSqlStatement, ObjConnection)

        With objSqlCommand

            ' Add SqlParameters to the SqlCommand. '
            .Parameters.Clear()
            .Parameters.AddWithValue("@SearchValue", TextBoxParentID.Text)

            ' Open the SqlConnection before executing the query. '
            Try
                ObjConnection.Open()

                Try
                    objParentNameFound = .ExecuteScalar()
                    If objParentNameFound <> Nothing Then

                        ' Display the parent name here. '
                        TextBoxParentsName.Text = objParentNameFound
                    End If

                Catch exSqlErrors As SqlException
                    MessageBox.Show("Sorry, I couldn't execute your query because of this error: " & _
                                    vbCrLf & vbCrLf & exSqlErrors.Message, _
                                    "Error")
                End Try
            Catch exErrors As Exception

                MessageBox.Show("Sorry, there was an error. Details follow: " & _
                                vbCrLf & vbCrLf & exErrors.Message, _
                                "Error")
            Finally
                ObjConnection.Close()
            End Try
        End With 
    End Using 
End If 

【问题讨论】:

    标签: sql vb.net coding-style executescalar


    【解决方案1】:

    Microsoft Access Application 块有一些很好的例子来说明如何使用 ADO.Net。特别是您可能会发现他们如何将诸如ExecuteScalar() 之类的任务组织成一系列重载方法,从而使调用您需要的过程变得容易。 您发布的示例将极大地受益于分离关注点。换句话说,使用你用来建立连接、命令和参数的代码,并将其作为一个或多个单独的方法。这允许重复使用代码,而无需在整个代码库中复制和粘贴它。这允许您的调用代码简单地传入参数并将结果绑定到您的文本框或其他控件。

    编辑:示例 假设您使用 SqlHelper.vb 类,您可以执行以下操作:

    Dim searchValue As Integer = 1
    Dim myConnectionString As String = "MyConnectionString"
    Dim sqlStatement As String = "SELECT FatherName FROM Parents WHERE ID = @SearchValue"
    Dim paramList(0) As SqlParameter
    paramList(0) = New SqlParameter() With {.Value = searchValue, .ParameterName = "@SearchValue"}
    
    TextBoxParentsName.Text = SqlHelper.ExecuteScalar(myConnectionString, CommandType.Text, sqlStatement, paramList).ToString()
    

    【讨论】:

    • 感谢您的回复。我将查看 Microsoft Access 应用程序,看看是否可以从中学习。如果有时间,能否展示一些代码块,以帮助进一步扩展将代码分离为其他方法?
    猜你喜欢
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多