简体   繁体   中英

c# - fastest way to insert value from DataReader into DataGridView

I need to load the data from SQLite, then show the data in DataTable to display in DataGridView. The current approach I am using is read the data from DataReader, then loop through reader.Read() to create each DataRow.

The performance is slow for above method because I have 600k rows of data row. I had tried to use dataTable.Load(reader), but it is even slow then previous. Is there any fastest way to add the data from SQLite into DataTable.

SQLiteCommand command = new SQLiteCommand(sql, connection);
SQLiteDataReader reader = command.ExecuteReader();
while(reader.Read())
{
  DataRow dRow = dataTable.NewRow();
  dRow[''] = reader[''].ToString();
  ......
  ......
  ......
  dataTable.Rows.Add(dRow);
}

This is just a option which you might not care.

What you're doing now is, one by one, inputing 600,000 value into each column of row(of dataTable) and if columns of new row are completely inserted, the new row is added to the DataTable.

Your code inside while reading loop seems just constructing a new row and adding it to dataTable.

If you don't need to manipulate each value while reading loop which means you only need datas as they're,

SQLiteCommand command = new SQLiteCommand(sql, connection);
SqlDataAdapter da = new SqlDataAdapter(commmand);

da.Fill(dataTable);
dataGridView.ItemsSource= dt.DefaultView;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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