从数据库中读取并填充DataTable [英] Read from database and fill DataTable
本文介绍了从数据库中读取并填充DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我通过 DataReader
获取一组数据并分配给一个字符串.现在我需要用查询字段填充 DataTable
列.DataTable
连接到网格以显示填充的数据.
I'm getting a set of data by a DataReader
and assigning to a string. Now I need to fill the DataTable
columns with the query fields. The DataTable
is connected to a grid to display the filled data.
查询是:
strSQL = "SELECT EmpCode,EmpID,EmpName FROM dbo.Employee
DataTable
列是 EmpCode、EmpID、EmpName
.
我需要读取查询并分配给DataTable
的列并填充表格.我试过如下,但我没有得到正确的输出,
I need to read the query and assign to the columns of DataTable
and fill the table. I have tried as below but i dont get the proper output,
Me.DtShifts.Tables("NonAllocated").Clear()
Me.DtShifts.Tables("NonAllocated").Load(dr)
推荐答案
Connection 对象仅用于说明.DataAdapter 是关键位:
Connection object is for illustration only. The DataAdapter is the key bit:
Dim strSql As String = "SELECT EmpCode,EmpID,EmpName FROM dbo.Employee"
Dim dtb As New DataTable
Using cnn As New SqlConnection(connectionString)
cnn.Open()
Using dad As New SqlDataAdapter(strSql, cnn)
dad.Fill(dtb)
End Using
cnn.Close()
End Using
这篇关于从数据库中读取并填充DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文