OledbConnection Close and/or Dispose are very slow, take 20s to execute

混江龙づ霸主 提交于 2019-12-01 22:52:55

问题


While using OleDbConnection to access sheet names of the excel files I encountered a problem. Everything in the code executes fine and works, until the OleDbConnection closes at the end of the using statement. This causes the program to stop for 15-20 seconds or more!

I've tried getting rid of the using statement and individually using the OleDbConnection.Close() and OleDbConnection.Dispose() methods, both of those take incredibly long to execute as well. If I never close the connection at all, the program runs perfectly.

//Closing connection is inredibly slow now for some reason
                using (OleDbConnection conn = new OleDbConnection(connectString))
                {
                    conn.Open();
                    DataTable dbSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    sheetNames.Clear();
                    for (int i = 0; i < dbSchema.Rows.Count; i++)
                    {
                        sheetNames.Add(dbSchema.Rows[i]["TABLE_NAME"].ToString());
                    }
                    comboBox1.Items.Clear();
                    comboBox1.Items.AddRange(sheetNames.ToArray());
                    comboBox1.SelectedIndex = 0;
                    //conn.Close();
                }
//Connection strings
//XLS
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}; Extended Properties='Excel 8.0;HDR=No;IMEX=1;';" />
//XLSX
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties='Excel 12.0;HDR=No;IMEX=1;';"

A very strange thing I noticed is that the connection takes very long to close for every type of excel file EXCEPT .xls files (from 1997-2003 era Excel).

On top of all this I've searched everywhere on the internet and cannot actually find anyone who has found a solution to this, only unsolved forum posts. Here are a few links I've found from those with the same problem:

OleDbConnection close method taking long execution time,

OleDbConnection Dispose very slow (2s)

http://forums.asp.net/t/2059359.aspx?OledbConnection+Close+taking+too+long+to+execute


回答1:


Sorry I don't have enough reputation to comment but would like to put my two cents here.

I was facing the same problem that OleDbConnection closes take forever. And for my case it was because I mistakenly use nested using. After resolving these nested using resolves the problem for my case.

Hope it help some novices like me.



来源:https://stackoverflow.com/questions/38212700/oledbconnection-close-and-or-dispose-are-very-slow-take-20s-to-execute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!