I am building a C# application that reads a text file, writes the data read into an access 97 DB, it then reads the db and writes a file. I seem to be having some issues with my OLEDB connection string. Below is my code, anyone have any ideas why I am getting this exception?
The database does exist and I only open the connection before I execute the query and then I close it again.
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();
Looking at the connection object I notice two things.
Server Version says:'cnn.ServerVersion' threw an exception of type 'System.InvalidOperationException'
Also cnn.Open() and cnn.OpenAsync() both don't change the state of the connection.