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

None.gif private   void  button1_Click_1( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
string[] servers = DBGrep.SqlLocator.GetServers();
InBlock.gif 
foreach ( string s in servers )
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif 
this.listBox1.Items.Add(s);
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif类的代码
None.gif
None.gif
using  System; using  System.Text;  using  System.Windows.Forms; using  System.Runtime.InteropServices;
None.gif
namespace  DBGrep
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {  public class SqlLocator
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{  [DllImport("odbc32.dll")]  private static extern short SQLAllocHandle(short hType, IntPtr inputHandle, out IntPtr outputHandle);
InBlock.gif  [DllImport(
"odbc32.dll")]
InBlock.gif  
private static extern short SQLSetEnvAttr(IntPtr henv, int attribute, IntPtr valuePtr, int strLength);
InBlock.gif  [DllImport(
"odbc32.dll")]
InBlock.gif 
private static extern short SQLFreeHandle(short hType, IntPtr handle); 
InBlock.gif[DllImport(
"odbc32.dll",CharSet=CharSet.Ansi)]
InBlock.gif
private static extern short SQLBrowseConnect(IntPtr hconn, StringBuilder inString, 
InBlock.gif
short inStringLength, StringBuilder outString, short outStringLength,
InBlock.gif
out short outLengthNeeded);
InBlock.gif
InBlock.gif
private const short SQL_HANDLE_ENV = 1;
InBlock.gif
private const short SQL_HANDLE_DBC = 2;
InBlock.gif
private const int SQL_ATTR_ODBC_VERSION = 200;
InBlock.gif
private const int SQL_OV_ODBC3 = 3;
InBlock.gif
private const short SQL_SUCCESS = 0;
InBlock.gif
private const short SQL_NEED_DATA = 99;
InBlock.gif
private const short DEFAULT_RESULT_SIZE = 1024;
InBlock.gif
private const string SQL_DRIVER_STR = "DRIVER=SQL SERVER";
ExpandedSubBlockStart.gifContractedSubBlock.gif
private SqlLocator()dot.gif{}
InBlock.gif
public static string[] GetServers()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
string[] retval = null;
InBlock.gif
string txt = string.Empty;
InBlock.gifIntPtr henv 
= IntPtr.Zero;
InBlock.gifIntPtr hconn 
= IntPtr.Zero;
InBlock.gifStringBuilder inString 
= new StringBuilder(SQL_DRIVER_STR);
InBlock.gifStringBuilder outString 
= new StringBuilder(DEFAULT_RESULT_SIZE);
InBlock.gif
short inStringLength = (short) inString.Length;
InBlock.gif
short lenNeeded = 0;
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if (SQL_SUCCESS == SQLAllocHandle(SQL_HANDLE_ENV, henv, out henv))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if (SQL_SUCCESS == SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(IntPtr)SQL_OV_ODBC3,0))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if (SQL_SUCCESS == SQLAllocHandle(SQL_HANDLE_DBC, henv, out hconn))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if (SQL_NEED_DATA == SQLBrowseConnect(hconn, inString, inStringLength, outString, 
InBlock.gifDEFAULT_RESULT_SIZE, 
out lenNeeded))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if (DEFAULT_RESULT_SIZE < lenNeeded)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifoutString.Capacity 
= lenNeeded;
InBlock.gif 
if (SQL_NEED_DATA != SQLBrowseConnect(hconn, inString, inStringLength, outString, 
InBlock.gif lenNeeded,
out lenNeeded))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif 
throw new ApplicationException("Unabled to aquire SQL Servers from ODBC driver.");
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif }

InBlock.gif txt 
= outString.ToString();
InBlock.gif 
int start = txt.IndexOf("{"+ 1;
InBlock.gif 
int len = txt.IndexOf("}"- start;
InBlock.gif 
if ((start > 0&& (len > 0))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.giftxt 
= txt.Substring(start,len);
ExpandedSubBlockEnd.gif}

InBlock.gif 
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.giftxt 
= string.Empty;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif 
//Throw away any error if we are not in debug mode
InBlock.gif
#if (DEBUG)
InBlock.gifMessageBox.Show(ex.Message,
"Acquire SQL Servier List Error");
InBlock.gif
#endif 
InBlock.giftxt 
= string.Empty;
ExpandedSubBlockEnd.gif}

InBlock.gif
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if (hconn != IntPtr.Zero)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSQLFreeHandle(SQL_HANDLE_DBC,hconn);
ExpandedSubBlockEnd.gif}

InBlock.gif
if (henv != IntPtr.Zero)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif SQLFreeHandle(SQL_HANDLE_ENV,hconn);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif }

InBlock.gif 
InBlock.gif
if (txt.Length > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif retval 
= txt.Split(",".ToCharArray());
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif
return retval;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值