K
ksedran
Hi Experts,
I am having an issue with an Excel COM Object not being released.
I have narrowed it down to a function with an OleDbDataAdapter.
When I run this way:
//Open spreadsheet
//do work
//Release COM Objects
//Close spreadsheet
All the COM object are released and the EXCEL.exe process closes in the task
manager.
But if I run this way:
//Open spreadsheet
DataTable table = GetSheetData(fileName, "Sheet1");
//do work
//Release COM Objects
//Close spreadsheet
There is COM Object not released and the EXCEL.exe process doesn't close in
the task manager.
I have spent the day making sure all the other code in the app is releasing
the COM Objects. As soon as I add the DataTable table =
GetSheetData(fileName, "Sheet1"); the EXCEL.exe process doesn't close. I have
all the code commented out that would use that table so there is no reference
to it in the code. So it must be with the OleDbDataAdapter.
Any advice????
Thanks in advance.
private DataTable GetSheetData(string fileName, string agencyName)
{
using (OleDbConnection connection =
CreateExcelConnection(fileName))
{
OleDbDataAdapter adapter = new
OleDbDataAdapter(string.Format("SELECT * FROM [" + agencyName + "$]"),
connection);
DataSet data = new DataSet();
adapter.Fill(data);
connection.Close();
adapter.Dispose();
return data.Tables[0];
}
}
private OleDbConnection CreateExcelConnection(string fileName)
{
StringBuilder connectionStringBuilder = new
StringBuilder(string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source={0}", fileName));
const string EXCEL_EXTENDED_CONNECTION_PROPERTIES = @";Extended
Properties=""Excel 8.0;HDR=Yes""";
connectionStringBuilder.Append(EXCEL_EXTENDED_CONNECTION_PROPERTIES);
return new OleDbConnection(connectionStringBuilder.ToString());
}
--
I am having an issue with an Excel COM Object not being released.
I have narrowed it down to a function with an OleDbDataAdapter.
When I run this way:
//Open spreadsheet
//do work
//Release COM Objects
//Close spreadsheet
All the COM object are released and the EXCEL.exe process closes in the task
manager.
But if I run this way:
//Open spreadsheet
DataTable table = GetSheetData(fileName, "Sheet1");
//do work
//Release COM Objects
//Close spreadsheet
There is COM Object not released and the EXCEL.exe process doesn't close in
the task manager.
I have spent the day making sure all the other code in the app is releasing
the COM Objects. As soon as I add the DataTable table =
GetSheetData(fileName, "Sheet1"); the EXCEL.exe process doesn't close. I have
all the code commented out that would use that table so there is no reference
to it in the code. So it must be with the OleDbDataAdapter.
Any advice????
Thanks in advance.
private DataTable GetSheetData(string fileName, string agencyName)
{
using (OleDbConnection connection =
CreateExcelConnection(fileName))
{
OleDbDataAdapter adapter = new
OleDbDataAdapter(string.Format("SELECT * FROM [" + agencyName + "$]"),
connection);
DataSet data = new DataSet();
adapter.Fill(data);
connection.Close();
adapter.Dispose();
return data.Tables[0];
}
}
private OleDbConnection CreateExcelConnection(string fileName)
{
StringBuilder connectionStringBuilder = new
StringBuilder(string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source={0}", fileName));
const string EXCEL_EXTENDED_CONNECTION_PROPERTIES = @";Extended
Properties=""Excel 8.0;HDR=Yes""";
connectionStringBuilder.Append(EXCEL_EXTENDED_CONNECTION_PROPERTIES);
return new OleDbConnection(connectionStringBuilder.ToString());
}
--