从vb.net数据阅读器中的多个表中读取

时间:2010-03-18 02:29:36

标签: mysql vb.net datareader mysql-error-1052

我正在尝试从mysql中的两个表中读取:

Dim sqlcom As MySqlCommand = New MySqlCommand("Select * from  mother, father where IDNO= '" & TextBox14.Text & "' ", sqlcon)

- 但我收到此错误:

Column 'IDNO' in where clause is ambiguous

以下是整个代码:

Dim NoAcc As String
        Dim NoAccmod2 As String
        Dim NoPas As String

        Dim sqlcon As New MySqlConnection("Server=localhost; Database=school;Uid=root;Pwd=nitoryolai123$%^;")
        Dim sqlcom As MySqlCommand = New MySqlCommand("Select * from  mother, father where IDNO= '" & TextBox14.Text & "' ", sqlcon)

        sqlcon.Open()


        Dim rdr As MySqlDataReader
        rdr = sqlcom.ExecuteReader


        If rdr.HasRows Then
            rdr.Read()
            NoAcc = rdr("IDNO")
            If (TextBox14.Text = NoAcc) Then TextBox7.Text = rdr("MOTHER")
            If (TextBox14.Text = NoAcc) Then TextBox8.Text = rdr("MOTHER_OCCUPATION")
            If (TextBox14.Text = NoAcc) Then TextBox10.Text = rdr("FATHER")
            If (TextBox14.Text = NoAcc) Then TextBox11.Text = rdr("FATHER_OCCUPATION")
        End If

- 任何有助于解决此问题的建议? 或者甚至是其他技术来实现使用数据读取器从两个表读取数据的目标?

这是一个winform,而不是网络表单

3 个答案:

答案 0 :(得分:1)

如果没有看到表格的模式,我无法肯定地说,但我猜你的ID列在两个表中都是相同的。要解决这个问题,你需要使用mother.IDNO或father.IDNO(或mother.IDNO和father.IDNO)来完全符合你要求的资格。

答案 1 :(得分:0)

按照您的命令尝试:

Dim sqlcom As MySqlCommand = New MySqlCommand("Select * from  mother, father where mother.IDNO= '" & TextBox14.Text & "' AND father.IDNO = '" & TextBox14.Text  & "'", sqlcon)

答案 2 :(得分:0)

If(TextBox14.Text = NoAcc)Then TextBox7.Text = rdr(“MOTHER”)

如果(TextBox14.Text = NoAcc)则TextBox8.Text = rdr(“MOTHER_OCCUPATION”)

If(TextBox14.Text = NoAcc)Then TextBox10.Text = rdr(“FATHER”)

如果(TextBox14.Text = NoAcc)则TextBox11.Text = rdr(“FATHER_OCCUPATION”)

看起来多余/低效不是吗?

您也可以尝试使用INNER JOIN而不是双IDNO“Where”s