DataAdapter.Fill too slow

2020-06-21 23:09发布

问题:

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?

回答1:

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 .



回答2:

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.



回答3:

Use BatchUpdate/BatchInsert. Make sure you specify UpdateBatchSize = 3000 (number of records you have)

Here is an example on how to do it: BatchInsert