8

我通过 a 获取一组数据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)
4

2 回答 2

27

连接对象仅用于说明。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
于 2012-06-01T07:00:12.410 回答
-1
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
于 2018-06-19T19:35:33.637 回答