从SQL Server数据库连接两个表的组合框

时间:2014-04-13 14:26:49

标签: sql sql-server vb.net

我试图从SQL Server数据库中显示2个表,但没有显示任何内容

请帮助!!!!!!!

MysqlConn = New SqlConnection
    MysqlConn.ConnectionString =
        "Data Source=SABAHALI-SHEIKH;Initial Catalog=md_1103763;Integrated Security=True"
Dim READER As SqlDataReader

Try
        MysqlConn.Open()
        Dim Query As String
        Query = "SELECT [customerID], [firstname], [surname], [contactnumber], [emailaddress] FROM md_1103763.dbo.customer where firstname='" & ComboBox1.Text & "'"
        Query = "SELECT [bookingID], [pickupaddress], [destinationaddress], [datebooked], [timebooked] FROM md_1103763.dbo.booking where bookingID='" & ComboBox1.Text & "'"
        COMMAND = New SqlCommand(Query, MysqlConn)
        READER = COMMAND.ExecuteReader
        While READER.Read
            TextBox1.Text = READER.GetInt32(READER.GetOrdinal("customerID"))
            TextBox2.Text = READER.GetString(READER.GetOrdinal("firstname"))
            TextBox3.Text = READER.GetString(READER.GetOrdinal("surname"))
            TextBox4.Text = READER.GetString(READER.GetOrdinal("contactnumber"))
            TextBox5.Text = READER.GetString(READER.GetOrdinal("emailaddress"))

            TextBox8.Text = READER.GetString(READER.GetOrdinal("bookingID"))
            TextBox6.Text = READER.GetString(READER.GetOrdinal("pickupaddress"))
            TextBox7.Text = READER.GetString(READER.GetOrdinal("destinationaddress"))
            DateTimePicker1.Text = READER.GetString(READER.GetOrdinal("datebooked"))
            DateTimePicker2.Text = READER.GetString(READER.GetOrdinal("timebooked"))

        End While

        MysqlConn.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        MysqlConn.Dispose()


    End Try

1 个答案:

答案 0 :(得分:0)

您可以尝试更改 这段代码

MysqlConn = New SqlConnection
    MysqlConn.ConnectionString =
        "Data Source=SABAHALI-SHEIKH;Initial Catalog=md_1103763;Integrated Security=True"
Dim READER As SqlDataReader

Try
        MysqlConn.Open()
        Dim Query As String
        Query = "SELECT md_1103763.dbo.customer.customerID, md_1103763.dbo.customer.firstname, md_1103763.dbo.customer.surname,"&"md_1103763.dbo.customer.contactnumber, md_1103763.dbo.customer.emailaddress,"&
        "md_1103763.dbo.booking.bookingID, md_1103763.dbo.booking.pickupaddress, md_1103763.dbo.booking.destinationaddress," &
        "md_1103763.dbo.booking.datebooked, md_1103763.dbo.booking.timebooked FROM md_1103763.dbo.customer,md_1103763.dbo.booking "&
        " where firstname='" & ComboBox1.Text & "'" +" And bookingID='" & ComboBox1.Text & "'"

        COMMAND = New SqlCommand(Query, MysqlConn)
        READER = COMMAND.ExecuteReader
        While READER.Read
            TextBox1.Text = READER.GetInt32(READER.GetOrdinal("customerID"))
            TextBox2.Text = READER.GetString(READER.GetOrdinal("firstname"))
            TextBox3.Text = READER.GetString(READER.GetOrdinal("surname"))
            TextBox4.Text = READER.GetString(READER.GetOrdinal("contactnumber"))
            TextBox5.Text = READER.GetString(READER.GetOrdinal("emailaddress"))

            TextBox8.Text = READER.GetString(READER.GetOrdinal("bookingID"))
            TextBox6.Text = READER.GetString(READER.GetOrdinal("pickupaddress"))
            TextBox7.Text = READER.GetString(READER.GetOrdinal("destinationaddress"))
            DateTimePicker1.Text = READER.GetString(READER.GetOrdinal("datebooked"))
            DateTimePicker2.Text = READER.GetString(READER.GetOrdinal("timebooked"))

        End While

        MysqlConn.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        MysqlConn.Dispose()


    End Try