Passing a DataTable by reference, to have a data access method fill it for you can be very costly, especially if you are going against an Access database.
I recently wrote a fairly complex database application against an Access database and I was experiencing a three second performance hit every time I attempted to fill a grid, regardless of the number of rows returned in the DataTable. I discovered that passing a DataTable by reference was causing the problem. Notice the following code example:
| Dim dt As New DataTable Sql = "select * from equipment" Dim iRows As Integer = SendQuery(Sql, dt, ConnectString) |
Public Function SendQuery(ByVal Sql As String, _ ByRef dt As DataTable, _ ByVal ConnectString As String) As Integer ' Returns number of rows affected Dim cmdOLE As New OleDbCommand Dim daOLE As New OleDbDataAdapter If Not MeOpenConnection(ConnectString, ConnOle) Then Return -1 ' can't connect to db End If cmdOLE.CommandText = Sql cmdOLE.CommandType = CommandType.Text cmdOLE.Connection = ConnOle daOLE = New OleDbDataAdapter(cmd_OLE) daOLE.Fill(dt) Return dt.Rows.Count End Function |
| Public Function SendQuery(ByVal Sql As String, _ ByRef dt As DataTable, _ ByVal ConnectString As String) As Integer ' Returns number of rows affected Dim localDT As New DataTable Dim cmdOLE As New OleDbCommand Dim daOLE As New OleDbDataAdapter If Not MeOpenConnection(ConnectString, ConnOle) Then Return -1 ' can't connect to db End If cmdOLE.CommandText = Sql cmdOLE.CommandType = CommandType.Text cmdOLE.Connection = ConnOle daOLE = New OleDbDataAdapter(cmd_OLE) daOLE.Fill(localDT) dt = localDT Return localDT.Rows.Count End Function |