通过Interop运行访问查询? [英] Run Access Queries via Interop?

查看:140
本文介绍了通过Interop运行访问查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在几个 *。mdb 文件中有几个MS-Access查询。它们是非常复杂的。

所以我只想打开一个文件,运行它包含的特定查询并返回一个结果表。

我该怎么做?

I have a few MS-Access queries in several *.mdb files. They are quite complex.
So I just want to open a file, run specific queries which it contains and get a resulting table back.
How can I do that ?

(我知道我们可以通过连接字符串等连接,但我想用这种方式。)



我的示例代码(已编辑以禁用安全提醒)

using Microsoft.Office.Interop.Access;
using Microsoft.Office.Core;

var app = new Application();
app.OpenCurrentDatabase(@"C:\test.mdb", true);
app.AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityLow;
app.Visible = false;
// run query
app.Quit();

查询示例

select date(), date()-1


推荐答案

要从Microsoft Access应用程序的实例中运行查询(例如,运行使用自定义VBA函数和其他可能无法从直接系统查询的功能的查询)。 Data.OleDb或System.Data.Odbc连接),你可以这样做:

To run the query from within an instance of the Microsoft Access application (e.g., to run queries that use custom VBA functions and other features that may not be available to queries from direct System.Data.OleDb or System.Data.Odbc connections) you can do something like this:

var accApp = new Microsoft.Office.Interop.Access.Application();
accApp.OpenCurrentDatabase(@"C:\Users\Public\Database1.accdb");
Microsoft.Office.Interop.Access.Dao.Database cdb = accApp.CurrentDb();

Microsoft.Office.Interop.Access.Dao.Recordset rst = 
        cdb.OpenRecordset(
            "SELECT FullName FROM ClientQuery", 
            Microsoft.Office.Interop.Access.Dao.RecordsetTypeEnum.dbOpenSnapshot);
while (!rst.EOF)
{
    Console.WriteLine(rst.Fields["FullName"].Value);
    rst.MoveNext();
}
rst.Close();
accApp.CloseCurrentDatabase();
accApp.Quit();

这篇关于通过Interop运行访问查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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