C#中读取数据库名,表名,列名

在C#中读取出所有数据库名,和相应的表名 !!
全部源代码 :
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控件 !!
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值