Create an ODBC connection from VS2005 setup
I am developing an application in the medical field with an Sql Server database ( let’s name it “MyDB” ) . Recently some of our clients have been asking for a way to easily export the data from our database in order to be used into some other analysis oriented tool.
That would have been easily achieved through an ODBC connection in the created into the Control Panel. I looked up for an easy way to create the ODBC connection through the Visual Studio 2005 Setup project. It turned out to be as easy as that. All you have to do is create registry entries for your ODBC connection. In my case, I’ve created a new System DSN and afterwards just copied the registry entries into my own setup project, I erased the DSN I’ve manually created and then I’ve executed the setup kit to test whether it works or not. Everything went just fine …
Let me give an example for a connection that requires an SqlServer driver.
In the Setup Project, open the registry view and browse to the key HKEY_LOCAL_MACHINE\Software. Under this key you will have to create the ODBC.INI key. Then, under this key, you will create a key with your database name, in our case that would be MyDB. Here is how an export from the registry editor would look like, for the database name key :
[HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI]
[HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\MyDB]
“Description”=”My database”
“Default”=”"
“StatsLog_On”=”Yes”
“Driver”=”C:\\Windows\\system32\\SQLSRV32.dll”
“Trusted_Connection”=”Yes”
“Server”=”(local)”
“LastUser”=”"
“Database”=”MyDB”.
You can easily copy these entries into your setup project under the database name ( “MyDB” ) key I’ve mentioned before.
Now, you must get back to the upper level ODBC.INI key and under it, create the key :”ODBC Data Sources”. You will then set the key : “MyDB” with the string value “SqlServer”. Here’s how the export looks like :
[HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources]
“MyDB”=”SQL Server”
So, now you have created, into your setup project, the registry entries you need in order to automatically create an ODBC connection upon application setup. If you want to test this, erase ODBC entry from your ODBC administrator tool in Control Panel, build up your kit, and when the setup finishes, check the registry editor again and view the entries right where they were before. Now, check the ODBC administrator tool and notice the ODBC entry.
On the other hand, when you need to uninstall the application, there is no need for you to do anything special as the uninstall process will remove the registry entries for you.
Please note that the example I’ve given works with an Sql Server database. For other types, you have to repeat the same procedure, because the registry keys will not be the same as in this case, for, let’s say, a Microsoft Access database.
-
Archives
- May 2009 (1)
- April 2009 (4)
- February 2008 (1)
- November 2007 (1)
- August 2007 (1)
- July 2007 (2)
- June 2007 (2)
- May 2007 (1)
-
Categories
-
RSS
Entries RSS
Comments RSS