Reputation: 11
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);
}
}
Upvotes: 0
Views: 5733
Reputation: 94625
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);
Upvotes: 1