【问题标题】:"Could Not find Installable ISAM" C# Exception after reading xls file读取 xls 文件后“找不到可安装的 ISAM”C# 异常
【发布时间】:2012-06-14 00:51:27
【问题描述】:

我们正在读取 xls 文件,该文件会定期从外部链接更新。我们有一个循环,它在 200 毫秒的间隔后读取相同的文件。读取文件超过 1000 次后,我们收到错误

“Microsoft Jet 数据库引擎无法打开文件 ''。它已被其他用户以独占方式打开,或者您需要权限才能查看其数据。”

连接字符串如下:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\FeedFiles\TESTING1.xls;Extended Properties="Excel 8.0;HDR=YES;IMEX=1;Importmixedtypes=text;typeguessrows=0;"

一段时间后,它开始给出“找不到可安装的 ISAM”。

代码如下:

String xlsConnString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;Importmixedtypes=text;typeguessrows=0;""", feedFiles.FullName);

OleDbDataAdapter dataAdapter = new OleDbDataAdapter(xlsQuery, xlsConnString);
while (true)
{
    try
    {
        //Exception handling if not able to read xls file.
        DataSet dataSet = new DataSet();
        dataAdapter.Fill(dataSet);
        String fileName = dirstr + "Temp-";
        System.IO.StreamWriter file = new System.IO.StreamWriter(fileName + ".tmp");

        file.WriteLine(dataSet.GetXml());
        file.Close();

        try
        {
            File.Replace(fileName + ".tmp", dirstr + "Temp-" + filecount.ToString() + ".xml", null);
        }
        catch (Exception ex)
        {
            try
            {
                File.Move(fileName + ".tmp", dirstr + "Temp-" + filecount.ToString() + ".xml");
            }
            catch
            {
                Thread.Sleep(xlsThreadSleep);
            }
        }

        filecount++;
        if (filecount > maxFileCnt)
        {
            filecount = 0;
        }
        dataSet.Clear();
        dataSet = null;

        Thread.Sleep(xlsThreadSleep);
    }
    catch (Exception ex)
    {
        txtlog.BeginInvoke(new DelegateForTxtLog(functionFortxtLog), "Exception occured > " + ex.Message);
        feedFileIndex++;

        if (feedFileIndex == feedFiles.Length)
        {
            feedFileIndex = 0;
        }
        dataAdapter.Dispose();
        dataAdapter = null;

        Thread.Sleep(xlsThreadSleep * 20);

        xlsConnString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;Importmixedtypes=text;typeguessrows=0;""", feedFiles[feedFileIndex].FullName);
        txtlog.BeginInvoke(new DelegateForTxtLog(functionFortxtLog), "Trying connecting with connection string > " + xlsConnString);

        dataAdapter = new OleDbDataAdapter(xlsQuery, xlsConnString);
        txtlog.BeginInvoke(new DelegateForTxtLog(functionFortxtLog), "Now reading file > " + feedFiles[feedFileIndex].FullName);
    }
}

【问题讨论】:

  • 1.这段代码(没有 txtlog 相关行)在我的机器上运行了超过 1000 次。所以问题可能与您的 xls 文件中的数据有关 2. 哪里引发了异常?哪一排? 3. 您是否尝试将 ;Mode=Share Exclusive; 添加到连接字符串?

标签: c# .net excel isam


【解决方案1】:

连接字符串的格式不正确。试试这个:

String xlsConnString = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=
             {0};Extended Properties=\"Excel 8.0;HDR=YES;
             IMEX=1;Importmixedtypes=text;typeguessrows=0;\"", feedFiles.FullName);

【讨论】:

  • 但是我可以使用相同的连接字符串读取文件近 1000 多次,并且在递归读取时出现上述错误。
  • 试过但同样的错误。另外,还有一点..这个完整的代码是在线程中编写的。
猜你喜欢
  • 1970-01-01
  • 2011-04-04
  • 1970-01-01
  • 2012-08-01
相关资源
最近更新 更多