时间:2021-07-01 10:21:17 帮助过:12人阅读
SQLiteConnection cn = new SQLiteConnection("Data Source=|DataDirectory|省市.db;Version=3");
SQLiteCommand cmd = new SQLiteCommand("select count(*) from city", cn);
cn.Open();
int recordCount = (int)(Int64)cmd.ExecuteScalar();
cn.Close();
Response.Write(recordCount);
SQLite中count函数返回的是一个Int64的整数,这一点同MSSQL、Access等不同。实际上,经过有限的使用发现,似乎所有INTEGER字段的返回值都是Int64,这一点未经过有效证实。ExecuteScalar方法返回一个object实例,按照C#规定,拆箱时进行标准转换,必须转换成该object实例实际存储的格式,因此分两步,先转换成Int64,再转换成int。当然用.net中某些高级转换器如Convert.ToInt32方法只要一步就可以了。
『例2』批量增删改时需要用事务,否则效率很低。
批量插入1000条记录,每条记录只有简单的id、name、password三个字段:
代码如下:
SQLiteConnection cn = new SQLiteConnection("Data Source=c:\\测试.db3;Version=3;password=12345");
SQLiteCommand cmd = new SQLiteCommand("select count(*) from test", cn);
cn.Open();
int recordCount = (int)(Int64)cmd.ExecuteScalar();
Response.Write("当前的总记录数:" + recordCount + "<br/>");
for (int i = 0; i < 1000; i++)
{
cmd.CommandText = "insert into test values(@id,@name,@password)";
cmd.Parameters.AddWithValue("@id", i);
cmd.Parameters.AddWithValue("@name", "姓名" + i);
cmd.Parameters.AddWithValue("@password", (i * 2).ToString());
cmd.ExecuteNonQuery();
}
cmd.CommandText = "select count(*) from test";
recordCount = (int)(Int64)cmd.ExecuteScalar();
cn.Close();
Response.Write("当前的总记录数:" + recordCount + "<br/>");
经过测试,这段代码中的for循环花费了70000~90000毫秒,一分钟多!
改用事务执行:
代码如下:
SQLiteConnection cn = new SQLiteConnection("Data Source=c:\\测试.db3;Version=3;password=12345");
SQLiteCommand cmd = new SQLiteCommand("select count(*) from test", cn);
cn.Open();
int recordCount = (int)(Int64)cmd.ExecuteScalar();
Response.Write("当前的总记录数:" + recordCount + "<br/>");
SQLiteTransaction tran = cn.BeginTransaction();
cmd.Transaction = tran;
try
{
for (int i = 0; i < 1000; i++)
{
cmd.CommandText = "insert into test values(@id,@name,@password)";
cmd.Parameters.AddWithValue("@id", i);
cmd.Parameters.AddWithValue("@name", "姓名" + i);
cmd.Parameters.AddWithValue("@password", (i * 2).ToString());
cmd.ExecuteNonQuery();
}
tran.Commit();
}
catch
{
tran.Rollback();
Response.Write("执行出错!");
}
finally
{
cmd.CommandText = "select count(*) from test";
recordCount = (int)(Int64)cmd.ExecuteScalar();
cn.Close();
Response.Write("当前的总记录数:" + recordCount + "<br/>");
}
经过测试,这段代码中的try部分只用了100~150毫秒!开启事务后,效率非常高!
『例3』一般开发中可以编写自己的数据库通用操作类,进一步封装ADO.NET。
如上面用事务操作的代码,改用数据库通用操作类后:
代码如下:
SQLiteData md = new SQLiteData("Data Source=c:\\测试.db3;Version=3;password=12345");
int recordCount = (int)(Int64)md.ExecuteScalar("select count(*) from test");
Response.Write("当前的总记录数:" + recordCount + "<br/>");
md.CreateTransaction();
try
{
for (int i = 0; i < 1000; i++)
md.ExecuteNonQuery("insert into test values(@id,@name,@password)", "@id", i, "@name", "姓名" + i, "@password", (i * 2).ToString());
md.CommitTransaction();
}
catch
{
md.RollBack();
Response.Write("执行出错!");
}
finally
{
recordCount = (int)(Int64)md.ExecuteScalar("select count(*) from test");
md.Close();
Response.Write("当前的总记录数:" + recordCount + "<br/>");
}
可以看到代码精简了很多。
【SQLite相关有用的链接地址】
SQLite官方网站:http://www.sqlite.org/
SQLite内置核心函数参考文档:http://www.sqlite.org/lang_corefunc.html
SQLite日期时间函数参考文档:http://www.sqlite.org/lang_datefunc.html
SQLite数学函数参考文档:http://www.sqlite.org/lang_aggfunc.html
SQLite相关SQL语法参考文档:http://www.sqlite.org/lang.html
System.Data.SQLite.dll数据访问驱动下载地址:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki