问题
This is a .NET 2.0 application written using VS 2005. It works fine on systems running .NET 2.0, but hard crashes on systems running .NET 4.0. Here's the critical section of the 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, crashes under .NET 4.0
ConnectionStrings 1 & 2 reference 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 property and Open() it before the OleDbDataAdapter's using statement. As so:
 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");
            }
        }
It's hard to believe that this is the reason why my app was hard crashing (no error messages) under .NET 4.0, but after removing lines of code one at a time and recompiling over and over I found that to be the cause of the problem.
I'm glad I solved the problem, but I'm not satisfied with the fact that the first code won't work with .NET 4.0.
Can someone please explain why .NET 4.0 doesn't like to work with code like the one above?
回答1:
The problem was "Application Verifier". Uninstalling it solved the problem.
回答2:
In your original code (without connection.open), have you initialized the tables prior to calling 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");
            }
        }
    }
    来源:https://stackoverflow.com/questions/11438952/why-does-my-net-2-0-application-crash-under-net-4-0-when-i-use-a-oledbdataadap