内部加入VB.Net

时间:2014-02-08 11:12:07

标签: mysql sql ms-access-2007 inner-join vb.net-2010

#Region "FillListView"
    Sub FillListview()
        LV.Items.Clear()
        myqry = "SELECT AccResult.StudNo,Exercises.ID from AccResult INNER JOIN Exercises ON AccResult.StudNo = Exercises.ID ORDER BY AccResult.FirstName,AccResult.YrandSec Asc;"
        mycmd = New OleDbCommand(myqry, con)
        con.Open()
        mydr = mycmd.ExecuteReader
        While mydr.Read
              With LV
                .Items.Add(mydr("StudNo"))
                With .Items(.Items.Count - 1).SubItems
                    .Add(mydr("CNumber"))
                    .Add(mydr("FirstName"))
                    .Add(mydr("LastName"))
                    .Add(mydr("YrandSec"))
                    .Add(mydr("Exer1"))
                    .Add(mydr("Exer2"))
                    .Add(mydr("Exer3"))
                    .Add(mydr("Exer4"))
                    .Add(mydr("Exer5"))
                End With
            End With
        End While
        con.Close()
    End Sub
#End Region

AccResult是我的第一个表的名称,Exercises是第二个表。 我AccResult的PK为StudNo,而ExercisesID。 如何将这两个表连接起来显示在ListView

AccResult表:

StudNo (PK)
CNumber
FirstName
LastName
YrandSec

Exercises表:

ID (PK)
StudNo
Exer1
Exer2
Exer3
Exer4
Exer5

3 个答案:

答案 0 :(得分:2)

您的加入条件不正确。

AccResult INNER JOIN Exercises ON AccResult.StudNo = Exercises.ID

使用子表的主键(Exercises.ID)加入父表(AccResult.StudNo)的主键。您需要将父表(AccResult.StudNo)的主键与子表的外键(Exercises.StudNo)连接,即

AccResult INNER JOIN Exercises ON AccResult.StudNo = Exercises.StudNo

答案 1 :(得分:0)

不确定我是否正确地得到了问题,但看起来查询只选择列:

myqry = "SELECT AccResult.StudNo,Exercises.ID from AccResult..."

然后代码尝试显示所有列。请尝试选择所有列:

myqry = "SELECT * from AccResult..."

答案 2 :(得分:0)

试试这个:

myqry = "SELECT AccResult.StudNo,Exercises.ID, Exer1, Exer2, Exer3, Exer4, Exer5 from AccResult INNER JOIN Exercises ON AccResult.StudNo = Exercises.ID ORDER BY AccResult.FirstName,AccResult.YrandSec Asc;"