如何以编程方式检索所有查询的查询文本 [英] How to programmatically retrieve query text of all queries

查看:94
本文介绍了如何以编程方式检索所有查询的查询文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式检索访问数据库中存储的每个查询的查询文本.我正在考虑使用OleDbConnection.GetSchema或OleDbConnection.GetOleDbSchemaTable甚至ADOX来获取所需的信息.我精通C#.

I would like to programmatically retrieve the query text of every query stored in an access database. I am thinking of using OleDbConnection.GetSchema or OleDbConnection.GetOleDbSchemaTable or even ADOX to get the required information. I am proficient in C#.

可以做到吗?

您能提供一个例子还是好的例子的链接?

Can you provide an example or link to good examples?

推荐答案

如果您打算在VBA中的代码中使用此代码,则可以尝试类似的

If your intention is to use this in code in the VBA you can try something like this

Dim qdef As QueryDef
Dim qdefs As QueryDefs
Dim i As Integer
Dim name As String
Dim qSql As String

    Set qdefs = Application.CodeDb.QueryDefs
    For Each qdef In qdefs
        qname = qdef.name
        qSql = qdef.SQL
    Next qdef

qdef对象还将为您提供有关查询的更多信息.

the qdef object will also give you a lot more info about the query.

对于c#,您必须将ref添加到项目中以进行互操作(Microsoft Access ##对象库)

for c# you will have to add the ref to the project for the access interop (Microsoft Access ## Object Library)

并使用

private void QueryValues()
        {
            Microsoft.Office.Interop.Access.Application app = new Application(); 
            app.OpenCurrentDatabase(@"C:\Tests\C#\MS Access\WindowsApplication1\New Microsoft Office Access 2007 Database.accdb", false,"");
            QueryDefs qdefs = app.CurrentDb().QueryDefs;
            foreach (QueryDef qdef in qdefs)
            {
                string qname = qdef.Name;
                string qSql = qdef.SQL;
            }
            app.Quit(AcQuitOption.acQuitSaveNone);
        }

这篇关于如何以编程方式检索所有查询的查询文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆