vb2010 oledb选择选择结果的位置

时间:2014-09-30 16:22:58

标签: sql vb.net oledb

我处理oledb的一个小问题。 我有两个excel表: 一个包括系统和负责人

    [person1|system1]
    [person1|system2]
    [person1|system3]
    [person2|system4]
    [person2|system5]
    ...

以及有关系统的其他信息

    [system1|location|weight|height]
    [system2|location|weight|height]
    ...

现在我希望能够选择一个人并将系统信息(对于此人的系统)写入datagridview

顺便说一下:当我从其他地方拿到桌子时,我无法改变桌子。

到目前为止我的代码:

    Using cn As New System.Data.OleDb.OleDbConnection
        Dim Builder As New OleDbConnectionStringBuilder With _
            { _
                .DataSource = "reports\PM.xlsx", _
                .Provider = "Microsoft.ACE.OLEDB.12.0" _
            }
        Builder.Add("Extended Properties", "Excel 12.0;")
        cn.ConnectionString = Builder.ConnectionString

        cn.Open()

        Using cmd As OleDbCommand = New OleDbCommand With {.Connection = cn}
            cmd.CommandText = "SELECT F2 AS system FROM [table1$] WHERE F1 ='" & ComboBox2.SelectedItem & "'"
            Dim dr As System.Data.IDataReader = cmd.ExecuteReader

            dtSheet1.Load(dr)
            DataGridView1.DataSource = dtSheet1
        End Using
    End Using

到目前为止,Thatworks完美,我收到了datagridview中的所有系统名称,但我不知道如何从第一个结果中选择另一个表。 我需要像

这样的东西
     SELECT F2 AS location, F3 AS weight, F4 AS hight FROM [table2$] WHERE F1 =ONE OF THE RESULTS FROM THE QUERY ABOVE'"

提前致谢,我真的很感谢你的帮助!!

1 个答案:

答案 0 :(得分:0)

您可以使用原始查询作为条件加入两个表中的数据:

SELECT t2.F2 AS location, t2.F3 AS weight, t2.F4 AS hight 
FROM [table2$] as t2 
  JOIN [table1$] as t1 
    ON t2.f1 = t1.f1 
WHERE t1.F1 ='" & ComboBox2.SelectedItem & "'"