Getting Names of System Data Sources in Windows

Hi,
How can I get the names of the System Data Sources in Windows from my C# code

Thank you.

Shakeel


Answer this question

Getting Names of System Data Sources in Windows

  • norsd

    How do you mean Do you want to iterate over SQL Server instances, all databases, or something else

  • adam kromm

    using System.Data.Common;

    using System.Data;

    public class GetName

    {

    public static void Main()

    {

    DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");

    DataTable tables = null;

    using (DbConnection connection = factory.CreateConnection())

    {

    connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\mydb.mdb;Jet OLEDB:Database Password=MyDbPassword;";

    string[] restrictions = null;

    connection.Open();

    tables = connection.GetSchema("Tables", restrictions);

    foreach (DataRow row in tables.Rows)

    {

    for (int i = 0; i < tables.Columns.Count; ++i)

    System.Console.WriteLine(rowIdea.ToString());

    System.Console.WriteLine();

    }

    }

    }

    }


  • BatesManty

    Thanks Karthik. This solves the problem.
    Cheers to you man! :D

    Would you also know how to get a list of all the table names from an MS Access database using C#

    Thanks again.

    Shakeel
    :)

  • Lenitcha

    Shakeel,

    Can you kindly mark the post as an answer


  • chris_dk

    If you are looking for the list of System DSN's then they are stored in

    HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources

    Here is the code for accessing them-

    static class Program

    {

    /// <summary>

    /// The main entry point for the application.

    /// </summary>

    [STAThread]

    static void Main()

    {

    RegistryKey localMachineKey = Registry.LocalMachine;

    RegistryKey key = localMachineKey.OpenSubKey(@"SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources");

    GetODBCS(key);

     

    private static void GetODBCS(RegistryKey key)

    {

    string[] odbcs = key.GetValueNames();//Gives you all the names of system DSN's

    foreach(string valuename in key.GetValueNames())

    {

    MessageBox.Show(valuename + " " + key.GetValue(valuename).ToString());

    Now,examine DBQ to get the name of the database

    }

    }

    }

     

    Hope this helps


  • dafan

    When you create a database, e.g. in MS Access you can set it as an ODBC data source on the System level. I want to get the names of all databases that have been set as System Data Sources from my C# code. Hope that explains it. :)

    Shakeel

  • Getting Names of System Data Sources in Windows