At my job, most of our data revolves around a single ODBC datasource — however, we have hundreds of VBA macros that are polluted with thousands of explicit instances of ADODB connection & recordset objects (all connected via the same DSN).
There is quite a bit of code that I’d like to refactor that’s currently in the form of:
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
cn.Open "MYDSN"
SQL = "SELECT * FROM [...]"
rs.Open SQL, cn
' utilize rs ...
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
I’m imagining a reusable class dll, probably written in C#.NET that would provide a simple interface to accomplish the above more concisely.
My question is, how would you set out to design this class? I’m fairly new to C#, but would like to avoid leaving the same mess behind for the next person.
Thanks in advance!
Applying the good old KISS principle – create one static method
CreateRecordset(string SQL)that reads the DSN from configuration, opens the connection and return the recordset object for the given SQL.