获取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; } }}
相关文章推荐
- 获取Sql服务器列表 (C#)
- [转]C# 获取 SQL服务器列表
- C#_获取 SQL服务器列表
- 获取Sql服务器列表 (C#)
- C# 获取sql服务器列表
- C# 获取 SQL服务器列表
- C#_获取 SQL服务器列表
- C# 获取 SQL服务器列表
- c#中获取服务器IP,客户端IP以及Request.ServerVariables详细说明-->防止SQL的注入
- C#获取指定数据库服务器下某用户可访问的数据库列表
- 获取Sql服务器列表
- C#获取局域网内sql server数据库服务器列表
- 在C#中用SQLDMO得到指定SQL服务器所有数据库的列表
- C#获取局域网内sql server数据库服务器列表
- C#利用SQL-DMO得到(局域网中)SQL服务器的列表
- C#获取指定数据库服务器下某用户可访问的数据库列表
- SQL脚本获取指定SQLServer数据库所有表及视图的字段列表及类型、长度
- 获取信息列表中每个类别下的最新一条记录的 SQL语句
- C# 目录操作(新建,删除),获取目录下文件列表
- C#通过反射获取枚举值列表