简体   繁体   English

读取xls文件后,“找不到可安装的ISAM” C#异常

[英]“Could Not find Installable ISAM” C# Exception after reading xls file

We are reading xls file which is getting updated regularly from external links. 我们正在读取xls文件,该文件会定期从外部链接更新。 We have loop which read the same file after some interval of 200ms. 我们有循环,在200ms的间隔后读取相同的文件。 After reading file for 1000+ time, we are getting Error 读取文件超过1000次后,我们收到错误消息

"The Microsoft Jet database engine cannot open the file ''. It is already opened exclusively by another user, or you need permission to view its data." “ Microsoft Jet数据库引擎无法打开文件”。该文件已经由另一个用户专门打开,或者您需要查看它的数据的权限。”

Connection string is as follows: 连接字符串如下:

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;" Provider = Microsoft.Jet.OLEDB.4.0;数据源= D:\\ FeedFiles \\ TESTING1.xls;扩展属性=“ Excel 8.0; HDR = YES; IMEX = 1; Importmixedtypes = text; typeguessrows = 0;”

And after some time, it start giving "Could not find Installable ISAM". 并在一段时间后,它开始显示“找不到可安装的ISAM”。

Code as follows: 代码如下:

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

Connection string is not formatted properly. 连接字符串格式不正确。 Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM