我通过DataReader
获取一组数据,并将其赋值给一个字符串。现在我需要用查询字段填充DataTable
列。DataTable
连接到网格以显示填充的数据。
查询为:
strSQL = "SELECT EmpCode,EmpID,EmpName FROM dbo.Employee
DataTable
列是EmpCode, EmpID, EmpName
。
我需要读取查询并将其赋值给DataTable
的列,然后填充表。我已经尝试了如下,但我没有得到正确的输出,
Me.DtShifts.Tables("NonAllocated").Clear()
Me.DtShifts.Tables("NonAllocated").Load(dr)
发布于 2012-06-01 15:00:12
Connection对象仅用于说明。DataAdapter是关键位:
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
发布于 2018-06-20 03:35:34
Private Function LoaderData(ByVal strSql As String) As DataTable
Dim cnn As SqlConnection
Dim dad As SqlDataAdapter
Dim dtb As New DataTable
cnn = New SqlConnection(My.Settings.mySqlConnectionString)
Try
cnn.Open()
dad = New SqlDataAdapter(strSql, cnn)
dad.Fill(dtb)
cnn.Close()
dad.Dispose()
Catch ex As Exception
cnn.Close()
MsgBox(ex.Message)
End Try
Return dtb
End Function
https://stackoverflow.com/questions/10844358
复制相似问题