'获取SQL Server服务器端所有数据库列表
void Page_Load(object sender, System.EventArgs e)
{
// 创建连接及执行数据库操作
string db_query = "sp_helpdb";
SqlCommand myCommand = new SqlCommand(db_query, new SqlConnection(ConnStr));
myCommand.Connection.Open();
SqlDataReader dr = myCommand.ExecuteReader();
// 将数据库列表绑定到下拉列表控件(DropDownList)
DropDownList1.DataSource = dr;
DropDownList1.DataTextField = "name";
DropDownList1.DataBind();
// 关闭DataReader对象和数据库连接
dr.Close();
myCommand.Connection.Close();
}
void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
// 获取要打开的数据库名称
string dbName = DropDownList1.SelectedItem.Text;
// 生成新的数据库连接字符串
ConnStr = ConnStr + "Database=" + dbName;
// 打开连接
SqlConnection myConn = new SqlConnection(ConnStr);
myConn.Open();
Label1.Text = myConn.Database;
myConn.Close();
}
void Page_Load(object sender, System.EventArgs e)
{
// 创建连接及执行数据库操作
string db_query = "sp_helpdb";
SqlCommand myCommand = new SqlCommand(db_query, new SqlConnection(ConnStr));
myCommand.Connection.Open();
SqlDataReader dr = myCommand.ExecuteReader();
// 将数据库列表绑定到下拉列表控件(DropDownList)
DropDownList1.DataSource = dr;
DropDownList1.DataTextField = "name";
DropDownList1.DataBind();
// 关闭DataReader对象和数据库连接
dr.Close();
myCommand.Connection.Close();
}
void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
// 获取要打开的数据库名称
string dbName = DropDownList1.SelectedItem.Text;
// 生成新的数据库连接字符串
ConnStr = ConnStr + "Database=" + dbName;
// 打开连接
SqlConnection myConn = new SqlConnection(ConnStr);
myConn.Open();
Label1.Text = myConn.Database;
myConn.Close();
}