I am reading data from a table in an SQLite3 database into a ListView using the code below
At the moment I am limiting the records to display only 200 as displaying all of them takes way too long. (Some 30,000 records)
I would like to give an option to display all the records and if that option is chosen to be able to load every record.
As I said it takes a fair amount of time and I would like to use a progress bar to show the status of it.
I have never used a progress bar before so I don't know how they work. Would it be the case of adding a few extra lines of code to my code to allow the use of a progress bar or would I have to thread it and use a background worker?
private void cmdReadDatabase_Click(object sender, EventArgs e)
{
listView4.Columns.Add("Row1", 50);
listView4.Columns.Add("Row2", 50);
listView4.Columns.Add("Row3", 50);
listView4.Columns.Add("Row4", 50);
listView4.Columns.Add("Row5", 50);
listView4.Columns.Add("Row6", 50);
listView4.Columns.Add("Row7", 50);
try
{
string connectionPath = Path.Combine(@"C:\", @"temp\");
SQLiteConnectionStringBuilder csb = new SQLiteConnectionStringBuilder();
csb.DataSource = Path.Combine(connectionPath, "database.db");
SQLiteConnection connection = new SQLiteConnection(csb.ConnectionString);
connection.Open();
// Query to read the data
SQLiteCommand command = connection.CreateCommand();
string query = "SELECT * FROM table_name LIMIT 200 ";
command.CommandText = query;
command.ExecuteNonQuery();
SQLiteDataAdapter dataAdaptor = new SQLiteDataAdapter(command);
DataSet dataset = new DataSet();
dataAdaptor.Fill(dataset, "dataset_name");
// Get the table from the data set
DataTable datatable = dataset.Tables["dataset_name"];
listView4.Items.Clear();
// Display items in the ListView control
for (int i = 0; i < datatable.Rows.Count; i++)
{
DataRow datarow = datatable.Rows[i];
if (datarow.RowState != DataRowState.Deleted)
{
// Define the list items
ListViewItem lvi = new ListViewItem(datarow["Row1"].ToString());
lvi.SubItems.Add(datarow["Row2"].ToString());
lvi.SubItems.Add(datarow["Row3"].ToString());
lvi.SubItems.Add(datarow["Row4"].ToString());
lvi.SubItems.Add(datarow["Row5"].ToString());
lvi.SubItems.Add(datarow["Row6"].ToString());
lvi.SubItems.Add(datarow["Row7"].ToString());
// Add the list items to the ListView
listView4.Items.Add(lvi);
}
}
connection.Close();
}
catch(SQLiteException ex)
{
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK);
}
}