来自Access的Vb从多个表导入数据

时间:2015-03-12 12:54:13

标签: mysql vb.net ms-access-2010

我在visual basic中有以下代码,目前允许来自一个表的数据填充表单。我需要能够从另一个表中选择数据并将该数据添加到与学号主键相关的同一表单上。

Private Sub ButSearch_Click(sender As Object, e As EventArgs) Handles ButSearch.Click
        If TxtSearchFirstname.Text = "" Then
            MessageBox.Show("Please complete the required fields..", "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            Call Connection()
            Try
                Dim sql As String = "SELECT * FROM Students WHERE FirstName='" & TxtSearchFirstname.Text & "' "
                Dim sqlCom As New System.Data.OleDb.OleDbCommand(sql)
                sqlCom.Connection = conn
                Dim sqlRead As System.Data.OleDb.OleDbDataReader = sqlCom.ExecuteReader()
                Dim Test As String : Test = sqlRead.Read()
                TxtFirstname.Text = sqlRead.GetString(0)
                TxtLastname.Text = sqlRead.GetString(1)
                TxtYear.Text = sqlRead.GetString(2)
                TxtGender.Text = sqlRead.GetString(3)
                TxtTarget.Text = sqlRead.GetString(4)
                TxtStudentNumber.Text = sqlRead.GetValue(5)


            Catch ex As Exception
                MessageBox.Show("Failed to connect to Database..", "Database Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If

2 个答案:

答案 0 :(得分:0)

您需要INNER JOIN句来从多个表中获取数据。

答案 1 :(得分:0)

是。这里需要加入。你可以使用这样的东西:

 Dim sql As String = "SELECT S.*, OT.Whatever FROM Students S LEFT JOIN OtherTable OT ON S.SomeColumn = OT.SomeColumn WHERE FirstName=@FirstName"
 Dim sqlCom As New System.Data.OleDb.OleDbCommand(sql)
 sqlCom.Parameters.Add("@FirstName", SqlDbType.NVarChar).Value = TxtSearchFirstname.Text