从 vb.net 数据读取器中的多个表读取
我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果没有看到你的表的架构,我不能肯定地说,但我猜你的 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).
在您的命令上尝试一下:
Try this on your command:
如果 (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