Enumerate SQL Server Instances in C#, Using ODBC

Enumerate SQL Server Instances in C#, Using ODBC


Introduction
this article describes a C# class that utilises ODBC (SQLBrowseConnect) to obtain a list of SQL Servers on a network and returns an array of instances. If an instance is supplied with a valid username/password, then a list of all the databases on the instance is returned. 
Background
i needed a way to obtain a list of SQL Server instances without using SQLDMO. I found a C  implementation of the SQLBrowseConnect by Santosh Rao. This is a C# implementation. Further information can be found at: 


SQLBrowseConnect Function


Using Win32 and Other Libraries





Download demo project - 6.61 Kb


Download source - 9.08 Kb



 
Using the code
in order for the code to work, ODBC must be installed on your machine. Nearly all Microsoft Operating Systems have this installed. I have only tested this on Windows 2000 and XP. In order to use this, add a reference to the sqlenumerator.cs file. The class is in the Moletrator.SQLDocumentor namespace and is SQLInfoEnumerator. The important work is done in the RetrieveInformation method. This calls the relevant ODBC commands passing in the relevant values. The important item is the value of inputParam. If this is blank then nothing is returned. When it contains a valid driver (DRIVER=SQL SERVER for MS SQL Server), it will check for all instances of this driver on the network returning a string value which is then parsed. 
if this string is expanded to include a valid SQL Server instance and a valid username/password, then a list of all the databases on the server instance is returned. If the username/password are not valid then the a list of SQL Server instances is returned: 

In order to get a list of SQL Server instances, create an instance of the class and call EnumerateSQLServers. The example below adds the list of SQL Servers to a list box SQLListBox. SQLInfoEnumerator sie = new SQLInfoEnumerator();
SQLListBox.Items.AddRange(sie.EnumerateSQLServers());
To get a list of databases on a SQL Server instance, use the code below. The SQL Server instance is the selected instance from the list box populated in sample A. The username/password are entered by the user. SQLInfoEnumerator sie = new SQLInfoEnumerator();
sie.SQLServer = listboxSQLServerInstances.SelectedItem.ToString();
sie.Username = textboxUserName.Text;
sie.Password = textboxPassword.Text;
SQLListBox.Items.AddRange(sie.EnumerateSQLServersDatabases());
the demo code contains a full GUI with an example on how to call each of the above methods.
 
History
18 April 2005 1:00 p.m. - Initial write. 
Eamonn Murray







Click
here
to view Eamonn Murray's online profile.      (2006-4-11:11:06)

 感谢原创者的辛勤劳动,希望对您有所帮助,转载请注明原出处。
 您可能对 [ADO.Net] 的这些文章也感兴趣:

ADO.NET Entity Framework  支持实体框架的首批ADO.NET Provider已经准备就绪
ADO.NET的开发场景及传统ADO的处理  Datagrid导出当前页和全部页为EXCEL文件的解决方案
用C#开发PocketPC数据库应用程序  ADO.NET实体框架已经准备好面向企业级应用了吗?
在ADO.NET数据集中浏览多个相关表(2)  用C#和ADO.NET建立一个数据绑定网格
ADO.NET Entity Framework(2)建模  微软发布Astoria项目的十二月份CTP版
ADO.NET 数据库访问之数据分页  通过DataTable获得表的主键
ADO.NET入门(6)  在C#中使用ADO.Net部件来访问Access数据库
SQL存储过程在.NET数据库中的应用  DataTable,DataView和DataGrid中一些容易混淆的概念
迈向Data 2.0——学习使用ADO.NET数据服务框架  注意ADO.NET中数据查询语句中的符号格式(c#实现)
DataTable.Select使用小心得  ADO.NET基础教程(一)