9

I need a batch script to create an ODBC/DSN connection from a .bat file in Windows. How can I do this?

2
  • .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.
    – Robert
    Sep 25, 2012 at 23:04
  • @Robert That's possible. Already did.
    – Jhonathan
    Sep 26, 2012 at 14:06

2 Answers 2

11

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
4
  • 4
    Be careful: there are two sets of registry keys for odbc connections on 64bit machines.
    – Joel Coel
    Sep 4, 2012 at 18:44
  • The 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. Sep 4, 2012 at 20:12
  • works! I opened ran regedit and exported the current dsn to make a copy to install on other machines.
    – crh225
    Jan 15, 2013 at 19:05
  • 5
    Note for 32-bit programs running on 64-bit systems the path is HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ODBC\....
    – Nux
    May 6, 2014 at 14:06
0
@Echo Off
cd %systemroot%\system32
call :IsAdmin

SET /P psqlname=PostgreSQL Link datasource ismini  Giriniz : 
reg.exe add "HKLM\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources" /v "%psqlname%" /t REG_SZ /d "PostgreSQL Unicode(x64)" /f
reg.exe add "HKLM\SOFTWARE\ODBC\ODBC.INI\%psqlname%" /v  "Driver" /t REG_SZ /d "C:\\Program Files\\psqlODBC\\1301\\bin\\psqlodbc35w.dll" /f
reg.exe add "HKLM\SOFTWARE\ODBC\ODBC.INI\%psqlname%" /v  "CommLog" /t REG_SZ /d "0" /f

reg.exe add "HKLM\SOFTWARE\ODBC\ODBC.INI\%psqlname%" /v  "Debug" /t REG_SZ /d "0" /f
reg.exe add "HKLM\SOFTWARE\ODBC\ODBC.INI\%psqlname%" /v  "Fetch" /t REG_SZ /d "100" /f
reg.exe add "HKLM\SOFTWARE\ODBC\ODBC.INI\%psqlname%" /v  "UniqueIndex" /t REG_SZ /d "1" /f
reg.exe add "HKLM\SOFTWARE\ODBC\ODBC.INI\%psqlname%" /v  "UseDeclareFetch" /t REG_SZ /d "0" /f
reg.exe add "HKLM\SOFTWARE\ODBC\ODBC.INI\%psqlname%" /v  "UnknownSizes" /t REG_SZ /d "0" /f
reg.exe add "HKLM\SOFTWARE\ODBC\ODBC.INI\%psqlname%" /v  "TextAsLongVarchar" /t REG_SZ /d "1" /f
reg.exe add "HKLM\SOFTWARE\ODBC\ODBC.INI\%psqlname%" /v  "UnknownsAsLongVarchar" /t REG_SZ /d "0" /f
reg.exe add "HKLM\SOFTWARE\ODBC\ODBC.INI\%psqlname%" /v  "BoolsAsChar" /t REG_SZ /d "1" /f
reg.exe add "HKLM\SOFTWARE\ODBC\ODBC.INI\%psqlname%" /v  "Parse" /t REG_SZ /d "0" /f
reg.exe add "HKLM\SOFTWARE\ODBC\ODBC.INI\%psqlname%" /v  "MaxVarcharSize" /t REG_SZ /d "255" /f
reg.exe add "HKLM\SOFTWARE\ODBC\ODBC.INI\%psqlname%" /v  "MaxLongVarcharSize" /t REG_SZ /d "8190" /f
reg.exe add "HKLM\SOFTWARE\ODBC\ODBC.INI\%psqlname%" /v  "ExtraSysTablePrefixes" /t REG_SZ /d "" /f
reg.exe add "HKLM\SOFTWARE\ODBC\ODBC.INI\%psqlname%" /v  "Description" /t REG_SZ /d "" /f
reg.exe add "HKLM\SOFTWARE\ODBC\ODBC.INI\%psqlname%" /v   "UID"        /t REG_SZ /d "postgres" /f
SET /P dbname=Baglanmak istediginiz Postgresql Database name  Giriniz : 
reg.exe add "HKLM\SOFTWARE\ODBC\ODBC.INI\%psqlname%" /v "Database" /t REG_SZ /d "%dbname%" /f
SET /P Username=Baglanmak istediginiz Database Kullanici ismini giriniz : 
reg.exe add "HKLM\SOFTWARE\ODBC\ODBC.INI\%psqlname%" /v "Username"  /t REG_SZ /d "%Username%" /f
SET /P  Password=Baglanmak istediginiz Database Kullanici password bilgisini  giriniz  : 
reg.exe add "HKLM\SOFTWARE\ODBC\ODBC.INI\%psqlname%" /v "Password"  /t  REG_SZ /d "%Password%" /f
SET /P  Servername=Baglanmak istediginiz Database Servername adresini giriniz : 
reg.exe add "HKLM\SOFTWARE\ODBC\ODBC.INI\%psqlname%" /v "Servername" /t REG_SZ /d  "%Servername%" /f
SET /P Port=Portu numarasini giriniz : 
reg.exe add "HKLM\SOFTWARE\ODBC\ODBC.INI\%psqlname%" /v  "Port"  /t  REG_SZ /d      "%Port%"    /f

echo lutfen odbc baglantilarini test edin
Pause & Exit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.