DataAdapter.Fill 太慢
我知道 DataAdapter 存在性能问题,但是有什么方法可以更快地解决这个问题吗?目前,DataAdapter.Fill 方法在 3000 条记录上花费 5-6 秒,这对于我的应用程序来说太慢了。如果我删除 Fill
行并仅执行 SQL(使用 SQLCE),则需要 20 毫秒,因此我猜测查询不是问题。我尝试在数据表上添加 BeginLoadData
,但这对性能没有影响。
using (SqlCeConnection con = new SqlCeConnection(conString))
{
con.Open();
using (SqlCeDataAdapter dAdapter= new SqlCeDataAdapter())
{
using (SqlCeCommand com = new SqlCeCommand(query, con))
{
com.Parameters.Add("uname", textBox1.Text);
dAdapter.SelectCommand = com;
dAdapter.SelectCommand.Connection = con;
DataTable dTable = new DataTable();
dAdapter.Fill(dTable);
dataGridView1.DataSource = dTable;
}
}
}
是否有更好的方法来填充 DataGridView 或加速 Fill
方法?
I know DataAdapters have performance issues, but are there any ways around it that might be faster? At the moment, the DataAdapter.Fill method is taking 5-6 seconds on 3000 records, which is too slow for my app. If I remove the Fill
line and just execute the SQL (using SQLCE), it takes 20 milliseconds, so I'm guessing the query isn't the problem. I've tried adding BeginLoadData
on the datatable, but it makes no difference to the performance.
using (SqlCeConnection con = new SqlCeConnection(conString))
{
con.Open();
using (SqlCeDataAdapter dAdapter= new SqlCeDataAdapter())
{
using (SqlCeCommand com = new SqlCeCommand(query, con))
{
com.Parameters.Add("uname", textBox1.Text);
dAdapter.SelectCommand = com;
dAdapter.SelectCommand.Connection = con;
DataTable dTable = new DataTable();
dAdapter.Fill(dTable);
dataGridView1.DataSource = dTable;
}
}
}
Are there better ways to fill a DataGridView or speed up the Fill
method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将 DataGridView 绑定到 DataReader,但这可能并没有好多少,因为将 3000 行加载到 DataGridView 的速度并不快。
You could bind the DataGridView to a
DataReader
instead, but it may not be much better, since loading 3000 rows into a DataGridView just isn't speedy .核心问题是一次为用户加载3000个。无论如何加载300条记录的数据量都是问题。在 SQL 查询中实现分页以允许用户查看记录的子集。然后,用户可以在需要时导航到更多记录。
the core problem is loading 3000 for the user at once. no matter how to load 300 records the amount of data is the problem. implement paging within the sql query to allow the user to view a subset of records. the user can then navigate to more records when they need to.
使用批量更新/批量插入。确保指定 UpdateBatchSize = 3000(您拥有的记录数)
以下是有关如何执行此操作的示例: 批量插入
Use BatchUpdate/BatchInsert. Make sure you specify UpdateBatchSize = 3000 (number of records you have)
Here is an example on how to do it: BatchInsert