参考文章:http://www.cnblogs.com/Abel_cn/archive/2008/12/09/1351425.html
http://blog.csdn.net/friendan/article/details/52182923
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
- // ...
- /// <summary>
- /// 获取数据库连接对象
- /// </summary>
- /// <param name="dbName"></param>
- /// <returns></returns>
- public SqlConnection getSqlConnection(string dbName)
- {
- SqlConnection conn = new SqlConnection();
- conn.ConnectionString = string.Format("server=127.0.0.1,9527;database={0};uid=sa;pwd=123456;Enlist=true", dbName);
- conn.Open();
- //showLog(string.Format("连接数据库成功:{0}", conn.Database));
- return conn;
- }
- /// <summary>
- /// 取所有数据库名称
- /// </summary>
- /// <returns></returns>
- public ArrayList getAllDbName()
- {
- ArrayList dbNameList = new ArrayList();
- DataTable dbNameTable = new DataTable();
- SqlConnection conn = getSqlConnection("master");
- SqlDataAdapter adapter = new SqlDataAdapter("select name from master..sysdatabases", conn);
- lock (adapter)
- {
- adapter.Fill(dbNameTable);
- }
- foreach (DataRow row in dbNameTable.Rows)
- {
- dbNameList.Add(row["name"]);
- }
- conn.Close();
- return dbNameList;
- }