如何让用户在查询“Visual Studio 2012”中输入值& “SQL 服务器”

发布于 2024-12-27 04:45:15 字数 223 浏览 2 评论 0原文

我是 Visual Studio 的新人,我想让用户在查询中输入一个值

,即搜索我将使用的员工

Select * From Employee Where ID = (i want the user to enter the value here)

我已经连接了数据库 我知道我可以从文本框中获取值,但我真的不知道如何直接将该值放入查询中并立即调用它

I 'm new At visual studio and i want to let the user enter a value into the query

i.e search for an employee I'll use

Select * From Employee Where ID = (i want the user to enter the value here)

I already connected the database
and I know I can get the value from a text box but i really don't know how to put that value in the query directly and call it immediately

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拧巴小姐 2025-01-03 04:45:15

参数化相对简单。

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    ExecuteSQL("Data Source=Server;Initial Catalog=Database;Persist Security Info=True;Integrated Security=True", _
               "Select * From Employee Where ID=@id",
               New SqlClient.SqlParameter("@id", 123))
End Sub

Public Function ExecuteSQL(ByVal Connection As String, _
                      ByVal SQL As String, _
                      ByRef Param As SqlClient.SqlParameter) As System.Data.DataTable
    Try
        Dim dt As System.Data.DataTable = Nothing
        Dim SqlRdr As SqlClient.SqlDataReader

        Using SqlConn As SqlClient.SqlConnection = New SqlClient.SqlConnection(Connection)
            Using SqlCmd As SqlClient.SqlCommand = New SqlClient.SqlCommand(SQL, SqlConn)
                SqlCmd.CommandType = CommandType.Text
                SqlCmd.Parameters.Add(Param)
                SqlConn.Open()
                SqlRdr = SqlCmd.ExecuteReader()
                Try
                    If SqlRdr.IsClosed = False AndAlso SqlRdr.HasRows = True Then
                        dt = New System.Data.DataTable
                        dt.BeginLoadData()
                        dt.Load(SqlRdr)
                        dt.EndLoadData()
                    End If
                Finally
                    If SqlRdr IsNot Nothing Then
                        SqlRdr.Close()
                    End If
                End Try
            End Using
        End Using

        Return dt
    Catch ex As Exception
        Return Nothing
    End Try
End Function

Parameterizing is relatively simple.

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    ExecuteSQL("Data Source=Server;Initial Catalog=Database;Persist Security Info=True;Integrated Security=True", _
               "Select * From Employee Where ID=@id",
               New SqlClient.SqlParameter("@id", 123))
End Sub

Public Function ExecuteSQL(ByVal Connection As String, _
                      ByVal SQL As String, _
                      ByRef Param As SqlClient.SqlParameter) As System.Data.DataTable
    Try
        Dim dt As System.Data.DataTable = Nothing
        Dim SqlRdr As SqlClient.SqlDataReader

        Using SqlConn As SqlClient.SqlConnection = New SqlClient.SqlConnection(Connection)
            Using SqlCmd As SqlClient.SqlCommand = New SqlClient.SqlCommand(SQL, SqlConn)
                SqlCmd.CommandType = CommandType.Text
                SqlCmd.Parameters.Add(Param)
                SqlConn.Open()
                SqlRdr = SqlCmd.ExecuteReader()
                Try
                    If SqlRdr.IsClosed = False AndAlso SqlRdr.HasRows = True Then
                        dt = New System.Data.DataTable
                        dt.BeginLoadData()
                        dt.Load(SqlRdr)
                        dt.EndLoadData()
                    End If
                Finally
                    If SqlRdr IsNot Nothing Then
                        SqlRdr.Close()
                    End If
                End Try
            End Using
        End Using

        Return dt
    Catch ex As Exception
        Return Nothing
    End Try
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文