我需要查看我的c#程序使用的Access数据库中是否存在表。知道有其他数据库的SQL命令将返回表列表。是否有这样的Access / Jet数据库命令?
答案 0 :(得分:11)
尝试GetSchema()
connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\access.mdb";
connection.Open();
DataTable userTables = connection.GetSchema("Tables");
答案 1 :(得分:7)
完整代码:Get List of Tables in an Access Database - ADO.NET Tutorials
// Microsoft Access provider factory
DbProviderFactory factory =
DbProviderFactories.GetFactory("System.Data.OleDb");
DataTable userTables = null;
using (DbConnection connection =
factory.CreateConnection())
{
// c:\test\test.mdb
connection.ConnectionString = "Provider=Microsoft
.Jet.OLEDB.4.0;Data Source=c:\\test\\test.mdb";
// We only want user tables, not system tables
string[] restrictions = new string[4];
restrictions[3] = "Table";
connection.Open();
// Get list of user tables
userTables =
connection.GetSchema("Tables", restrictions);
}
// Add list of table names to listBox
for (int i=0; i < userTables.Rows.Count; i++)
listBox1.Items.Add(userTables.Rows[i][2].ToString())
答案 2 :(得分:2)
这样的事情应该可以解决问题。子句Type = 1
指定表。请注意,这还将包括结果集中的系统表(它们以前缀“MSys”开头。
SELECT Name FROM MSysObjects WHERE Type = 1
答案 3 :(得分:0)
有人为我工作
using (OleDbConnection con = new OleDbConnection(connectionString))
{
con.Open();
DataTable dt = con.GetSchema("Tables");
var selectNames = dt.Rows.Cast<DataRow>().Where(c => !c["TABLE_NAME"].ToString().Contains("MSys")).ToArray();
foreach (var item in selectNames)
{
// add names to comboBox
comboBox1.Items.Add(item["TABLE_NAME"]);
}
}