Reputation: 103
I'm trying to load configuration settings into a set of DataTables and then disconnect from the database. I'm sure I am doing something wrong as the DataTables are empty afterwards. Here is the code:
From the top of the class:
'Set up Configuration DataTables
Dim dtDataAlerts As New DataTable
Running the Routine to get the data:
Public Sub ReadDataAlerts()
'Read the configuration for the Data Alerts from the RDB
dtDataAlerts = GetRecordSet("SELECT DataAlertGroups.DataAlertGroup_UID, DataAlertGroups.EmailGroup_UID, DataAlertGroups.EmailTemplate_UID, EmailTemplates.EmailTemplate_CustomSubject, EmailTemplates.EmailTemplate_CustomMessage FROM (DataAlertGroups INNER JOIN EmailGroups ON DataAlertGroups.EmailGroup_UID=EmailGroups.EmailGroup_UID) INNER JOIN EmailTemplates ON DataAlertGroups.EmailTemplate_UID=EmailTemplates.EmailTemplate_UID")
End Sub
The routine to get the DataTable
Function GetRecordSet(SQLString As String) As DataTable
'function used to run a query and return a disconnected DataTable
Try
'Create Dataset, Open Connection
Dim dsPWC As New DataSet()
Dim OleDbDataAdapter1 As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter(SQLString, sqlConn)
sqlConn.Open()
'Fill the Dataset with the PlantWatchConfiguration Table
OleDbDataAdapter1.Fill(dsPWC, "CollectorAlertGroups")
'Create Table from Dataset and iterate data
Dim DT As New DataTable
Dim DTClone As New DataTable
DT = dsPWC.Tables(0)
DTClone = DT.Clone
'Close Connection
sqlConn.Close()
Return DTClone
Catch ex As Exception
WriteToLog(ex.Message, "GetRecordSet")
End Try
End Function
I know the SQL is correct as I have already run it against the RDB and it produces results. I have a ton of logging to file in my code, so I am sure the database is connecting properly. (And other direct queries pull back data just fine)
But I am doing something wrong on the GetRecordSet function. I'm thinking that I am not properly cloning and disconnecting?
Anyway... I'm sure this is simple since I am not a coding expert :)
Thanks.
Upvotes: 0
Views: 836
Reputation: 216302
The error is exactly the Clone method. It clones the datatable structure, not the data.
DataTable.Clone: Clones the structure of the DataTable, including all DataTable schemas and constraints.
If you want the data you need to use the Copy method.
DataTable.Copy: Copies both the structure and data for this DataTable.
However, as I have said in my comment, you don't need to waste the memory of your PC in this way. ADO.NET is based on the disconnected model. The datatables present in the Dataset filled by the adapter are already disconnected from the database and you could return directly the table at index zero without any useless copy/clone.
Upvotes: 1