问题描述
我通常使用Java,并且仅使用C#读取旧的MS Access 95数据库,因此我对此并不陌生。 我有一个C#脚本,可打开一个OleDBConnection到访问数据库并从中读取一些数据:
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();
}
}
}
该项目是一个控制台应用程序,当我在Debugging
上具有配置模式时,在Visual Studio中单击“运行”时,它可以很好地工作。但是,当我尝试在没有调试模式的情况下运行它或发布它并尝试运行.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();
的连接?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)