AndrewdAzotus
Member
- Joined
- Aug 8, 2012
- Messages
- 22
- Programming Experience
- 10+
This post is as a consequence of the following quote from jmcilhinney
So I began thinking that it must be more efficient (performance wise) to open the connection to the database and just leave it open, so I wrote this:
and I ran it five times modifying the code a little each time and got the following results.
I admit that the last is, perhaps, a little odd but I wanted to see the timings, then I thought I would post them here just for interest.
The three lines with [#] comments at the end were inserted since the SQL table had fifteen rows and the Access table had 64 rows so I was trying to even the code out a little.
It's not a case of can you run them faster since I am not going to publish any details of the system I'm running.
But the results make an interesting comparison...
DO NOT create your connection object separately to your command object. You should be create your connection object where you create your command. There is no benefit to creating a single connection object in a module. ADO.NET is designed specifically to NOT be used like that. Create the connection and the command together, open the connection, execute the command, close the connection. That is the proper way to use ADO.NET.
So I began thinking that it must be more efficient (performance wise) to open the connection to the database and just leave it open, so I wrote this:
VB.NET:
Dim TableRead As SqlDataReader ' OleDbDataReader Dim Cxn As New SqlConnection ' OleDbConnection()
Dim CxnOpen As Boolean = True
Dim Lp1 As Integer
Dim StartedAt As DateTime
Dim t1 As TimeSpan
Dim idx As Integer
Dim Descr As String
Dim MasterAssetList As New Dictionary(Of Integer, String)
StartedAt = Now()
For Lp1 = 1 To 10000
' Cxn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=C:\[Database]\Data.mdb")
Cxn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=""C:\[Database]\Data.mdf"";Integrated Security=True;Connect Timeout=30;User Instance=True"
' Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccess2007file.accdb;Persist Security Info=False;
Try
Cxn.Open()
Catch ex As Exception
MessageBox.Show("Failed to connect to data source")
CxnOpen = False
End Try
If CxnOpen Then
MasterAssetList.Clear()
' TableRead = New OleDbCommand("Select * from AcsTbl", Cxn).ExecuteReader
TableRead = New SqlCommand("Select * from SQLTbl", Cxn).ExecuteReader
While TableRead.Read
idx = TableRead("Idx")
Descr = TableRead("NameGiven")
MasterAssetList.Add(idx, Descr)
MasterAssetList.Add(100 + idx, Descr) ' [#]
MasterAssetList.Add(200 + idx, Descr) ' [#]
MasterAssetList.Add(300 + idx, Descr) ' [#]
End While
TableRead.Close()
End If
Cxn.Close()
Next
t1 = Now - StartedAt
Msgbox("It Took " & t1)
and I ran it five times modifying the code a little each time and got the following results.
Database Access | Time |
Access (mdb) file opening and closing connection inside the loop | 7m29s |
Access (mdb) file opening and closing connection outside the loop | 0m15s |
SQL Server opening and closing connection inside the loop | 0m10s |
SQL Server opening and closing connection outside the loop | 0m04.06s |
SQL Server opening connection in settings.vb and closing connection in MyApplication_Shutdown | 0m03.55s |
I admit that the last is, perhaps, a little odd but I wanted to see the timings, then I thought I would post them here just for interest.
The three lines with [#] comments at the end were inserted since the SQL table had fifteen rows and the Access table had 64 rows so I was trying to even the code out a little.
It's not a case of can you run them faster since I am not going to publish any details of the system I'm running.
But the results make an interesting comparison...