I need a batch script to create an ODBC/DSN connection from a .bat file in Windows. How can I do this?
Manually create an ODBC connection on your PC using the "Data Sources (ODBC)" applet in Admin tools.
Launch regedit and go to HKLM\Software\ODBC and export the whole lot from ODBC down to a .reg file. This is a right click option on the folder.
Open the exported file in Notepad now you need to delete everything that doesn't refer to your particular ODBC connection.
You should end up with a registry file looking something like this
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources] "TEST"="SQL Server" [HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\TEST] "Driver"="C:\\Windows\\system32\\SQLSRV32.dll" "Server"="SERVERNAME" "LastUser"="USERNAME" "Trusted_Connection"="Yes"
It may be slightly different depending on connection settings and such like. You will need the two sections, the data sources defines the type of connection, in this instance a SQL server one, then the bit that tells you what it is called, which driver to use and what sort of authentication to use. this will all be done when you set up the connection, you just need the .reg file with it in.
You can then import it on any pc by either double clicking the reg file or if you have the facility run a script to import it using regedit. You can call this import from a batch file:
regedit /s FileToImport.reg