Reading from a database and populating a DataTable

I get a dataset using DataReaderand assigning a row. Now I need to fill the columns with DataTablequery fields. DataTableconnects to the grid to display filled data.

:

strSQL = "SELECT EmpCode,EmpID,EmpName FROM dbo.Employee

DataTablethe columns EmpCode, EmpID, EmpName.

I need to read a query and assign to columns DataTableand populate a table. I tried as below, but I am not getting the correct output,

Me.DtShifts.Tables("NonAllocated").Clear()
Me.DtShifts.Tables("NonAllocated").Load(dr)
+5
source share
1 answer

The connection object is for illustration purposes only. DataAdapter - 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
+21
source

All Articles