data sources using C#

  • Thread starter Thread starter abcd
  • Start date Start date
A

abcd

I want to get the list of data sources on the server machine using C# and
ASP.NET
any clues how to get that...I have done this using VB using SQLDataSources
function from ODBC32.dll

thanks
 
Hi,

I don;t know of any managed way of doing that , but you can p/invoke it.

cheers,
 
Here is the code that I use to get a SortedList of all the DSN names
from the registry...

<code>
/// <summary>
/// Reads the local user/system DSN registry entries and returns a
SortedList of Data Source Names.
/// </summary>
/// <remarks>In the event that a DSN exists in both the User and System
registries,
/// the User DSN takes precedence over the System DSN.</remarks>
/// <returns>The SortedList of Data Source Names</returns>
/// <seealso cref="System.Collections.SortedList"></seealso>
public static SortedList listAllDSN()
{
SortedList allDSN = new SortedList();

// Get User DNS Names
RegistryKey reg = (Registry.CurrentUser).OpenSubKey("Software");
reg = reg.OpenSubKey("ODBC");
reg = reg.OpenSubKey("ODBC.INI");
reg = reg.OpenSubKey("ODBC Data Sources");

if (reg != null)
{
// Get all DSN entries defined in DSN_LOC_IN_REGISTRY.
foreach ( string s in reg.GetValueNames() )
{
allDSN.Add( s , null );
}
}
try
{
reg.Close();
}
catch ( System.Exception ex )
{
}

// Get System DNS Names
reg = (Registry.LocalMachine).OpenSubKey("Software");
reg = reg.OpenSubKey("ODBC");
reg = reg.OpenSubKey("ODBC.INI");
reg = reg.OpenSubKey("ODBC Data Sources");

if (reg != null)
{
// Get all DSN entries defined in DSN_LOC_IN_REGISTRY.
foreach ( string s in reg.GetValueNames() )
{
try
{
allDSN.Add( s , null );
}
catch ( System.Exception ex )
{
}
}
}
try
{
reg.Close();
}
catch ( System.Exception ex )
{
}

return allDSN ;
}
</code>

Joel
 
Back
Top