C# 如何使用OLEDB连接解决连接关闭错误

C# 如何使用OLEDB连接解决连接关闭错误,c#,ms-access,oledbconnection,C#,Ms Access,Oledbconnection,我通常使用Java,只使用C#读取旧的MS Access 95数据库,所以我对这一点不熟悉。 我有一个C#脚本,它打开一个到access db的OLEDB连接,并从中读取多个数据: using System; using System.Data.OleDb; namespace ReadMsAccessDB { class Program { static void Main(string[] args) { const

我通常使用Java,只使用C#读取旧的MS Access 95数据库,所以我对这一点不熟悉。 我有一个C#脚本,它打开一个到access db的OLEDB连接,并从中读取多个数据:

using System;
using System.Data.OleDb;


namespace ReadMsAccessDB
{
    class Program
    {
        static void Main(string[] args)
        {
            const string connectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:/workspace-csharp/ReadMsAccessDB/ReadMsAccessDB/File/remote.mdb; Persist Security Info = True";
        
            using (var con = new OleDbConnection(connectionString))
            {
                con.OpenAsync();
                var cmd = con.CreateCommand();
                cmd.CommandText = "select * from summary_0199_550";
                cmd.Connection = con;

                using (var dataReader = cmd.ExecuteReader())
                {
                    while (dataReader.Read())
                    {
                        var output = "ScanDateTime: " + dataReader.GetValue(3) + "\n"
                                                        + "RCP_Name: " + dataReader.GetValue(13) + "\n"
                                                        + "Slot: " + dataReader.GetValue(14) + "\n"
                                                        + "DFCT_Tot: " + dataReader.GetValue(19) + "\n"
                                                        + "Area_Count: " + dataReader.GetValue(22) + "\n"
                                                        + "Part1: " + dataReader.GetValue(23) + "\n"
                                                        + "Part2: " + dataReader.GetValue(24) + "\n"
                                                        + "Part3: " + dataReader.GetValue(25) + "\n"
                                                        + "Part4: " + dataReader.GetValue(26) + "\n"
                                                        + "Part5: " + dataReader.GetValue(27) + "\n"
                                                        + "Part6: " + dataReader.GetValue(28) + "\n"
                                                        + "Part7: " + dataReader.GetValue(29) + "\n"
                                                        + "Part8: " + dataReader.GetValue(30) + "\n"
                                                        + "HazeRegion: " + dataReader.GetValue(33) + "\n"
                                                        + "HazeAverage: " + dataReader.GetValue(34) + "\n"
                                                        + "HazePeak: " + dataReader.GetValue(35) + "\n\n";
                        Console.WriteLine(output);
                    }

                }

            }

            Console.ReadLine();
        }
    }
}
该项目是一个控制台应用程序,当我在Visual Studio中点击Run时,配置模式为“调试”,它工作得非常好。但是,当我尝试在没有调试模式或发布它的情况下运行它,并尝试运行.exe文件时,会出现如下连接错误:

Unhandled exception. System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is closed.
at System.Data.OleDb.OleDbConnection.CheckStateOpen(String method)
at System.Data.OleDb.OleDbCommand.ValidateConnection(String method)
at System.Data.OleDb.OleDbCommand.ValidateConnectionAndTransaction(String method)
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
at ReadMsAccessDB.Program.Main(String[] args) in C:\workspace-csharp\ReadMsAccessDB\ReadMsAccessDB\Program.cs:line 18

我不明白为什么它说连接状态是关闭的,因为我正在用
con.OpenAsync()打开连接在使用部分?

异步
方法需要使用
await
关键字等待。否则,当前方法的执行将在
OpenAsync
方法完成之前继续。如果存在非异步变量
Open
,您可能可以使用它。当我仅使用
Open
时,我会得到一个异常,即
OLEDB.4.0
提供程序未在本地计算机上注册:
未处理的异常。System.InvalidOperationException:“Microsoft.Jet.OLEDB.4.0”提供程序未在本地计算机上注册。
这是否有帮助?我安装了
Ace
驱动程序,并将提供程序设置为
Provider=Microsoft.Ace.OLEDB.12.0如回答中所述,但它不起作用。我也有同样的例外。但是后来我将项目的目标平台设置为x86,现在它似乎可以与
Jet
驱动程序一起工作。如果文件格式是mdb,那么可以使用Jet。如果文件格式为accDB,则必须使用ACE。是的,您必须将目标CPU设置/强制为x86。当然,除非将代码连接起来等待连接完成,否则无法进行异步打开。如果您要等待,那么您最好不使用异步代码打开。通过以编译代码的形式运行,它的运行速度更快,因此打开的代码还没有完成,代码就直接从上面掉下来,而不用等到打开并开始运行需要连接的代码。因此,编译后的代码运行得更快—如果没有开放式等待,速度太快。