在组合框中显示c#中sql db中所有表的名称 [英] display names of all tables in sql db in c# in a combo box
本文介绍了在组合框中显示c#中sql db中所有表的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
PLZ可以通过提供c#代码来帮助我显示我在我的sql数据库中创建的表的名称...
解决方案
所有你需要的do是发出SQL查询:
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
所以基本的方法是:
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
使用(SqlCommand com = new SqlCommand( SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES,con))
{
using using (SqlDataReader reader = com.ExecuteReader())
{
myComboBox.Items.Clear();
while (reader.Read())
{
myComboBox.Items.Add(( string )reader [ TABLE_NAME]);
}
}
}
}
SELECT * FROM INFORMATION_SCHEMA.TABLES 其中 TABLE_TYPE = ' BASE TABLE'
或
USE DBName
GO
SELECT * FROM sys.Tables
GO
使用此查询从数据库中获取所有表名
嗨试试这个,
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Data.SqlClient;
命名空间 ConsoleApplication1
{
class 程序
{
// 请尝试以下
// 对于MySQL:
// SELECT TABLE_NAME
// FROM INFORMATION_SCHEMA .TABLES
// WHERE TABLE_TYPE ='BASE TABLE'和TABLE_SCHEMA ='dbName'
// 对于MS SQL:
// SELECT TABLE_NAME
/ / FROM INFORMATION_SCHEMA.TABLES
// WHERE TABLE_TYPE ='BASE TABLE'
static void Main( string [] args)
{
SqlConnection con = new SqlConnection( data source =您的服务器名称; database =数据库名称; uid = sa; pwd =您的sql server密码);
con.Open();
SqlCommand cmd = new SqlCommand( SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE ='BASE TABLE',con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows == true )
{
while (dr.Read())
{
Console.WriteLine( 表名: + dr [ 0 ]);
}
}
如果(!dr.IsClosed)
{
dr.Close();
}
如果(con!= null )
{
con.Close();
}
Console.ReadLine();
}
}
}
问候
sarva
plz can u help me by giving the c# code for displaying names of the tables i hv created in my sql database...
解决方案
All you need to do is issue an SQL query:
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
So a basic way is:
using (SqlConnection con = new SqlConnection(strConnect)) { con.Open(); using (SqlCommand com = new SqlCommand("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES", con)) { using (SqlDataReader reader = com.ExecuteReader()) { myComboBox.Items.Clear(); while (reader.Read()) { myComboBox.Items.Add((string) reader["TABLE_NAME"]); } } } }
SELECT * FROM INFORMATION_SCHEMA.TABLES where TABLE_TYPE='BASE TABLE'
or
USE DBName GO SELECT * FROM sys.Tables GO
Use this query to get all the table name from a database
Hi Try this one,
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace ConsoleApplication1 { class Program { // Try the following //For MySQL: //SELECT TABLE_NAME //FROM INFORMATION_SCHEMA.TABLES //WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA='dbName' //For MS SQL: //SELECT TABLE_NAME //FROM INFORMATION_SCHEMA.TABLES //WHERE TABLE_TYPE = 'BASE TABLE' static void Main(string[] args) { SqlConnection con = new SqlConnection("data source=Your server name;database=database name;uid=sa;pwd=your sql server password"); con.Open(); SqlCommand cmd = new SqlCommand("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'", con); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows == true) { while (dr.Read()) { Console.WriteLine("Table Name : " + dr[0]); } } if (!dr.IsClosed) { dr.Close(); } if (con != null) { con.Close(); } Console.ReadLine(); } } }
regards
sarva
这篇关于在组合框中显示c#中sql db中所有表的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文