如何从具有条件的五个表中选择记录并将记录读取到datagridview

时间:2016-03-09 03:59:02

标签: vb.net datagridview

我创建了一个包含5个表的Access数据库(personalData,WorkExperience,EducationalData,SpouseData,DependantData)

我在第一个表中使用STHN_ID作为主键,在其他表中使用外键。我还有一个名为DateEngaged的列作为日期/时间。

我尝试从PersonalData中选择STHN_ID = 00001且DateEngaged在某个日期范围内并且工作正常。这是我的代码

Dim rcmd As OleDbCommand = New OleDbCommand("Select STHN_ID FROM PersonalData WHERE ([STHN_ID]='" & STHN_ID.Text & "') and ([DateEngaged]>=CDate('" & FromDate.Value & "') AND [DateEngaged] <=CDate('" & ToDate.Value & "'))", myConnection)

我的问题是在从PersonalData读取这些条件并找到记录之后,它应该继续并选择其他表中具有该STHN_ID的所有字段,也就是说,它应该组合所有表中的所有列。我想做的最后一件事是将组合列读取到datagridview。

因此在datagridview中,我拥有PersonalData,WorkExperience,EducationalData,SpouseData和Dependent数据中的所有列。所以datagridview中的一行将包含从第一个表到最后一个表的特定记录。

这是我尝试的但却出错了

da = New OleDbDataAdapter("Select * FROM [PersonalData] WHERE [STHN_ID]='" & STHNID.Text & "' AND ([DateEngaged]>=CDate('" & FromDate.Value & "') AND [DateEngaged]<=CDate('" & ToDate.Value & "' JOIN select * from EducationalData where STHN_ID='" & STHNID.Text & "' JOIN select * from WorkExperience where STHN_ID='" & STHNID.Text & "' JOIN select * from SpouseData where STHN_ID='" & STHNID.Text & "' JOIN select * from DependantData where STHN_ID='" & STHNID.Text & "'))", myConnection)

1 个答案:

答案 0 :(得分:0)

在dataAdapter中使用此查询:

select PD.STHN_ID
    , PD.NAME
    , PD.DateEngaged
    , ED.NAMEOFSCHOOL
    , WE.EMPLOYER
    , SD.SPOUSENAME
    , DD.DEPENDANTNAME
from PersonalData PD
inner join EducationalData ED on PD.STHN_ID=ED.STHN_ID
inner join SPOUSEDATA SD on PD.STHN_ID=SD.STHN_ID
inner join DEPENDANTDATA DD on PD.STHN_ID=DD.STHN_ID
inner join WORKEXPERIENCE WE on PD.STHN_ID=WE.STHN_ID
WHERE ([PD.STHN_ID]='" & STHN_ID.Text & "')
    and ([PD.DateEngaged]>=CDate('" & FromDate.Value & "') 
    and [PD.DateEngaged] <=CDate('" & ToDate.Value & "'))