首页 > 开发 > 综合 > 正文

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

2024-07-21 02:19:38
字体:
来源:转载
供稿:网友
怎么实现这个功能大家肯定早都知道了,放上来是给自己留个备份
使用:


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); }}类的代码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; } }}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表