在ADO中处理多个到多个Excel文件的连接的正确方法是什么?NET 4.0吗?

时间:2022-05-19 13:51:29

I have a piece of code written using VS 2005 that works fine in computers running .NET 2.0 but hard crashes in computers running .NET 4.0.

我有一段使用VS 2005编写的代码,在运行。net 2.0的计算机上运行良好,但是在运行。net 4.0的计算机上却出现了严重的崩溃。

The section of hte code that's causing the problem is a call to the DataAdapter's Fill() method. The code looks as follows:

引起问题的hte代码部分是对DataAdapter的Fill()方法的调用。代码如下:

private void button_Click(object sender, EventArgs e)
{
    DataTable dt1 = new DataTable();
    string connectionString = ... //connects to excelfile1.xls
    string selectCommand = "SELECT * FROM [Sheet1$]";

    using(OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand, connectionString))
    {
        adapter.SelectCommand.Connection.Open();
        adapter.Fill(dt1);
    }

    DataTable dt2 = new DataTable();
    connectionString = ... //connects to excelfile2.xls

    using(OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand, connectionString))
    {
        adapter.SelectCommand.Connection.Open();
        adapter.Fill(dt2);
    }
}

Several things happen if I make slight modifications to the code:

如果我对代码稍加修改,会发生以下几件事:

  1. if I remove the two calls to OleDbConnection.Open(), the code will work just fine with .NET 2.0, but hard crash with .NET 4.0.

    如果我删除了对oledbconnector . open()的两个调用,那么代码在。net 2.0上就可以正常工作,但是在。net 4.0上就很难崩溃了。

  2. if I remove only the second call to OleDbConnection.Open(), the code will work fine with .NET 2.0 and .NET 4.0. Alas, I need to retrieve data from two separate excel files and fill two separate DataTables each time the event is fired.

    如果我只删除第二个对oledbconnector . open()的调用,那么代码在。net 2.0和。net 4.0中就可以正常工作了。唉,我需要从两个独立的excel文件中检索数据,并在每次触发事件时填充两个独立的数据。

  3. if I use both calls to OleDbConnection.Open(), as shown in the code above, the code will work fine with .NET 2.0 and .NET 4.0, BUT hard crash with .NET 4.0 the second or third time the user clicks on the button and the procedure runs.

    如果我同时使用对oledbconnector . open()的调用,如上面的代码所示,这段代码在。net 2.0和。net 4.0上运行得很好,但是在用户第二次或第三次单击按钮时,在。net 4.0上就会出现硬崩溃。

My guess is that .NET 4.0 manages connections differently than .NET 2.0 and I'm missing some very important step.

我的猜测是。net 4.0与。net 2.0管理连接的方式不同,我漏掉了一些非常重要的步骤。

Can someone please tell how I should write the above code in such a way that it will work fine under .NET 2.0 and .NET 4.0?

谁能告诉我该如何在。net 2.0和。net 4.0下编写上述代码?

1 个解决方案

#1


0  

The problem was "Application Verifier".

问题是“应用程序验证器”。

#1


0  

The problem was "Application Verifier".

问题是“应用程序验证器”。