获取sqlserverName(计算机名)上所有的数据库名称

/// <summary>
        /// 获取sqlserverName(计算机名)上所有的数据库名称
        /// </summary>
        /// <param name="sqlserverName"></param>
        /// <returns></returns>
        public static List<string> getAllDataBase(string sqlserverName)
        {
            List<string> listdatabase = new List<string>();
            string connstr = "Data Source=" + sqlserverName + "\\SQLEXPRESS;DataBase=master;Integrated Security=True;User Instance=True";
            SqlConnection conn = new SqlConnection(connstr);
            using (conn)
            {
                string cmdtext = "SELECT name FROM master.dbo.sysdatabases WHERE name NOT IN ('master','tempdb','msdb','model')";
                SqlCommand cmd = conn.CreateCommand();
                conn.Open();
                cmd.CommandText = cmdtext;
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    listdatabase.Add(dr.GetString(0));
                }
                return listdatabase;
            }
        }
posted @ 2012-04-24 14:02  易水寒2012  阅读(1175)  评论(0编辑  收藏  举报