Load Access Table to Datatable
Loads data from a Microsoft Access table into a DataTable using the specified connection and query parameters.
The function ensures the database connection is open before executing the query. It does not throw exceptions or return error details; any failure will result in an empty table.
VB
Public Function LoadAccessTable(MyConnection As OleDb.OleDbConnection,
TableName As String,
Optional FieldList As String = "*",
Optional WhereCondition As String = "") As DataTable
Dim dtToReturn As New DataTable
Try
If Not MyConnection.State = ConnectionState.Open Then MyConnection.Open()
Dim szQuery As String = $"SELECT {FieldList} FROM {TableName} {WhereCondition}"
Dim MyDataAdapter As New OleDb.OleDbDataAdapter(New OleDb.OleDbCommand(szQuery, MyConnection))
MyDataAdapter.Fill(dtToReturn)
Return dtToReturn
Catch ex As Exception
Return New DataTable
End Try
End Function