如何在C#中枚举网络中的可用数据库服务(SQLSewrver或Oracle或MySQL或PostgreSQL等)?

如何在C#中枚举网络中的可用数据库服务(SQLSewrver或Oracle或MySQL或PostgreSQL等)?,第1张

概述如何使用C#枚举网络中的可用数据库服务(SQL Server或Oracle或 MySQL或PostgreSQL等)? 我还需要找到他们的端口号和服务实例名称. 例如: class Server{ #region DLL Inports [DllImport("odbc32.dll")] private static extern short SQLAllocHandle( 如何使用C#枚举网络中的可用数据库服务(sql Server或Oracle或 MySQL或Postgresql等)?

我还需要找到他们的端口号和服务实例名称.

例如:

class Server{    #region DLL Inports    [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);    #endregion    #region Constants    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";    #endregion    #region static string[] GetServers()    public static string[] Getnames()    {        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,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,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;            throw ex;        }        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;    }    #endregion}

此代码仅适用于sqlServers和windowsXP.

它不适用于其他DBMS服务器和Win7.

我需要一个规范的解决方案.

解决方法 您也可以使用 SqlDataSourceEnumerator类.请记住,这是MS sql Server特定的……
var results = sqlDataSourceEnumerator.Instance.GetDataSources();foreach (var row in results.Rows){    Console.Writeline("{0}\{1}",row["Servername"],row["Instancename"]);}

有关其他信息,请参阅此@L_502_2@

总结

以上是内存溢出为你收集整理的如何在C#中枚举网络中的可用数据库服务(SQLSewrver或Oracle或MySQL或PostgreSQL等)?全部内容,希望文章能够帮你解决如何在C#中枚举网络中的可用数据库服务(SQLSewrver或Oracle或MySQL或PostgreSQL等)?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1248864.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存