【问题标题】:Displaying data from stored procedures with multiple tables into textbox将具有多个表的存储过程中的数据显示到文本框中
【发布时间】:2012-02-05 19:28:32
【问题描述】:
SELECT 
    Login.LoginID, Student.[Student Name], Student.[Student address], Student.StudentID
FROM
    Login 
INNER JOIN
    Student ON Login.LoginID = Student.LoginID
WHERE
    (Login.username = @user) AND (Login.password = @pass)

RETURN 

如何在多个文本框中显示学生姓名、学生地址、学生 ID 和登录 ID?

【问题讨论】:

  • 你需要看一个教程;您的问题可能无意中过于宽泛

标签: database vb.net stored-procedures


【解决方案1】:

下面是如何从我们的存储过程中检索结果并将返回的列分配给文本框的示例:

Function GetInfoForStudent(ByRef QueryName As String, ByVal UserName As String, ByVal Password As String) As DataTable
    Using Con As New SqlConnection
        Try
            Using OleCon As New SqlConnection
                Dim Connection As String = "MyConnectionString"
                Con.Open()
                Dim Cmd As SqlCommand = Con.CreateCommand()
                Cmd.CommandType = CommandType.StoredProcedure
                Cmd.CommandText = QueryName
                Cmd.Parameters.AddWithValue("user", UserName)
                Cmd.Parameters.AddWithValue("password", Password)
                Dim da As New SqlDataAdapter(Cmd)
                Dim ds As New DataTable()
                da.Fill(ds)
                Return ds
            End Using
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Using

End Function

Sub ShowStudentInfo()
    Dim dt As DataTable = GetInfoForStudent("MyStoredProcName", "MyUserName", "MyPasswword")
    ' Since (presumably) only one is returned
    With dt.Rows(0)
        ' Assign your text boxes 
        'LoginIDTextBox.Text = .Item("LoginID")
        'StudentNameTextBox.Text = .Item("Student Name")
        'StudentAddressTextBox.Text = .Item("Student address")
        'StudentIDTextBox.Text = .Item("StudentID")
    End With
End Sub

【讨论】:

    【解决方案2】:

    您的问题不够具体,但您可以为您的 SP 创建一个类型化的数据集,生成一个表格,然后在设计器中将学生姓名、学生地址、学生 ID 和登录 ID 列绑定到 4 个不同的文本框:TextBox1 ->DataBindings->Text 并选择你的 SP 作为数据源。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-28
      • 1970-01-01
      • 2018-08-13
      • 2016-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多