为什么我的.NET .NET 4.0下2.0应用程序崩溃时,我用OleDbDataAdapter的对象没有OleDbConnection对象? [英] why does my .NET 2.0 application crash under .NET 4.0 when I use a OleDbDataAdapter object without an OleDBConnection object?

查看:189
本文介绍了为什么我的.NET .NET 4.0下2.0应用程序崩溃时,我用OleDbDataAdapter的对象没有OleDbConnection对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个.NET 2.0应用程序使用VS 2005,它工作正常运行.NET 2.0的系统编写的,但在运行.NET 4.0的系统硬盘崩溃。下面是的code中的关键部分:

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");
            }
        }

同样,.NET 2.0下工作,.NET 4.0下崩溃

Again, it works under .NET 2.0, crashes under .NET 4.0

的ConnectionStrings 1和; 2引用不同的.xls文件。

ConnectionStrings 1 & 2 reference different .xls files.

我发现,解决这个问题的方法是,声明并初始化类型的OleDbConnection的字段变量,设置OleDbDataAdapter的的使用语句之前ConnectionString属性和Open()它。像这样:

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");
            }
        }

这是很难相信,这就是为什么我的应用程序中的硬盘崩溃.NET 4.0下(没有错误消息)的原因,但除去code一根线的时间和重新编译一遍又一遍后,我发现,要是问题的原因。

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.

我很高兴我解决了这个问题,但我不满意的是:第一code不能与.NET 4.0下运行。

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.

有人能解释为什么.NET 4.0不喜欢用code像上面的?

Can someone please explain why .NET 4.0 doesn't like to work with code like the one above?

推荐答案

问题是应用程序验证。卸载它解决了这个问题。

The problem was "Application Verifier". Uninstalling it solved the problem.

这篇关于为什么我的.NET .NET 4.0下2.0应用程序崩溃时,我用OleDbDataAdapter的对象没有OleDbConnection对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆