您的位置:首页 > 数据库

获取Sql服务器列表 (C#)

2008-04-24 13:26 435 查看
怎么实现这个功能大家肯定早都知道了,放上来是给自己留个备份使用:
private void button1_Click_1(object sender, System.EventArgs e)...{ string[] servers = DBGrep.SqlLocator.GetServers(); foreach ( string s in servers ) ...{ this.listBox1.Items.Add(s); }}[code]类的代码
using System;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;namespace DBGrep...{ public class SqlLocator ...{ [DllImport("odbc32.dll")] private static extern short SqlAllocHandle(short hType, IntPtr inputHandle, out IntPtr outputHandle); [DllImport("odbc32.dll")] private static extern short SqlSetEnvAttr(IntPtr henv, int attribute, IntPtr valuePtr, int strLength); [DllImport("odbc32.dll")] private static extern short SqlFreeHandle(short hType, IntPtr handle);  [DllImport("odbc32.dll",CharSet=CharSet.Ansi)] private static extern short SqlBrowseConnect(IntPtr hconn, StringBuilder inString,  short inStringLength, 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()...{} public static string[] GetServers() ...{ string[] retval = null; string txt = string.Empty; IntPtr henv = IntPtr.Zero; IntPtr hconn = IntPtr.Zero; StringBuilder inString = new StringBuilder(Sql_DRIVER_STR); StringBuilder outString = new StringBuilder(DEFAULT_RESULT_SIZE); short inStringLength = (short) inString.Length; short lenNeeded = 0; try ...{ if (Sql_SUCCESS == SqlAllocHandle(Sql_HANDLE_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 < lenNeeded) ...{ outString.Capacity = lenNeeded; if (Sql_NEED_DATA != SqlBrowseConnect(hconn, inString, inStringLength, outString,  lenNeeded,out lenNeeded)) ...{ throw new ApplicationException("Unabled to aquire Sql Servers from ODBC driver."); }  } txt = outString.ToString(); int start = txt.IndexOf("{") + 1; int len = txt.IndexOf("}") - start; if ((start > 0) && (len > 0)) ...{ txt = txt.Substring(start,len); } else ...{ txt = string.Empty; } }  } } } } catch (Exception ex) ...{ //Throw away any error if we are not in debug mode#if (DEBUG) MessageBox.Show(ex.Message,"Acquire Sql Servier List Error");#endif  txt = string.Empty; } finally ...{ if (hconn != IntPtr.Zero) ...{ SqlFreeHandle(Sql_HANDLE_DBC,hconn); } if (henv != IntPtr.Zero) ...{ SqlFreeHandle(Sql_HANDLE_ENV,hconn); } }  if (txt.Length > 0) ...{ retval = txt.Split(",".ToCharArray()); } return retval; } }}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: