Named Data Sources

From IronPython Cookbook

Named data sources are setup through the ODBC Data Source Administrator.

The Odbc Data Source Administrator

These named data sources, can either be user for the user or for the system. You access them through the registry. (This not a documented way of accessing them, just the only way we found.)

from Microsoft.Win32 import Registry

def GetDataSources(rootKey, dataSources):
    subKey = rootKey.OpenSubKey("Software\\ODBC\\ODBC.INI\\ODBC Data Sources")
    if subKey:
        for systemDsn in subKey.GetValueNames():
            dataSources.append(systemDsn)


systemDataSources = []
userDataSources = []
GetDataSources(Registry.LocalMachine, systemDataSources)
GetDataSources(Registry.CurrentUser, userDataSources)

This returns a list of the named data-sources, both for the system and for the user.

You can then access the details of these sources:

def GetRegistryValue(rootKey, name):
    return rootKey.OpenSubKey("Software\\ODBC\\ODBC.INI\\%s" % name)


for entry in systemDataSources:
    GetRegistryValue(Registry.LocalMachine, entry)

for entry in systemDataSources:
    GetRegistryValue(Registry.CurrentUser, entry)

GetRegistryValue above, returns a registry Value object. You can inspect this for its properties. For example, to get the server name from the named data source:

value = GetRegistryValue(Registry.LocalMachine, name)
server = value.GetValue("Server")


There is a more direct way you can use them than by inspecting them in the registry. You can use the data-source name as part of the connection string. If you formulate a connection string of the form "Dsn=NAME", then you can use it as a connection string for: Databases with Odbc.


Back to Contents.

TOOLBOX
LANGUAGES