Why does the .NET 2.0 application crash in .NET 4.0 when I use the OleDbDataAdapter object without the OleDBConnection object?

This is a .NET 2.0 application written using VS 2005. It works fine on systems running .NET 2.0, but has severe crashes on systems running .NET 4.0. Here's the critical section of code:

      string selectCommand1 = ....
      string connectionString1 = ....
      using (OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand1, connectionString1))
        {
            try
            {
                adapter.Fill(table1);
            }
            catch
            {
               MessageBox.Show("error");
            }
        }

      string selectCommand2 = ....
      string connectionString2 = ....
      using (OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand2, connectionString2))
        {
            try
            {
                adapter.Fill(table2);
            }
            catch
            {
               MessageBox.Show("error");
            }
        }

Again, it works under .NET 2.0, a crash in .NET 4.0

ConnectionStrings 1 and 2 refer to different .xls files.

I found that the way around this problem is to declare and initialize a field variable of type OleDbConnection, set the ConnectionString and Open () property before the OleDbDataAdapter statement using the instruction. In this way:

 OleDbConnection connection = new OleDbConnection();

  .......

        connection.ConnectionString = connectionString1;
        connection.Open();
        using (OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand1, connection))
        {
            try
            {
                adapter.Fill(table1);
            }
            catch
            {
                MessageBox.Show("error");
            }
        }

        connection.Close();
        connection.ConnectionString = connectionString2;
        connection.Open();
        using (OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand2, connection))
        {
            try
            {
                adapter.Fill(table2);
            }
            catch
            {
                MessageBox.Show("error");
            }
        }

, , ( ) .NET 4.0, , .

, , , .NET 4.0.

- , .NET 4.0 , ?

+5
2

"Application Verifier". .

+3

( connection.open) adapter.Fill(table1)?

    using (OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand1, connectionString1))
    {
        using (System.Data.DataTable table1 = new System.Data.DataTable()) {

            try
            {
                adapter.Fill(table1);
            }
            catch
            {
               MessageBox.Show("error");
            }
        }
    }
+2

All Articles