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

前端之家收集整理的这篇文章主要介绍了如何在C#中枚举网络中的可用数据库服务(SQLSewrver或Oracle或MySQL或PostgreSQL等)?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何使用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"]);
}

有关其他信息,请参阅此link

猜你在找的C#相关文章