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控件

 

连接到master库中,执行 select name from master..sysdatabases 。

sp_helpdb也可以列出数据名,但不是全部,sp_helpdb是会根据当前用户角色找出相应的数据库,而用sysdatabases表则只要有权限访问该表就能查出所有数据库信息。

默认情况下,sqlserver提供的6个数据库中,除了model数据库外,其他若干个数据库还可能拥有guest用户。
exec sp_helpdb得到的记录包括:当前用户拥有访问权限的数据库(包括默认的几个guest访问数据库)。  
任何用户(只要能连接到sqlserver)查询master数据库中的表sysdatabases可以得到所有的数据库名称,因为该表的guest用户不可删除。

posted @ 2010-07-29 04:31  海军  阅读(864)  评论(0编辑  收藏  举报