0

We are reading xls file which is getting updated regularly from external links. We have loop which read the same file after some interval of 200ms. After reading file for 1000+ time, we are getting Error

"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."

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

And after some time, it start giving "Could not find Installable 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);
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
rajeshemailto
  • 11
  • 1
  • 1
  • 1. This code (without *txtlog* related lines) ran just fine on my machine for over than 1000 times. So the trouble might relate to the data inside your xls file 2. Where the exception raises ? Which row ? 3. did you tried to add *;Mode=Share Exclusive;* to the connection string ? – itsho Aug 20 '13 at 07:04

1 Answers1

1

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);
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186