DataReader 傳資料給 DataGridView
Dim cn As New SqlConnection("server=wk-db;database=northwind;user id=sa;password=sa")
Dim cmd As New SqlCommand("select * from products", cn)
cn.Open()
Dim dt As New DataTable
Dim dr As SqlDataReader = cmd.ExecuteReader
For i As Integer = 0 To dr.FieldCount - 1
Dim col As New DataColumn(dr.GetName(i), dr.GetFieldType(i))
dt.Columns.Add(col)
Next
Do While dr.Read
Dim r As DataRow = dt.NewRow
For i As Integer = 0 To dr.FieldCount - 1
r(i) = dr(i)
Next
dt.Rows.Add(r)
Loop
DataGridView1.DataSource = dt
沒有留言:
張貼留言