如何从下拉列表中显示所选数据库的所有表 [英] how to show all tables of selected database from dropdownlist
本文介绍了如何从下拉列表中显示所选数据库的所有表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用2个下拉列表
第一个显示服务器上可用的所有数据库
第二个用于显示下拉列表上方所选数据库的所有表...
为此,应使用C#编写什么代码:-(
请帮助我:(
我的代码
I am using 2 dropdownlist
1st for showing all the databases available on server
and 2nd for showing all the table of selected database from above dropdownlist...
what the code should be written in C# to do this :-(
plzz help me out :(
my code
protected void dropbind()
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select name from sys.databases";
cmd.Connection = con;
SqlDataReader dr;
dr = cmd.ExecuteReader();
DropDownList1.DataTextField = "name";
// DropDownList1.DataValueField = "database_id";
DropDownList1.DataSource= dr;
DropDownList1.DataBind();
// cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlDataAdapter adp = new SqlDataAdapter("select name from sysobjects where type = 'u'" ,ConfigurationManager.ConnectionStrings["cn"].ConnectionString);// here in query i m stuck ...plz tell me how to retreive all table in selected database
Above query give me only name of master database
DataSet ds = new DataSet();
adp.Fill(ds);
DropDownList2.DataTextField = "name";
DropDownList2.DataSource = ds;
DropDownList2.DataBind();
推荐答案
尝试 ^ ]您需要使用的语法.
Try here[^] for the SQL syntax you need to use.
String connString =
"Data Source=bla bla bla; Integrated Security=True;";
Use this code in in the form load
using (SqlConnection sqlConn = new SqlConnection (connString))
{
sqlConn.Open();
DataTable tblDatabases = sqlConn.GetSchema ("Databases");
sqlConn.Close();
foreach (DataRow row in tblDatabases.Rows)
{
//add each database to dropdown
}
}
下拉菜单1的clicked事件
获取选定的项目或数据库名称.
然后针对所选数据库
clicked event of dropdown 1
get the selected item or database name.
then for the selected database
string connString = "Datasource = (selected databasse from dropdown ) integrated securtity = true";
DataTable tables = new DataTable("Tables");
using (SqlConnection sqlConn = new SqlConnection(connString))
{
SqlCommand cmd = sqlConn.CreateCommand();
cmd.CommandText = "select table_name as Name from INFORMATION_SCHEMA.Tables where TABLE_TYPE = 'BASE TABLE'";
sqlConn.Open();
tables.Load(cmd.ExecuteReader( CommandBehavior.CloseConnection));
}
可以帮助您..
That will help you out..
入门指南,介绍如何通过C#访问SQL Server [CP的[ ^ ]文章可能帮助您.
Beginners guide to accessing SQL Server through C#[^] article from CP might help you.
这篇关于如何从下拉列表中显示所选数据库的所有表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文