将具有多个表的存储过程中的数据显示到文本框中

时间:2012-02-05 19:28:33

标签: database vb.net stored-procedures

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?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您的问题不够具体,但您可以为SP创建一个类型化数据集,从而生成一个表,然后将Student Name,Student Address,StudentID和LoginID列绑定到设计器中的4个不同的TextBox:TextBox1 - > DataBindings->文本并选择您的SP作为数据源。