2

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.

GhostDZ9
  • 147
  • 1
  • 6
  • 18

2 Answers2

0

Try setting up a DSN using Control Panel>Administrative Tools>Setup data sources (ODBC)>System DSN>Add.

Give the Data Source name as MYDSN, then click on Select and browse to your Access DB.

See the snapshot.

enter image description here

after that try the code as follows:

string strDSN = "DSN=MYDSN";
string cmdText = "Insert into AccessTable (ColumnA,ColumnB) Values (?,?)";
using (OdbcConnection cn = new OdbcConnection(strDSN))
{
    using (OdbcCommand cmd = new OdbcCommand(cmdText, cn))
    {
        cn.Open();
        foreach (DataRow r in dt.Rows)
        {
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@p1", r["ColumnA"].ToString());
            cmd.Parameters.AddWithValue("@p2", r["ColumnB"].ToString());
            cmd.ExecuteNonQuery();
        }

    }
}
Tshilidzi Mudau
  • 5,472
  • 6
  • 32
  • 44
Anurag
  • 510
  • 8
  • 27
0

So after doing more investigation I have determined that the oledb driver in my connection string supports only 32bit applications. I am using a 64 bit machine and so the application properties automatically makes it a 64 bit application.

I did the following steps to resolve the issue:

  1. Right Click on the Project in Solution Explorer
  2. Select Properties
  3. Select the Build Tab
  4. Change Platform Target from 'Any CPU' to 'x86'(32Bit)
  5. Re-build the application and it should work.
GhostDZ9
  • 147
  • 1
  • 6
  • 18