C# 操作 Sqlite数据库,这是一个有关分页的实例,代码来源于网络,仅供参考
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SQLite; using System.Threading; using System.Collections; using System.IO; namespace ReadCardDemo { public partial class Form3 : Form { Form4 form4 = new Form4(); int pageSize = 12; //每页显示行数 int nMax = 0; //总记录数 int totalPage = 0; //总页数=总记录数/每页显示行数 int pageCount = 0; //当前页数的记录数 int selectRow; //当前选中的行数 DataTable dataTable = new DataTable(); SQLiteDataAdapter dataAdapter; SQLiteCommand cmd; SQLiteDataReader dr; string _sql; { InitializeComponent(); //初始化数据 { dataTable.Clear(); //创建数据库 //datasource = "/Windows/ReadCardDemo/PersonId.db"; //SQLiteConnection.CreateFile(datasource); //建立数据库连接 conn.ConnectionString = datasource; //获取当前数据库的记录数 conn.Open(); cmd = new SQLiteCommand(_sql, conn); while (dr.Read()) { { nMax = Convert.ToInt32(dr.GetValue(0)); catch(Exception e) { } } conn.Close(); //计算总页数、当前页数 totalPage = (nMax / pageSize); //计算出总页数 //如果最大页数大于0,查询第一页数据。为1,则当前页要取的数据条数为总记录数,否则为页面行数 { //计算当前页数,如果记录的临时当前页数大于零 { currentPage = totalPage; //如果当前页等于总页数 { pageCount = nMax - (currentPage - 1) * pageSize; else { } getSelectRecord(); //更改状态栏的页码信息 } else //更改状态栏的页码信息 this.label1.Text = "0 / 0"; } private void getSelectRecord() { _sql = "select * from PersonId limit " + (currentPage-1)*pageSize + "," + pageCount; cmd.Connection = conn; dataAdapter = new SQLiteDataAdapter(cmd); dataAdapter.Fill(dataSet, "PersonId"); //数据绑定显示 this.dataGrid1.DataSource = bindingSource1; } //删除某一条记录 private void menuItem1_Click_1(object sender, EventArgs e) selectRow = this.dataGrid1.CurrentRowIndex; if (selectRow >= 0) DialogResult dialogResult = MessageBox.Show("确认要删除该条记录吗?", "删除确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (dialogResult == DialogResult.OK) //删除数据库记录 object[] datas = dataTable.Rows[selectRow].ItemArray; if (delR == 1)//如果删除成功,返回1,更新dataTable里的数据 { dataTable.AcceptChanges(); InitializeDataSource(); } { MessageBox.Show("记录删除失败!"); } } { MessageBox.Show("当前无被选中记录!"); } private void bt_previous_Click(object sender, EventArgs e) { { if (currentPage > 1) currentPage--; InitializeDataSource(); } } //下一页 private void bt_next_Click(object sender, EventArgs e) if (totalPage > 0) { { currentPage++; } } //执行删除数据记录操作 { string delStr = ""; if (id != null) { } else//如果ID为空,则清空所有记录 delStr = "delete from PersonId"; } conn.Open(); cmd.CommandText = delStr; int delResult = cmd.ExecuteNonQuery(); conn.Close(); } private void menuItem2_Click(object sender, EventArgs e) { { DialogResult dialogResult = MessageBox.Show("确认要删除所有记录吗?", "删除确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); { dataTable.Clear(); dataTable.AcceptChanges(); } } { MessageBox.Show("当前无记录可删除!"); } { selectRow = this.dataGrid1.CurrentRowIndex; { //获取数据记录信息并显示 form4.initData(datas); form4.Show(); } { InitializeDataSource(); } } }