如何在C#中使用OleDB在MS Access文件中列出所有查询? [英] How do I list all the queries in a MS Access file using OleDB in C#?
问题描述
我有一个包含200个查询的Access 2003文件,我想用SQL打印出它们的表示形式.我可以使用Design View查看每个查询并将其剪切并粘贴到文件中,但这很繁琐.另外,我可能必须在其他Access文件上再次执行此操作,因此,我绝对想编写一个程序来执行此操作.
I have an Access 2003 file that contains 200 queries, and I want to print out their representation in SQL. I can use Design View to look at each query and cut and paste it to a file, but that's tedious. Also, I may have to do this again on other Access files, so I definitely want to write a program to do it.
查询在哪里存储Access数据库?我找不到任何关于如何找到他们的信息.我不熟悉Access,因此不胜感激.谢谢!
Where are queries stored an Access db? I can't find anything saying how to get at them. I'm unfamiliar with Access, so I'd appreciate any pointers. Thanks!
推荐答案
您要寻找的程序是
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();
DataTable queries = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Procedures, null);
conn.Close();
这将为您提供一个包含以下各列的DataTable(以及其他内容):
This will give you a DataTable with the following columns in it (among others):
PROCEDURE_NAME:查询的名称
PROCEDURE_NAME: Name of the query
PROCEDURE_DEFINITION:SQL定义
PROCEDURE_DEFINITION: SQL definition
因此您可以像这样遍历表:
So you can loop through the table like so:
foreach(DataRow row in queries.Rows)
{
// Do what you want with the values here
queryName = row["PROCEDURE_NAME"].ToString();
sql = row["PROCEDURE_DEFINITION"].ToString();
}
这篇关于如何在C#中使用OleDB在MS Access文件中列出所有查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!