using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; public partial class ReadAllTables : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { //取得所有数据库名 string constr = "data source=1f5f1616d1bb489//sqlexpress;Integrated Security=True"; SqlConnection myConnection = new SqlConnection(constr); string strSQL = "select name from sysdatabases where dbid>=5 order by dbid desc"; DataSet ds = new DataSet(); SqlDataAdapter myCommand = new SqlDataAdapter(strSQL, myConnection); myCommand.Fill(ds, "database"); this.AllDataBases.DataSource = ds.Tables["database"].DefaultView; this.AllDataBases.DataValueField = ds.Tables["database"].Columns[0].ColumnName; this.AllDataBases.DataTextField = ds.Tables["database"].Columns[0].ColumnName; this.AllDataBases.DataBind(); } } protected void AllDataBases_SelectedIndexChanged(object sender, EventArgs e) { string dbname = this.AllDataBases.SelectedValue.ToString(); //得到选中的数据库 string constr = "data source=1f5f1616d1bb489//sqlexpress;Initial Catalog="+dbname+";Integrated Security=True"; SqlConnection myConnection = new SqlConnection(constr); //根据选中数据库,得到相应的 所有表 string strSQL = "select name from sysobjects where type='"+'U'+"'"; DataSet ds = new DataSet(); SqlDataAdapter myCommand = new SqlDataAdapter(strSQL, myConnection); myCommand.Fill(ds, "table"); this.AllTables.DataSource = ds.Tables["table"].DefaultView; this.AllTables.DataValueField = ds.Tables["table"].Columns[0].ColumnName; this.AllTables.DataTextField = ds.Tables["table"].Columns[0].ColumnName; this.AllTables.DataBind(); } protected void AllTables_SelectedIndexChanged(object sender, EventArgs e) { string dbname = this.AllDataBases.SelectedValue.ToString(); string tablename = this.AllTables.SelectedValue.ToString(); string constr = "data source=1f5f1616d1bb489//sqlexpress;Initial Catalog=" + dbname + ";Integrated Security=True"; SqlConnection myConnection = new SqlConnection(constr); string strSQL = " select name from syscolumns where (id = (select id from sysobjects where (id = OBJECT_ID('"+tablename+"'))))order by colid"; DataSet ds = new DataSet(); SqlDataAdapter myCommand = new SqlDataAdapter(strSQL, myConnection); myCommand.Fill(ds, "column"); this.AllColumns.DataSource = ds.Tables["column"].DefaultView; this.AllColumns.DataValueField = ds.Tables["column"].Columns[0].ColumnName; this.AllColumns.DataTextField = ds.Tables["column"].Columns[0].ColumnName; this.AllColumns.DataBind(); } } //其中AllDataBases ,AllColumns和 AllTables 都为dropdownlist控件