我正在构建一个读取文本文件的 C# 应用程序,将读取的数据写入访问 97 DB,然后读取 db 并写入文件。我的 OLEDB 连接字符串似乎有一些问题。以下是我的代码,有人知道为什么我会收到此异常吗?
数据库确实存在,我只在执行查询之前打开连接,然后再次关闭它。
string fileName = "JobLogReport.mdb";
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + fileName + ";";
OleDbConnection cnn = new OleDbConnection(connectionString);
string[] fileNames = openFileDialog1.FileNames;
int lineSkips = 2;
int fileCount = 1;
for (int i = 0; i < fileNames.Length; i++)
{
if (i == 0) { tbProgress.Text += "Reading Job Log File Number:" + fileCount + "\r\n"; }
if (i == 1) { tbProgress.Text += "Reading Job Log File Number:" + (fileCount + 1) + "\r\n"; }
tbProgress.Text += "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \r\n";
tbProgress.Text += "Populating Database \r\n";
tbProgress.Text += "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \r\n";
fileReader = new StreamReader(fileNames[i].ToString());
while (!fileReader.EndOfStream)
{
String file = fileReader.ReadLine();
if (file.StartsWith("IC.PXPSG"))
{
OleDbCommand command = new OleDbCommand();
char[] delimiters = new char[] { ' ' };
string[] line = file.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
command.CommandText = "INSERT INTO DocsReceived (Filename, Documents) VALUES (?,?)";
command.Parameters.AddWithValue("@filename", line[0]);
command.Parameters.AddWithValue("@documents", Int32.Parse(line[1], NumberStyles.Any, CultureInfo.InvariantCulture));
command.Connection = cnn;
cnn.Open();
command.ExecuteNonQuery();
cnn.Close();
查看连接对象,我注意到两件事。
服务器版本说:“cnn.ServerVersion”引发了“System.InvalidOperationException”类型的异常
另外 cnn.Open() 和 cnn.OpenAsync() 都不会改变连接的状态。