将MySql数据库中不同表的数据显示到vb.net 2012 Textboxes中

时间:2017-02-04 18:26:15

标签: mysql vb.net

我正在尝试运行一些代码行,以便在vb.net 2012中显示来自MySql数据库的不同表的一些数据。这就是它的工作原理。当我使用我的用户名和密码登录时,它应该使用我的登录详细信息在提供的文本框中显示我的商店名称和分支名称。此代码不是以登录表单形式编写的。下面是我的代码

Imports MySql.Data.MySqlClient

Public Class SelectionForm

    Dim sqlconn As MySqlConnection
    Dim sqlcmd As MySqlCommand


    Private Sub SelectionForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        sqlconn = New MySqlConnection
        sqlconn.ConnectionString = "server=localhost;userid=root;password=;database=osap"

        Dim sqlreader As MySqlDataReader
        'Dim sqladapter As MySqlDataAdapter

        Try
            sqlconn.Open()
            Dim Query As String
            Query = "SELECT sales_reps.username, stores.name, branches.name FROM sales_reps, stores, branches WHERE sales_reps.id =(SELECT id FROM sales_reps WHERE username='" & LoginForm.UserTextBox.Text & "' AND password='" & LoginForm.PassTextBox.Text & "') AND branches.id = sales_reps.branch_id AND stores.id = branches.store_id"
            sqlcmd = New MySqlCommand(Query, sqlconn)

            sqlreader = sqlcmd.ExecuteReader()

            If sqlreader.HasRows Then
                sqlreader.Read()
                BranchTextBox.Text = sqlreader.GetString("branches.name")
                StoreTextBox.Text = sqlreader.GetString("stores.name")

            End If

            sqlconn.Close()

        Catch ex As MySqlException
            MessageBox.Show(ex.Message)

        Finally
            sqlconn.Dispose()
        End Try

    End Sub
End Class

不会显示任何错误,但也不会在文本框中显示数据。

0 个答案:

没有答案