C#获取指定IP地址的数据库所有数据库实例名

/// <summary>
        /// 获取指定IP地址的数据库所有数据库实例名。
        /// </summary>
        /// <param name="ip">指定的 IP 地址。</param>
        /// <param name="username">登录数据库的用户名。</param>
        /// <param name="password">登陆数据库的密码。</param>
        /// <returns>返回包含数据实例名的列表。</returns>
        private ArrayList GetAllDataBase(string ip, string username, string password)
        {
            ArrayList DBNameList = new ArrayList();
            SqlConnection Connection = new SqlConnection(
                String.Format("Data Source={0};Initial Catalog = master;User ID = {1};PWD = {2}", ip, username, password));
            DataTable DBNameTable = new DataTable();
            SqlDataAdapter Adapter = new SqlDataAdapter("select name from master..sysdatabases", Connection);

            lock (Adapter)
            {
                Adapter.Fill(DBNameTable);
            }

            foreach (DataRow row in DBNameTable.Rows)
            {
                DBNameList.Add(row["name"]);
            }
            Connection.Close();
            return DBNameList;
        }

 

posted @ 2016-03-02 14:04  晓晨Master  阅读(736)  评论(0编辑  收藏