I create user system dsn entries by using a batch file and odbcconf call. I've found references here on how to do that, yet cannot seem to find any results on how to remove them using that same call. The library information (http://msdn.microsoft.com/en-us/library/ee388579(VS.85).aspx) states add and modify a system dsn for configdsn and configsysdsn and references equivalent to SQLConfgDataSource function. (http://msdn.microsoft.com/en-us/library/ms716476(VS.85).aspx) but the config data source function has a a removal and this doesn't seem too.
Apologies for being obtuse, but I can't seem to locate it. Any links to how this is done from a batch file are much appreciated.
If you do find a way through odbcconf, please post it or email me, so we can update the documentation. The only way I know to do it is to delete the registry key for the DSN:
Hope that helps, and if you do find a way through odbcconf -- please do email me and post the method on the forum!
This post is provided 'as is' and confers no express or implied warranties or rights.
- Proposed as answer by John C GordonMicrosoft employee, Moderator Monday, October 05, 2009 8:03 PM
John C is basically correct, you can remove a System ODBC data source via the registry, but there is apparently no way via odbcconf.exe. That is of course really stupid and poor programming!
John, however, forgot another important registry entry. That key deletion he mentioned will disable the odbc connection, but not remove it entirely. If you just do that and then open the "ODBC Data Source Administrator", you will find the connection is still listed, but you can't remove it or modify it. It becomes completely screwed up! There is a value to delete as well which dictates if the connection is listed. Consider it the "header" to the data source.
Also, in Windows 2003 and 2008 at least, the registry key John C listed is not quite the location to modify either. This is where to find them in those OSs:
The "header" I reffered to is a string value in inside a key (not the whole key!)
HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ODBC\ODBC.INI\ODBC Data Sources
The string value is the data source name. This entry must be removed to remove the data source listing entirely.
Here are some batch snipets for creating and then removing an odbc data source. I'm leaving out some premiliary chunks, but you can figure it out from here. If you can't, you probably shouldn't be messing with this stuff in the first place!
In these scripts, I create and remove a 32-bit data source in either a 32-bit or 64-bit version of windows. The windows folder variable gets set to either "C:\Windows\System32" or "C:\Windows\SysWOW64" depending on the os. In case you didn't know, on a 64-bit machine there is are also 2 differrent versions of the "ODBC Data Source Administrator" gui tool and the data source lists differ (32-bit vs 64-bit lists). The 32-bit version on a 64 bit OS is found at "C:\Windows\SysWOW64\odbcad32.exe". The one in the start menu will load the 64-bit version so you will never find your 32-bit connections there.
Note - for simplicity I use the database name as the user name and and the data source name as well (in case there was any confusion).
Create the connection like so:
echo Creating 32-Bit System ODBC Connection "%DatabaseName%"... "!WindowsFolder!\ODBCCONF.EXE" CONFIGSYSDSN "!ODBCDriver!" "DSN=%DatabaseName%;Server=localhost;Port=3306;Database=%DatabaseName%;UID=%DatabaseName%;PWD=%DatabasePassword%"
And then remove it like this:
echo Removing 32-Bit System ODBC Connection "%DatabaseName%"... if "%WindowsBits%"=="32" ( If Exist "!TempRegFile!" Del "!TempRegFile!" echo Windows Registry Editor Version 5.00>>"!TempRegFile!" echo.>>"!TempRegFile!" echo [-HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\%DatabaseName%]>>"!TempRegFile!" echo [HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources]>>"!TempRegFile!" echo "%DatabaseName%"=->>"!TempRegFile!" regedit /s "!TempRegFile!" ) else ( If Exist "!TempRegFile!" Del "!TempRegFile!" echo Windows Registry Editor Version 5.00>>"!TempRegFile!" echo.>>"!TempRegFile!" echo [-HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ODBC\ODBC.INI\%DatabaseName%]>>"!TempRegFile!" echo [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ODBC\ODBC.INI\ODBC Data Sources]>>"!TempRegFile!" echo "%DatabaseName%"=->>"!TempRegFile!" regedit /s "!TempRegFile!" ) If Exist "!TempRegFile!" Del "!TempRegFile!"
- Proposed as answer by Buvin Tech Thursday, February 10, 2011 9:08 PM