2010-03-18 87 views
0

我想从两个表在MySQL阅读:从vb.net数据读取多个表中读取

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表单

+0

仅供参考,您的代码是受到SQL注入攻击。 http://stackoverflow.com/questions/tagged/sql-injection – Josh 2010-03-18 04:54:50

回答

0

试试这个在您的命令:

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

没有看到你的表的架构我不能肯定地说,但我猜你的ID列被命名为两个表中的相同。为了解决这个问题,你需要通过使用mother.IDNO或者father.IDNO(或者mother.IDNO和father.IDNO)来完全限定你正在寻找的那个。

+0

是的,他们都有IDNO作为主键 – user225269 2010-03-18 02:50:14

+0

巴里的权利。这个问题与.NET无关,而是查询。如果您在Management Studio或LINQpad中发出相同的查询,您会看到相同的错误。 – Josh 2010-03-18 03:00:05

+0

对,我在mysql上查询时遇到同样的错误,那么正确的查询是什么? – user225269 2010-03-18 03:08:25

0

如果(TextBox14.Text = NoAcc)然后TextBox7.Text = RDR( “母亲”)

如果(TextBox14.Text = NoAcc)然后TextBox8的.text = RDR( “MOTHER_OCCUPATION”)

如果(TextBox14.Text = NoAcc)然后TextBox10.Text = RDR( “父亲”)

如果(TextBox14.Text = NoAcc)然后TextBox11.Text = RDR (“父亲的职业”)

看起来相当冗余/效率不高吗?你

还可以尝试使用INNER JOIN,而不是双IDNO“其中” s