Sqlite在嵌入式产品中使用的轻型数据库,只需几百k的内存,支持Windows/Linux/Unix等等主流的操作系统,支持c#。php。java,tcl,处理速度快。支持的SQL语句不会逊色于其他开源数据库。
如果把Sqlite看做事SQLServer,那么 ADO.NET2.0 Provider for Sqlite就相当于System.Data.SqlClient,而SqliteAdmin则是SQLServer Management Studio Express.
在网上下载SQLite-1.0.66.0-setup后,进行安装,在安装文件bin目录下找到System.Data.SQLite.DLL,把它加载到工程引用中,并且在代码中加上using System.Data.SQLite; 这样就已经准备好vs工程中使用SQLite的条件。
接下去通过一个例子说明它的使用:
vs2008中创建一个windows窗体项目,在form1中添加一个按钮button1,form1.cs代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
namespace SQLLiteTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private SQLiteConnection conn;
private SQLiteCommand cmd;
public void test()
{
//连接DB
try
{
conn = new SQLiteConnection("Data Source=c:\\test.db");
conn.Open();
}
catch
{ }
finally
{ }
//插入或者更新
cmd = conn.CreateCommand();
cmd.CommandText="INSERT INTO user(email,name) VALUES ('email','name')";
cmd.ExecuteNonQuery();
//选择
cmd.CommandText = "SELECT ID, name FROM user";
SQLiteDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
MessageBox.Show("ID:" + reader.GetInt16(0) + "\r" + "Name:" + reader.GetString(1));
}
}
}
private void button1_Click(object sender, EventArgs e)
{
test();
}
}
}
注意:在运行该例子之前,应该先创建数据库c:\\test.db,这里我使用一款可视化管理SQLite数据库的工具软件VisualSQLite(只需下载安装就可以)