国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

獲取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; } }}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安顺市| 山东省| 黔南| 叶城县| 库车县| 武宣县| 云霄县| 壶关县| 会理县| 拉孜县| 肇庆市| 伊宁县| 东乌珠穆沁旗| 睢宁县| 独山县| 游戏| 呼和浩特市| 手游| 兴义市| 乳山市| 宁德市| 桦甸市| 繁峙县| 景宁| 甘泉县| 偃师市| 壶关县| 定远县| 浪卡子县| 涪陵区| 新竹市| 金秀| 海伦市| 宁明县| 平远县| 广宁县| 海兴县| 石渠县| 崇仁县| 西青区| 河南省|