DatagridView从ms-access 2不同的表

时间:2018-04-06 10:44:53

标签: vb.net datagridview

我有2 ms-access table这样的话:

student                     library
----------------------      ----------------------
|ID  | Name | Address |     |s_ID   | book | date |
|10  | John | add1    |     | 20    | book1| 03   |
|20  | Joe  | add2    |     | 30    | book2| 05   |
|30  | User3| add3    |     ----------------------

所以我想在一个datagridview中使用这样的所有数据:

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

我不知道该怎么做。

我怎么能得到它?还是其他任何方法?

感谢任何帮助。谢谢

1 个答案:

答案 0 :(得分:1)

试一试

  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