How do I list all the queries in a MS Access file using OleDB in C#?

狂风中的少年 提交于 2019-12-02 03:56:24

问题


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.

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!


回答1:


Procedures are what you're looking for:

OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();

DataTable queries = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Procedures, null);

conn.Close();

This will give you a DataTable with the following columns in it (among others):

PROCEDURE_NAME: Name of the query

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();
}



回答2:


you can put this together using the OleDbConnection's GetSchema method along with what Remou posted with regards to the ADO Schemas

oops forgot link: MSDN




回答3:


In case you wanted to do a quick query by hand.

SELECT MSysObjects.Name
FROM MSysObjects
WHERE type = 5



回答4:


Not in C#, but may be a good place to start:

http://www.datastrat.com/Code/DocDatabase.txt



来源:https://stackoverflow.com/questions/371404/how-do-i-list-all-the-queries-in-a-ms-access-file-using-oledb-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!