如何获取表的名称和计数 [英] how to get names and count of tables
本文介绍了如何获取表的名称和计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从sqlserver和MSAccess获取数据库中表的名称和计数?
how to get names and count of tables in a database from sqlserver and MSAccess?
could u help me pls?
推荐答案
数据库中的表计数,可以使用以下查询找到:
Count of tables in a database, can be found using the below query:
SELECT COUNT(*) from information_schema.tables
WHERE table_type = 'base table'
对于Access数据库:
For Access database :
using System;
using System.Data;
using System.Data.OleDb;
public class DatabaseInfo {
public static void Main () {
String connect = "Provider=Microsoft.JET.OLEDB.4.0;data source=.\\Employee.mdb";
OleDbConnection con = new OleDbConnection(connect);
con.Open();
Console.WriteLine("Made the connection to the database");
Console.WriteLine("Information for each table contains:");
DataTable tables = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[]{null,null,null,"TABLE"});
Console.WriteLine("The tables are:");
foreach(DataRow row in tables.Rows)
Console.Write(" {0}", row[2]);
con.Close();
}
}
从这里摘录:
http://stackoverflow.com/issues/4443191/how-do-i-extract-table-names-from-ms-access-database-using-ado [
Extracted from here :
http://stackoverflow.com/questions/4443191/how-do-i-extract-table-names-from-ms-access-database-using-ado[^]
For SQL Server :
public static List<string> GetTables(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
DataTable schema = connection.GetSchema("Tables");
List<string> TableNames = new List<string>();
foreach (DataRow row in schema.Rows)
{
TableNames.Add(row[2].ToString());
}
return TableNames;
}
}
从这里摘录:
http://stackoverflow.com/questions/6671067/retrieve-list-of-tables-from-specific-database-on-server-c-sharp [
Extracted from here :
http://stackoverflow.com/questions/6671067/retrieve-list-of-tables-from-specific-database-on-server-c-sharp[^]
Hope it helps.
这篇关于如何获取表的名称和计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文