Vb.net DatagridView从ms access 2不同的表获取数据
我有一个Vb.net DatagridView从ms access 2不同的表获取数据,vb.net,datagridview,Vb.net,Datagridview,我有一个2ms的访问表,如下所示: student library ---------------------- ---------------------- |ID | Name | Address | |s_ID | book | date | |10 | John | add1 | | 20 | book1| 03 | |20 | Joe | add2 | | 30 | boo
2ms的访问表
,如下所示:
student library
---------------------- ----------------------
|ID | Name | Address | |s_ID | book | date |
|10 | John | add1 | | 20 | book1| 03 |
|20 | Joe | add2 | | 30 | book2| 05 |
|30 | User3| add3 | ----------------------
datagridview1
----------------------------------
|S.N | ID | Name | book | date |
| 1 | 10 | John | - | - |
| 2 | 20 | Joe | book1 | 03 |
| 3 | 30 | User3| book2 | 05 |
所以我希望所有数据都在一个datagridview
中,如下所示:
student library
---------------------- ----------------------
|ID | Name | Address | |s_ID | book | date |
|10 | John | add1 | | 20 | book1| 03 |
|20 | Joe | add2 | | 30 | book2| 05 |
|30 | User3| add3 | ----------------------
datagridview1
----------------------------------
|S.N | ID | Name | book | date |
| 1 | 10 | John | - | - |
| 2 | 20 | Joe | book1 | 03 |
| 3 | 30 | User3| book2 | 05 |
我试过这个:
dim i as Integer = 1
sql = "SELECT ID, Name FROM student"
cmd = New OleDbCommand(sql, connection)
reader = cmd.ExecuteReader()
If Not reader.HasRows Then
DataGridView1.Rows().Clear()
ElseIf reader.HasRows Then
Do While reader.Read
Dim row() As String = {CStr(i), reader.Item(0), reader.Item(1),"",""}
DataGridView1.Rows.Add(row)
i += 1
Loop
End If
我不知道该怎么做
我怎么能得到这个?还是其他方法
感谢您的帮助。谢谢试试这个
Dim sql As String = "select row_number() over (order by ID) as S.N, ID, Name, book, date from student left outer join library on id=s_id"
Dim dataadapter As New SqlDataAdapter(sql, connection)
Dim ds As New DataSet()
connection.Open()
dataadapter.Fill(ds, "ID")
connection.Close()
DataGridView1.DataSource = ds
我想从两个不同的sql
选择命令获取这些数据,并将它们连接到Datagridview
中。你能帮我怎么做吗?你的意思是说两个sql命令被uded了?就像从表1中选择*从表2中选择*是吗?是的!如从表1中选择*并将这些数据插入datagridview
中,如从表2中选择*并插入datagridview
中。我不知道这样在datagridview单元格中插入是否可行。@PradipDhakal,你的建议是不可行的;没有任何意义。如果要填充一个DataGridView
,则只能有一个数据源,即一个DataTable
。如果您想从两个数据库表中填充一个DataTable
,那么您可以通过一个包含联接的查询来完成。已经向你展示了如何做到这一点。现在去做。如果你只是想知道,那么你应该说你只是想知道,而不是说你想用另一种方式去做。如果有人给了你一个非常好的方法,你为什么要用不同的方法去做呢?为什么即使通过其他方法也能做到这一点也很重要?除非另一种方法特别好,否则有什么意义?