我正在使用下面的代码从SQLite3数据库中的表中的数据读取到ListView中
目前,我将记录限制为仅显示200条,因为显示所有记录的时间过长。 (约30,000条记录)
我想提供一个显示所有记录的选项,如果选择了该选项以能够加载每条记录。
正如我说的,这需要花费大量时间,我想使用进度条来显示其状态。
我以前从未使用过进度栏,所以我不知道它们是如何工作的。是在我的代码中添加一些额外的代码行以允许使用进度条,还是我必须对其进行线程化并使用后台工作程序?
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);
}
}
最佳答案
ID使用DataView,因此我可以使用RowStateFilter获得所需记录的计数。然后使用foreach而不是for语句遍历DataView。
ProgressBar _Bar = new ProgressBar();
DataView _View = new DataView();
_View.Table = datatable;
_View.RowStateFilter = DataViewRowState.CurrentRows;
_Bar.Minimum = 0;
_Bar.Maximum = _View.ToTable().Rows.Count;
foreach (DataRow datarow in _View.ToTable().Rows)
{
// 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);
_Bar.PerformStep();
}
以下增加了进度条:
_Bar.PerformStep();
如果不运行此代码,我怀疑进度栏实际上会像您认为的那样出现问题。您的进程正在UI线程上运行,因此它将阻止UI更新。您可能要考虑在另一个线程上运行此过程。这是另一个主题。
关于c# - 在读取SQL#的SQLite数据库上实现进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10016090/