此VBA代码是否会异步执行

JMK*_*JMK 2 c# ms-access

好的,我试图从C#自动化Microsoft Access.显然你不能在VBA本身中异步执行VBA代码,但我的想法是使用委托从C#强制执行此操作.

我们有一个遗留报告系统,它运行数百个设计糟糕的查询来获取信息,这些查询在宏中同步运行.每个查询都是使用MS Access查询设计器设计的,并通过ODBC查询MySql数据库.它们需要2-3分钟才能运行,而宏可能包含<= 20个查询,这意味着宏将花费大约一小时的时间来运行.如果我运行这些异步,我可以在几分钟内运行整个宏.

我的完整C#代码如下:

using System;
using Microsoft.Office.Interop.Access;

namespace AsyncVBA
{
    class Program
    {
        private static Application ap;
        private delegate void ExportThread(string queryName, string exportLocation);

        private static int count;

        static void Main(string[] args)
        {
            var dbName = @"C:\Users\JMK\Desktop\MyDatabase.accdb";
            count = 0;

            ExportThread queryThread = new ExportThread(ExportQuery);

            ap = new Application();
            ap.OpenCurrentDatabase(dbName);

            queryThread.BeginInvoke("qryOne", @"C:\Users\JMK\Desktop\x\one.xlsx", null, null);
            queryThread.BeginInvoke("qryTwo", @"C:\Users\JMK\Desktop\x\two.xlsx", null, null);
            queryThread.BeginInvoke("qryThree", @"C:\Users\JMK\Desktop\x\three.xlsx", null, null);
            queryThread.BeginInvoke("qryFour", @"C:\Users\JMK\Desktop\x\four.xlsx", null, null);
            queryThread.BeginInvoke("qryFive", @"C:\Users\JMK\Desktop\x\five.xlsx", null, null);
            queryThread.BeginInvoke("qrySix", @"C:\Users\JMK\Desktop\x\six.xlsx", null, null);
            queryThread.BeginInvoke("qrySeven", @"C:\Users\JMK\Desktop\x\seven.xlsx", null, null);
            queryThread.BeginInvoke("qryEight", @"C:\Users\JMK\Desktop\x\eight.xlsx", null, null);
            queryThread.BeginInvoke("qryNine", @"C:\Users\JMK\Desktop\x\nine.xlsx", null, null);
            queryThread.BeginInvoke("qryTen", @"C:\Users\JMK\Desktop\x\ten.xlsx", null, null);

            while (count < 10)
            {
                Console.ReadLine();
            }

            ap.CloseCurrentDatabase();
        }

        private static void ExportQuery(string queryName, string exportLocation)
        {
            ap.DoCmd.TransferSpreadsheet(AcDataTransferType.acExport, AcSpreadSheetType.acSpreadsheetTypeExcel9, queryName, exportLocation);
            count++;
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

我目前有两个问题,第一个是我的代码似乎仍在同步执行,这可能与MS Access中的限制有关.我猜测MS Access在收到请求时将请求排队.第二个不太重要的问题是,我的计数似乎没有增加.

我哪里错了?

谢谢

小智 7

从Access作为前端的大量经验谈起,最好是将所有查询更改为传递查询并使用mySQL语法重写它们.

当前方法花费这么长时间的原因可能是通过网络传递的数据集远远大于它需要的数据集.Access通过mySQL DB可以解释的内容,然后在到达时应用其余的sql语法.对于大型数据集来说效率非常低.