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 currentPage = 1; //当前页号
int pageCount = 0; //当前页数的记录数
int selectRow; //当前选中的行数
DataSet dataSet = new DataSet();
DataTable dataTable = new DataTable();
SQLiteDataAdapter dataAdapter;
SQLiteConnection conn = new SQLiteConnection();
SQLiteCommand cmd;
SQLiteDataReader dr;
string datasource;
string _sql;
public Form3()
{
InitializeComponent();
}
//初始化数据
public void InitializeDataSource()
{
dataTable.Clear();
dataTable.AcceptChanges();
//创建数据库
//datasource = "/Windows/ReadCardDemo/PersonId.db";
//SQLiteConnection.CreateFile(datasource);
//建立数据库连接
datasource = @"Data Source=/Windows/ReadCardDemo/PersonId.db";
conn.ConnectionString = datasource;
//获取当前数据库的记录数
_sql = "select count(*) from PersonId";
conn.Open();
cmd = new SQLiteCommand(_sql, conn);
dr = cmd.ExecuteReader();
while (dr.Read())
{
try
{
nMax = Convert.ToInt32(dr.GetValue(0));
}
catch(Exception e)
{
相关文档:
1. 打开内存数据库
直接打开一个名为":memory:"或者 名字直接为空的数据库, 两种数据库略微有些区别。
参考 :http://www.sqlite.org/inmemorydb.html
2. 保存内存数据库
3. 判断表是否存在
通过查询系统表sqlite_master来获取相关信息
select * f ......
★如何权衡?
当你在权衡某个场合是否应该使用SQLite时,(在技术层面)至少要考虑如下几点:
◇能否发挥SQLite的某些特长?
◇是否还有其它的替代方案?
◇是否有啥潜在的技术风险?
想清楚上述问题之后,再做出决策。
★SQLite的特点
关于SQLite的特长,在上次的帖子中已经介绍过了。 ......
  ......
SQLite不仅可以把数据库 放在硬盘上,还可以放在内存中(sqlite3_open(":memory:", &db)),经测试,同样条件下数据库放在内存中比放在硬盘上插入记录速度快差不多3倍。但数据库放在内存中时有如下缺陷:
1、断电或程序崩溃后数据库就会消失,你需要定期Attach到硬盘上备份;
2、在内存中的数据库不能被别的进程 ......
判断表存在的方法很简单,网上很多:
SELECT COUNT(*) from sqlite_master where type='table' and name='%s'" % tname;
那么判断字段是否存在, 或者说如何判断表的版本是否最新就只需要:
select * from sqlite_master where tbl_name='tblContactList';
sqlite_master 的表结构如下:
type |name  ......