日期:2014-05-18  浏览次数:20437 次

求asp.net读写sqlite数据库的代码
谢谢,或者那有相关的文章
中文或英文

------解决方案--------------------
转自:http://www.cnblogs.com/yelsea/archive/2007/06/21/792314.html

using System.Data;
using System.Data.Common;
using System.Data.SQLite;

// 创建数据库文件
File.Delete( "test1.db3 ");
SQLiteConnection.CreateFile( "test1.db3 ");

DbProviderFactory factory = SQLiteFactory.Instance;
using (DbConnection conn = factory.CreateConnection())
{
// 连接数据库
conn.ConnectionString = "Data Source=test1.db3 ";
conn.Open();

// 创建数据表
string sql = "create table [test1] ([id] INTEGER PRIMARY KEY, [s] TEXT COLLATE NOCASE) ";
DbCommand cmd = conn.CreateCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();

// 添加参数
cmd.Parameters.Add(cmd.CreateParameter());

// 开始计时
Stopwatch watch = new Stopwatch();
watch.Start();

// 连续插入1000条记录
for (int i = 0; i < 1000; i++)
{
cmd.CommandText = "insert into [test1] ([s]) values (?) ";
cmd.Parameters[0].Value = i.ToString();

cmd.ExecuteNonQuery();
}

// 停止计时
watch.Stop();
Console.WriteLine(watch.Elapsed);
}

哎~~~~ 一个常识性的错误,我加几行代码 (新增代码标记 "// <------------------- ")。

using System.Data;
using System.Data.Common;
using System.Data.SQLite;

// 创建数据库文件
File.Delete( "test1.db3 ");
SQLiteConnection.CreateFile( "test1.db3 ");

DbProviderFactory factory = SQLiteFactory.Instance;
using (DbConnection conn = factory.CreateConnection())
{
// 连接数据库
conn.ConnectionString = "Data Source=test1.db3 ";
conn.Open();

// 创建数据表
string sql = "create table [test1] ([id] INTEGER PRIMARY KEY, [s] TEXT COLLATE NOCASE) ";
DbCommand cmd = conn.CreateCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();

// 添加参数
cmd.Parameters.Add(cmd.CreateParameter());

// 开始计时
Stopwatch watch = new Stopwatch();
watch.Start();

DbTransaction trans = conn.BeginTransaction(); // <-------------------
try
{
// 连续插入1000条记录
for (int i = 0; i < 1000; i++)
{
cmd.CommandText = "insert into [test1] ([s]) values (?) ";
cmd.Parameters[0].Value = i.ToString();

cmd.ExecuteNonQuery();
}

trans.Commit(); // <-------------------
}
catch
{
trans.Rollback(); // <-------------------
throw; // <-------------------
}

// 停止计时
watch.Stop();
Console.WriteLine(watch.Elapsed);
}