简体   繁体   English

DataAdapter.Fill太慢

[英]DataAdapter.Fill too slow

I know DataAdapters have performance issues, but are there any ways around it that might be faster? 我知道DataAdapters存在性能问题,但是有没有解决的方法可能会更快? At the moment, the DataAdapter.Fill method is taking 5-6 seconds on 3000 records, which is too slow for my app. 目前,DataAdapter.Fill方法对3000条记录花费5-6秒,这对于我的应用程序来说太慢了。 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. 如果我删除Fill行并仅执行SQL(使用SQLCE),则需要20毫秒,因此我猜测查询不是问题。 I've tried adding BeginLoadData on the datatable, but it makes no difference to the performance. 我试过在数据表上添加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;


           }
       }
  }

Are there better ways to fill a DataGridView or speed up the Fill method? 有没有更好的方法来填充DataGridView或加快Fill方法?

您可以改为将DataGridView绑定到DataReader ,但这可能不会好得多,因为将3000行加载到DataGridView中并不是很方便。

the core problem is loading 3000 for the user at once. 核心问题是一次为用户加载3000。 no matter how to load 300 records the amount of data is the problem. 无论如何加载300条记录,数据量都是问题。 implement paging within the sql query to allow the user to view a subset of records. 在sql查询中实现分页,以允许用户查看记录的子集。 the user can then navigate to more records when they need to. 然后,用户可以在需要时导航到更多记录。

Use BatchUpdate/BatchInsert. 使用BatchUpdate / BatchInsert。 Make sure you specify UpdateBatchSize = 3000 (number of records you have) 确保指定UpdateBatchSize = 3000(具有的记录数)

Here is an example on how to do it: BatchInsert 这是有关如何执行此操作的示例: BatchInsert

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM