How can I create an ODBC connection from .bat file in Windows?
Sure.
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
Jhonathan
Updated on September 18, 2022Comments
-
Jhonathan almost 2 years
I need a batch script to create an ODBC/DSN connection from a .bat file in Windows. How can I do this?
-
Robert almost 12 years.bat only gives you access to what is at command line, so I don't think it's possible. It should be possible with visual basic though, although I've never done it.
-
Jhonathan almost 12 years@Robert That's possible. Already did.
-
-
Joel Coel almost 12 yearsBe careful: there are two sets of registry keys for odbc connections on 64bit machines.
-
Ansgar Wiechers almost 12 yearsThe registry entries can also be created with
reg.exe
. If you're going to move a file around anyway, it might be better to create a file DSN instead of a system DSN. -
crh225 over 11 yearsworks! I opened ran regedit and exported the current dsn to make a copy to install on other machines.
-
hachi about 10 yearsNote for 32-bit programs running on 64-bit systems the path is
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ODBC\...
.