擷取區域網路所有資料庫伺服器、查詢sqlserver非系統資料庫、所有表、所有列

來源:互聯網
上載者:User
        /// <summary>          /// 擷取區域網路內的所有資料庫伺服器名稱          /// </summary>          /// <returns>伺服器名稱數組</returns>          public List<string> GetSqlServerNames()        {            DataTable dataSources = SqlClientFactory.Instance.CreateDataSourceEnumerator().GetDataSources();            DataColumn column = dataSources.Columns["InstanceName"];            DataColumn column2 = dataSources.Columns["ServerName"];            DataRowCollection rows = dataSources.Rows;            List<string> Serverlist = new List<string>();            string array = string.Empty;            for (int i = 0; i < rows.Count; i++)            {                string str2 = rows[i][column2] as string;                string str = rows[i][column] as string;                if (((str == null) || (str.Length == 0)) || ("MSSQLSERVER" == str))                {                    array = str2;                }                else                {                    array = str2 + @"\" + str;                }                Serverlist.Add(array);            }            Serverlist.Sort();            return Serverlist;        }        /// <summary>          /// 查詢sql中的非系統庫          /// </summary>          /// <param name="connection"></param>          /// <returns></returns>          public List<string> databaseList(string connection)        {            List<string> getCataList = new List<string>();            string cmdStirng = "select name from sys.databases where database_id > 4";            SqlConnection connect = new SqlConnection(connection);            SqlCommand cmd = new SqlCommand(cmdStirng, connect);            try            {                if (connect.State == ConnectionState.Closed)                {                    connect.Open();                    IDataReader dr = cmd.ExecuteReader();                    getCataList.Clear();                    while (dr.Read())                    {                        getCataList.Add(dr["name"].ToString());                    }                    dr.Close();                }            }            catch (SqlException e)            {                //MessageBox.Show(e.Message);              }            finally            {                if (connect != null && connect.State == ConnectionState.Open)                {                    connect.Dispose();                }            }            return getCataList;        }        /// <summary>          /// 擷取列名          /// </summary>          /// <param name="connection"></param>          /// <returns></returns>          public List<string> GetTables(string connection)        {            List<string> tablelist = new List<string>();            SqlConnection objConnetion = new SqlConnection(connection);            try            {                if (objConnetion.State == ConnectionState.Closed)                {                    objConnetion.Open();                    DataTable objTable = objConnetion.GetSchema("Tables");                    foreach (DataRow row in objTable.Rows)                    {                        tablelist.Add(row[2].ToString());                    }                }            }            catch            {            }            finally            {                if (objConnetion != null && objConnetion.State == ConnectionState.Closed)                {                    objConnetion.Dispose();                }            }            return tablelist;        }        /// <summary>          /// 擷取欄位          /// </summary>          /// <param name="connection"></param>          /// <param name="TableName"></param>          /// <returns></returns>          public List<string> GetColumnField(string connection, string TableName)        {            List<string> Columnlist = new List<string>();            SqlConnection objConnetion = new SqlConnection(connection);            try            {                if (objConnetion.State == ConnectionState.Closed)                {                    objConnetion.Open();                }                SqlCommand cmd = new SqlCommand("Select Name FROM SysColumns Where id=Object_Id('" + TableName + "')", objConnetion);                SqlDataReader objReader = cmd.ExecuteReader();                while (objReader.Read())                {                    Columnlist.Add(objReader[0].ToString());                }            }            catch            {            }            objConnetion.Close();            return Columnlist;        }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.