1、下载System.Data.sqlite.DLL,下载地址:
ADO.NET 2.0 Provider for sqlite - Browse /sqlite for ADO.NET 2.0 at SourceForge.net
http://sourceforge.net/projects/sqlite-dotnet2/files/SQLite%20for%20ADO.NET%202.0/
安装后会在C:\Program Files\sqlite.NET\bin\CompactFramework下面有 System.Data.sqlite.dll 和sqlite.Interop.066.DLL两个文件
在VS2008项目中引用System.Data.sqlite.DLL即可。
2、创建表、读取数据等操作
using System.Data;
using System.Data.sqlite;
private void BtnGetData_Click(object sender,EventArgs e)
{
//创建一个空数据库
string datasource=System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) +"\test.db"; //数据库文件放在了程序运行根目录下
sqliteConnection.CreateFile(datasource);
//连接数据库
sqliteConnection conn = new System.Data.sqlite.sqliteConnection();
conn.ConnectionString = "Data Source=" + datasource + ";Pooling=true;FailIfMissing=false";
conn.Open();
//创建表
sqliteCommand cmd = new System.Data.sqlite.sqliteCommand();
string sql = "CREATE TABLE users(username varchar(20),password varchar(20))";
cmd.CommandText=sql;
cmd.Connection=conn;
cmd.ExecuteNonQuery();
//插入数据
sql = "INSERT INTO [users] VALUES('AAA','123556')";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
//取出数据
sql = "SELECT * FROM [users]";
cmd.CommandText = sql;
System.Data.sqlite.sqliteDataReader reader = cmd.ExecuteReader();
StringBuilder sb = new StringBuilder();
while (reader.Read())
{
sb.Append("username:").Append(reader.GetString(0)).Append("\n");
sb.Append("password:").Append(reader.GetString(1));
}
MessageBox.Show(sb.ToString());
}
注意: 程序编译完后需要将安装后的两个DLL文件复制到WINCE的程序运行目录下,即:System.Data.sqlite.dll和sqlite.Interop.066.DLL(该文件需要更名为sqlite.Interop.DLL)