Inadvertently found a C # program, I feel so good to share it here so that I will share it with everyone. In fact, I am copying others. The original author is unknown. I feel that I have a good share with you. API to get the function of the SQL Server server list, I think it is more meaningful than using the SQLDemo component, the code is as follows:
///
/// Get the name of the database server in the network
/// summary>
Public Class Sqllocator
{
[System.Runtime.InteropServices.dllimport ("ODBC32.DLL")]]]]
Private static extern short sqlallochandle (short htype, pointptr inputhandle);
[System.Runtime.InteropServices.dllimport ("ODBC32.DLL")]]]]
Private Static Extern Short Sqlsetenvattr (INTPTR HENV, INT Attribute, INTPTR VALUEPTR, INT STRLENGTH);
[System.Runtime.InteropServices.dllimport ("ODBC32.DLL")]]]]
Private static extern short sqlfreehandle (short htype, pointpe, pointpe);
[System.Runtime.InteropServices.dllimport ("ODBC32.DLL", Charset = System.Runtime.InteropServices.Charset.ansi)]
Private Static Extern Short SqlbrowSeconnect (INTPTR HCONN, System.Text.StringBuilder Instring,
Short InstringLength, System.Text.StringBuilder Outstring, Short OutstringLength,
OUT Short Outlengthneeded;
Private const short sql_handle_env = 1;
Private const short sql_handle_dbc = 2;
PRIVATE CONST INT SQL_ATTR_ODBC_VERSION = 200;
Private const INT SQL_OV_ODBC3 = 3;
Private const short sql_success = 0;
Private const short sql_need_data = 99;
PRIVATE CONST Short default_result_size = 1024;
Private const string SQL_Driver_str = "driver = sql server";
Private sqllocator () {}
///
/// Get the database server name in the network is a string array. /// summary>
///
Public static string [] getServers ()
{
String list = string.empty;
INTPTR HENV = INTPTR.ZERO;
INTPTR HCONN = INTPTR.ZERO; System.Text.StringBuilder Instring = New System.Text.StringBuilder (SQL_DRIVER_STR);
System.Text.StringBuilder outstring = new system.text.stringbuilder (default_result_size);
Short InstringLength = (short) Instring.length;
Short lenneeded = 0;
Try
{
IF (SQL_SUCCESS == SQLAllochandle (SQL_HANDE_ENV, HENV, OUT HENV))
{
IF (SQL_Success == SQLSETENVATTR (Henv, SQL_ATTR_ODBC_VERSION, (INTPTR) SQL_OV_ODBC3, 0))))
{
IF (SQL_SUCCESS == SQLAllochandle (SQL_HANDLE_DBC, HENV, OUT HCONN))
{
IF (SQL_NEED_DATA == SQLBROWSECONNECT (HCONN, Instring, InstringLength, Outstring,
Default_Result_size, Out Lenneeded))
{
IF (Default_Result_size { Outstring.capacity = lenneed; IF (SQL_NEED_DATA! = SQLBROWSECONNECT (HCONN, Instring, InstringLength, Outstring, Lenneeded, Out Lenneeded)) { Throw New ApplicationException ("Unabled to Aquire SQL Servers from ODBC DRIVER."); } } List = outstring.toString (); INT Start = list.indexof ("{") 1; INT LEN = list.indexof ("}") - start; IF ((Start> 0) && (Len> 0))) { List = list.substring (start, len); } Else { List = String.empty; } } } } } } Catch { List = String.empty; } Finally { IF (hconn! = INTPTR.ZERO) { SQLFreeHandle (SQL_HANDLE_DBC, HCONN); } IF (Henv! = INTPTR.ZERO) { SQLFreeHandle (SQL_HANDLE_ENV, HCONN); } } String [] array = null; IF (List.length> 0) { Array = List.split (','); } Return array; } }