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

发布于 2024-08-26 03:48:27 字数 1259 浏览 9 评论 0原文

我正在尝试从 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表单

I'm trying to read from two tables in mysql:

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

-But I get this error:

Column 'IDNO' in where clause is ambiguous

Here is the whole code:

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

-Any suggestions that could help solve this problem?
Or even other techniques on achieving the goal of reading data from two tables using data reader?

This is a winform, not a web form

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉城 2024-09-02 03:48:28

如果没有看到你的表的架构,我不能肯定地说,但我猜你的 ID 列在两个表中的命名相同。为了解决这个问题,您需要使用 mother.IDNO 或father.IDNO(或 mother.IDNO ANDfather.IDNO)来完全限定您要查找的对象。

Without seeing the schema of your tables I can't say for certain, but I'd guess your ID columns are named the same in both tables. To get around that, you're going to need to fully qualify the one you're looking for, by using mother.IDNO or father.IDNO (or mother.IDNO AND father.IDNO).

人心善变 2024-09-02 03:48:28

在您的命令上尝试一下:

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

Try this on your command:

Dim sqlcom As MySqlCommand = New MySqlCommand("Select * from  mother, father where mother.IDNO= '" & TextBox14.Text & "' AND father.IDNO = '" & TextBox14.Text  & "'", sqlcon)
此岸叶落 2024-09-02 03:48:28

如果 (TextBox14.Text = NoAcc) 那么 TextBox7.Text = rdr("MOTHER")

如果 (TextBox14.Text = NoAcc) 那么 TextBox8.Text = rdr("MOTHER_OCCUPATION")

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

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

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

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

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")

Looks rather redundant/inefficient doesn't it?

Also you might try using an INNER JOIN instead of dual IDNO "Where"s

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文