【问题标题】:Read from multiple tables in vb.net data reader从 vb.net 数据阅读器中的多个表中读取
【发布时间】:2010-03-18 02:29:36
【问题描述】:

我正在尝试从 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,而不是 web 表单

【问题讨论】:

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


【解决方案1】:

如果没有看到您的表的架构,我不能肯定地说,但我猜您的 ID 列在两个表中的名称相同。为了解决这个问题,您需要使用 mother.IDNO 或父亲.IDNO(或母亲.IDNO 和父亲.IDNO)来完全限定您正在寻找的那个。

【讨论】:

  • 巴里说得对。这个问题与.NET 无关,而是查询。如果您在 Management Studio 或 LINQpad 中发出相同的查询,您会看到相同的错误。
  • 对了,我在mysql上查询时遇到同样的错误,请问正确的查询是什么?
【解决方案2】:

在你的命令上试试这个:

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

【讨论】:

    【解决方案3】:

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

    If (TextBox14.Text = NoAcc) Then TextBox8.Text = rdr("MOTHER_OCCUPATION")

    如果 (TextBox14.Text = NoAcc) 那么 TextBox10.Text = rdr("父亲")

    If (TextBox14.Text = NoAcc) Then TextBox11.Text = rdr("FATHER_OCCUPATION")

    看起来相当多余/效率低下不是吗?

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多