1

我想实现一个“smartODBCLogin”。我知道如何从注册表中获取所有 ODBC 数据源中的所有引擎名称和驱动程序的列表。

现在我想知道,这些来源中哪些是可用的(活动的)。(打开一个新的 odbcConnection 不起作用/帮助,会花费很多时间。)

有人知道如何在 c# 中实现它吗?

4

1 回答 1

1

没有用于此的 .NET API(据我所知),但有用于此的本机 ODBC API,在此处的 Microsoft 站点上有详细说明。还有一个很好的代码示例,可以帮助您在这里列出用户和系统 DSN。CodeProject 上还有一个代码示例,看起来像是获取了驱动程序列表

如果您赶时间,这里是第一篇文章示例(用于获取用户和系统 DSN),它们的所有抄袭荣耀:

获取系统 DSN:

/// <summary>
/// Gets all System data source names for the local machine.
/// </summary>
public System.Collections.SortedList GetSystemDataSourceNames()
{
    System.Collections.SortedList dsnList = new System.Collections.SortedList();

    // get system dsn's
    Microsoft.Win32.RegistryKey reg = (Microsoft.Win32.Registry.LocalMachine).OpenSubKey("Software");
    if (reg != null)
    {
        reg = reg.OpenSubKey("ODBC");
        if (reg != null)
        {
            reg = reg.OpenSubKey("ODBC.INI");
            if (reg != null)
            {
                reg = reg.OpenSubKey("ODBC Data Sources");
                if (reg != null)
                {
                    // Get all DSN entries defined in DSN_LOC_IN_REGISTRY.
                    foreach (string sName in reg.GetValueNames())
                    {
                        dsnList.Add(sName, DataSourceType.System);
                    }
                }
                try
                {
                    reg.Close();
                }
                catch { /* ignore this exception if we couldn't close */ }
            }
        }
    }

    return dsnList;
}

获取用户 DSN:

/// <summary>
/// Gets all User data source names for the local machine.
/// </summary>
public System.Collections.SortedList GetUserDataSourceNames()
{
    System.Collections.SortedList dsnList = new System.Collections.SortedList();

    // get user dsn's
    Microsoft.Win32.RegistryKey reg = (Microsoft.Win32.Registry.CurrentUser).OpenSubKey("Software");
    if (reg != null)
    {
        reg = reg.OpenSubKey("ODBC");
        if (reg != null)
        {
            reg = reg.OpenSubKey("ODBC.INI");
            if (reg != null)
            {
                reg = reg.OpenSubKey("ODBC Data Sources");
                if (reg != null)
                {
                    // Get all DSN entries defined in DSN_LOC_IN_REGISTRY.
                    foreach (string sName in reg.GetValueNames())
                    {
                        dsnList.Add(sName, DataSourceType.User);
                    }
                }
                try
                {
                    reg.Close();
                }
                catch { /* ignore this exception if we couldn't close */ }
            }
        }
    }

    return dsnList;
}

获取所有 DSN:

// Returns a list of data source names from the local machine.
public System.Collections.SortedList GetAllDataSourceNames()
{
    // Get the list of user DSN's first.
    System.Collections.SortedList dsnList = GetUserDataSourceNames();

    // Get list of System DSN's and add them to the first list.
    System.Collections.SortedList systemDsnList = GetSystemDataSourceNames();
    for (int i = 0; i < systemDsnList.Count; i++)
    {
        string sName = systemDsnList.GetKey(i) as string;
        DataSourceType type = (DataSourceType)systemDsnList.GetByIndex(i);
        try
        {
            // This dsn to the master list
            dsnList.Add(sName, type);
        }
        catch 
        { 
            // An exception can be thrown if the key being added is a duplicate so 
            // we just catch it here and have to ignore it.
        }
    }

    return dsnList;
}

将它们绑定到组合框:

// fill data source names
DevToolShed.OdbcDataSourceManager dsnManager = new DevToolShed.OdbcDataSourceManager();
System.Collections.SortedList dsnList = dsnManager.GetAllDataSourceNames();
for (int i = 0; i < dsnList.Count; i++)
{
    string sName = (string)dsnList.GetKey(i);
    DevToolShed.DataSourceType type = (DevToolShed.DataSourceType)dsnList.GetByIndex(i);
    cbxDataSourceName.Items.Add(sName + " - (" + type.ToString() + " DSN)");
}

完整的源代码可在上面的链接中找到。

于 2009-09-16T14:03:02.993 回答