Excutereader通过oledbcommand对象获取数据时非常慢
我正在使用 oledbcommand 对象从 dBase4 数据库获取数据并将其加载到数据表中。但在 5-10 分钟左右获取 160 条记录需要花费太多时间。 请帮助我。
代码:
using (OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data Source=" + TrendFilePath + "\\" + Pathname + ";" + @"Extended Properties=dBASE III;"))
using (OleDbCommand cm = cn.CreateCommand())
{
cn.Open();
for (int L = 0; L <= months; L++)
{
DataTable dt_Dbf = new DataTable();
From_Date = DateTime.ParseExact(frmdate, dateFormat2, provider);
From_Date = From_Date.AddMonths(L);
int month = From_Date.Month;
string year = "1" + From_Date.Year.ToString().Substring(2, 2);
if (L == 0)
{
cm.CommandText = @"SELECT * FROM 128.DBF where DATE_Y ="
+ year + " and DATE_M = " + month + " and DATE_D>=" + From_Day + "";
dt_Dbf.Load(cm.ExecuteReader(CommandBehavior.CloseConnection));
}
}
}
I am fetching data from dBase4 database by using object of oledbcommand and load it into datatable. but it is taking too much time to fetch 160 records around 5-10 minutes.
Please Help me Out.
Code:
using (OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data Source=" + TrendFilePath + "\\" + Pathname + ";" + @"Extended Properties=dBASE III;"))
using (OleDbCommand cm = cn.CreateCommand())
{
cn.Open();
for (int L = 0; L <= months; L++)
{
DataTable dt_Dbf = new DataTable();
From_Date = DateTime.ParseExact(frmdate, dateFormat2, provider);
From_Date = From_Date.AddMonths(L);
int month = From_Date.Month;
string year = "1" + From_Date.Year.ToString().Substring(2, 2);
if (L == 0)
{
cm.CommandText = @"SELECT * FROM 128.DBF where DATE_Y ="
+ year + " and DATE_M = " + month + " and DATE_D>=" + From_Day + "";
dt_Dbf.Load(cm.ExecuteReader(CommandBehavior.CloseConnection));
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑您正在执行的查询是问题所在。除非查询正在过滤大量(数百万/数十亿)数据集,否则我不认为它有问题 - 即使您正在执行 12 个(我假设每个月?)这些查询。另外,在我写作时,您是否打算使用数据表?每个月,您都会填充表,然后将其丢弃。 (由于您提到数据库只有 ~300k,因此查询本身不可能占用任何大量时间)。
可能与网络或文件本身有关。正如我所说,您正在执行 12 个查询,但在每个查询成功后都会断开连接。这意味着您需要建立 12 个连接,并运行 12 个查询。您应该调查是否可以为需要执行的一组查询维护单个连接。
另一种可能性是锁定。数据库文件是否能够维护多个连接并为不同用户执行多个查询?我根本不了解 dbase 产品,但如果它是单用户/单线程,则可能是其他人正在使用该数据库并将您锁定一段时间。
尝试将数据库复制到本地计算机,更改连接字符串以引用本地副本,然后再次运行此代码。如果执行时间显着减少,我相信您可以自信地说存在某种网络问题。从那时起,您(可能)需要向组织中的某人寻求帮助。 5-10 分钟 12 个看似简单的查询简直是个笑话。
如果网络是问题的原因,您可以采取多种选择。
I doubt that the query you're executing is the problem. Unless the query is filtering on a massive (millions/billions) data set, I don't see a problem with it - even though you're executing 12 (I'm assuming for each month?) of these queries. Also, while I'm writing, were you planning on using the DataTable at all? For each month, you populate the table, then throw it away. (Since you mention the database is only ~300k, it's impossible that the queries themselves are taking up any significant time).
It probably has something to do with the network, or the file itself. As I said, you're executing 12 queries, but tearing down the connection as each one succeeds. This means that you need to establish 12 connections, along with running 12 queries. You should investigate whether or not you can maintain a single connection for the set of queries you need to execute.
Another possibility is locking. Is the database file capable of maintaining multiple connections and executing multiple queries for different users? I don't know the dbase product at all, but if it is single user / single threaded, it could be a case that other people are using the database and locking you out of it for a time.
Try copying the database to your local machine, changing the connection string to reference the local copy, and run this code again. If the execution time is significantly reduced, I believe you'll be able to confidently say that there is some kind of network issue. From that point, you will (probably) need to seek help from someone in your organisation. 5-10 minutes for 12 seemingly simple queries is a joke.
If the network is the cause of the issue, there are several options that you can take.