Sqlite 操作类代码
作者:
这两天用了用SQLite,感觉比SQL CE好用多了,最主要的是性能好多了.这一点对手持设备来讲很重要.闲话少说,下来将怎么在WM用.NET CF访问SQLite.
1. ADO.NET Provider For SQLite.
ADO.NET 提供程序是香港(貌似)一个公司提供的.项目地址见:http://sourceforge.net/projects/sqlite-dotnet2
2. 对SQLite.NET的封装,提供一个简单的操作帮助类.
SQLiteHelper
/**//**
* SQLite操作的帮助类.
*
* Author: egmkang.wang
* Date: 2009-06-21
*/
namespace System.Data.SQLite
{
using System.Data;
using System.Data.SQLite;
using System.IO;
public class SqliteHelper
{
private static string pwd = "PWD";
private static string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\sqliteTest.db";
private static string connString = string.Format("Data Source =\"{0}\"", path, pwd);
/**//// <summary>
/// 返回数据库链接字符串
/// </summary>
public static string ConnString
{
get { return connString; }
}
/**//// <summary>
/// 执行SQL语句,返回受影响的行数
/// </summary>
/// <param name="cmdText">需要被执行的SQL语句</param>
/// <returns>受影响的行数</returns>
public static int ExecuteNonQuery(string cmdText)
{
return ExecuteNonQuery(ConnString, cmdText);
}
/**//// <summary>
/// 执行带有事务的SQL语句
/// </summary>
/// <param name="trans">事务</param>
/// <param name="cmdText">SQL语句</param>
/// <returns>受影响的行数</returns>
public static int ExecuteNonQuery(SQLiteTransaction trans, string cmdText, params SQLiteParameter[] parameters)
{
int val = 0;
using (SQLiteCommand cmd = new SQLiteCommand())
{
PrepareCommand(cmd, (SQLiteConnection)trans.Connection, trans, cmdText, parameters);
val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
return val;
}
/**//// <summary>
/// 执行SQL语句,返回受影响的行数
/// </summary>
/// <param name="connString">连接字符串</param>
/// <param name="cmdText">SQL语句</param>
/// <param name="parameters">SQL的参数</param>
/// <returns>受影响的行数</returns>
public static int ExecuteNonQuery(string connString, string cmdText, params SQLiteParameter[] parameters)
{
using (SQLiteConnection conn = new SQLiteConnection(connString))
{
return ExecuteNonQuery(conn, cmdText, parameters);
}
}
/**//// <summary>
/// 执行SQL语句,返回受影响的行数
/// </summary>
/// <param name="connection">数据库链接</param>
/// <param name="cmdText">SQL语句</param>
/// <param name="parameters">参数</param>
/// <returns>受影响的行数</returns>
public static int ExecuteNonQuery(SQLiteConnection connection, string cmdText, params SQLiteParameter[] parameters)
{
int val = 0;
using (SQLiteCommand cmd = new SQLiteCommand())
{
PrepareCommand(cmd, connection, null, cmdText, parameters);
val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
return val;
}
/**//// <summary>
/// 执行查询,并返回结果集的第一行的第一列.其他所有的行和列被忽略.
/// </summary>
/// <param name="cmdText">SQL 语句</param>
/// <returns>第一行的第一列的值</returns>
public static object ExecuteScalar(string cmdText)
{
return ExecuteScalar(ConnString, cmdText);
}
/**//// <summary>
/// 执行查询,并返回结果集的第一行的第一列.其他所有的行和列被忽略.
/// </summary>
/// <param name="connString">连接字符串</param>
/// <param name="cmdText">SQL 语句</param>
/// <returns>第一行的第一列的值</returns>
public static object ExecuteScalar(string connString, string cmdText)
{
using (SQLiteConnection conn = new SQLiteConnection(connString))
{
return ExecuteScalar(conn, cmdText);
}
}
/**//// <summary>
/// 执行查询,并返回结果集的第一行的第一列.其他所有的行和列被忽略.
/// </summary>
/// <param name="connection">数据库链接</param>
/// <param name="cmdText">SQL 语句</param>
/// <returns>第一行的第一列的值</returns>
public static object ExecuteScalar(SQLiteConnection connection, string cmdText)
{
object val;
using (SQLiteCommand cmd = new SQLiteCommand())
{
PrepareCommand(cmd, connection, null, cmdText);
val = cmd.ExecuteScalar();
}
return val;
}
/**//// <summary>
/// 执行SQL语句,返回结果集的DataReader
/// </summary>
/// <param name="cmdText">SQL语句</param>
/// <param name="parameters">参数</param>
/// <returns>结果集的DataReader</returns>
public static SQLiteDataReader ExecuteReader(string cmdText, params SQLiteParameter[] parameters)
{
return ExecuteReader(ConnString, cmdText, parameters);
}
/**//// <summary>
/// 执行SQL语句,返回结果集的DataReader
/// </summary>
/// <param name="connString">连接字符串</param>
/// <param name="cmdText">SQL语句</param>
/// <param name="parameters">参数</param>
/// <returns>结果集的DataReader</returns>
public static SQLiteDataReader ExecuteReader(string connString, string cmdText, params SQLiteParameter[] parameters)
{
SQLiteConnection conn = new SQLiteConnection(connString);
SQLiteCommand cmd = new SQLiteCommand();
try
{
PrepareCommand(cmd, conn, null, cmdText, parameters);
SQLiteDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch
{
conn.Close();
throw;
}
}
/**//// <summary>
/// 预处理Command对象,数据库链接,事务,需要执行的对象,参数等的初始化
/// </summary>
/// <param name="cmd">Command对象</param>
/// <param name="conn">Connection对象</param>
/// <param name="trans">Transcation对象</param>
/// <param name="cmdText">SQL Text</param>
/// <param name="parameters">参数实例</param>
private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, SQLiteTransaction trans, string cmdText, params SQLiteParameter[] parameters)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
if (null != parameters && parameters.Length > 0)
{
cmd.Parameters.AddRange(parameters);
}
}
}
}
3. 增删改查:
Insert,Delete,Update
const string s_AddressTreeIntoSQLite = "Insert into [AddressTree] ([Id],[ItemType],[ParentId],[Name]) values (@Id,@ItemType,@ParentId,@Name);";
SqliteHelper.ExecuteNonQuery(tran, s_AddressTreeIntoSQLite,
//new SQLiteParameter[] here
);
Select
const string s_AddresTreeFromSqlCE = "Select [Id],[ItemType],[ParentId],[Name] From [AddressTree];";
using (SqlCeDataReader rdr = SqlCeHelper.ExecuteReader(s_AddresTreeFromSqlCE ))
{
while (rdr.Read())
{
//Read Data Here
}
}
4. 其他
SQLite性能绝对强悍.四表连接查询,查询200次,SQL CE需要44秒(with index),SQLite只需要3-6秒(with index).
插入,删除更新性能参见http://www.cnblogs.com/egmkang/archive/2009/06/06/1497678.html
PS:最近发现执行sql的时候,最好使用单一的长连接,而不是ConnectionString.原因很简单,嵌入式数据库没有连接池技术,
在进行数据库查询中的链接的打开关闭费用相对来说比较高昂.这一点在写程序的时候注意以下.
还有,有关二进制资源,需要及时释放,例如SqlCommand,这些在写Web 程序的时候体验不是很大,毕竟那种环境拥有大量
的内存,GC的效率又是比较高.
ADO.NET 提供程序是香港(貌似)一个公司提供的.项目地址见:http://sourceforge.net/projects/sqlite-dotnet2
2. 对SQLite.NET的封装,提供一个简单的操作帮助类.
SQLiteHelper
复制代码 代码如下:
/**//**
* SQLite操作的帮助类.
*
* Author: egmkang.wang
* Date: 2009-06-21
*/
namespace System.Data.SQLite
{
using System.Data;
using System.Data.SQLite;
using System.IO;
public class SqliteHelper
{
private static string pwd = "PWD";
private static string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\sqliteTest.db";
private static string connString = string.Format("Data Source =\"{0}\"", path, pwd);
/**//// <summary>
/// 返回数据库链接字符串
/// </summary>
public static string ConnString
{
get { return connString; }
}
/**//// <summary>
/// 执行SQL语句,返回受影响的行数
/// </summary>
/// <param name="cmdText">需要被执行的SQL语句</param>
/// <returns>受影响的行数</returns>
public static int ExecuteNonQuery(string cmdText)
{
return ExecuteNonQuery(ConnString, cmdText);
}
/**//// <summary>
/// 执行带有事务的SQL语句
/// </summary>
/// <param name="trans">事务</param>
/// <param name="cmdText">SQL语句</param>
/// <returns>受影响的行数</returns>
public static int ExecuteNonQuery(SQLiteTransaction trans, string cmdText, params SQLiteParameter[] parameters)
{
int val = 0;
using (SQLiteCommand cmd = new SQLiteCommand())
{
PrepareCommand(cmd, (SQLiteConnection)trans.Connection, trans, cmdText, parameters);
val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
return val;
}
/**//// <summary>
/// 执行SQL语句,返回受影响的行数
/// </summary>
/// <param name="connString">连接字符串</param>
/// <param name="cmdText">SQL语句</param>
/// <param name="parameters">SQL的参数</param>
/// <returns>受影响的行数</returns>
public static int ExecuteNonQuery(string connString, string cmdText, params SQLiteParameter[] parameters)
{
using (SQLiteConnection conn = new SQLiteConnection(connString))
{
return ExecuteNonQuery(conn, cmdText, parameters);
}
}
/**//// <summary>
/// 执行SQL语句,返回受影响的行数
/// </summary>
/// <param name="connection">数据库链接</param>
/// <param name="cmdText">SQL语句</param>
/// <param name="parameters">参数</param>
/// <returns>受影响的行数</returns>
public static int ExecuteNonQuery(SQLiteConnection connection, string cmdText, params SQLiteParameter[] parameters)
{
int val = 0;
using (SQLiteCommand cmd = new SQLiteCommand())
{
PrepareCommand(cmd, connection, null, cmdText, parameters);
val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
return val;
}
/**//// <summary>
/// 执行查询,并返回结果集的第一行的第一列.其他所有的行和列被忽略.
/// </summary>
/// <param name="cmdText">SQL 语句</param>
/// <returns>第一行的第一列的值</returns>
public static object ExecuteScalar(string cmdText)
{
return ExecuteScalar(ConnString, cmdText);
}
/**//// <summary>
/// 执行查询,并返回结果集的第一行的第一列.其他所有的行和列被忽略.
/// </summary>
/// <param name="connString">连接字符串</param>
/// <param name="cmdText">SQL 语句</param>
/// <returns>第一行的第一列的值</returns>
public static object ExecuteScalar(string connString, string cmdText)
{
using (SQLiteConnection conn = new SQLiteConnection(connString))
{
return ExecuteScalar(conn, cmdText);
}
}
/**//// <summary>
/// 执行查询,并返回结果集的第一行的第一列.其他所有的行和列被忽略.
/// </summary>
/// <param name="connection">数据库链接</param>
/// <param name="cmdText">SQL 语句</param>
/// <returns>第一行的第一列的值</returns>
public static object ExecuteScalar(SQLiteConnection connection, string cmdText)
{
object val;
using (SQLiteCommand cmd = new SQLiteCommand())
{
PrepareCommand(cmd, connection, null, cmdText);
val = cmd.ExecuteScalar();
}
return val;
}
/**//// <summary>
/// 执行SQL语句,返回结果集的DataReader
/// </summary>
/// <param name="cmdText">SQL语句</param>
/// <param name="parameters">参数</param>
/// <returns>结果集的DataReader</returns>
public static SQLiteDataReader ExecuteReader(string cmdText, params SQLiteParameter[] parameters)
{
return ExecuteReader(ConnString, cmdText, parameters);
}
/**//// <summary>
/// 执行SQL语句,返回结果集的DataReader
/// </summary>
/// <param name="connString">连接字符串</param>
/// <param name="cmdText">SQL语句</param>
/// <param name="parameters">参数</param>
/// <returns>结果集的DataReader</returns>
public static SQLiteDataReader ExecuteReader(string connString, string cmdText, params SQLiteParameter[] parameters)
{
SQLiteConnection conn = new SQLiteConnection(connString);
SQLiteCommand cmd = new SQLiteCommand();
try
{
PrepareCommand(cmd, conn, null, cmdText, parameters);
SQLiteDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch
{
conn.Close();
throw;
}
}
/**//// <summary>
/// 预处理Command对象,数据库链接,事务,需要执行的对象,参数等的初始化
/// </summary>
/// <param name="cmd">Command对象</param>
/// <param name="conn">Connection对象</param>
/// <param name="trans">Transcation对象</param>
/// <param name="cmdText">SQL Text</param>
/// <param name="parameters">参数实例</param>
private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, SQLiteTransaction trans, string cmdText, params SQLiteParameter[] parameters)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
if (null != parameters && parameters.Length > 0)
{
cmd.Parameters.AddRange(parameters);
}
}
}
}
3. 增删改查:
复制代码 代码如下:
Insert,Delete,Update
const string s_AddressTreeIntoSQLite = "Insert into [AddressTree] ([Id],[ItemType],[ParentId],[Name]) values (@Id,@ItemType,@ParentId,@Name);";
SqliteHelper.ExecuteNonQuery(tran, s_AddressTreeIntoSQLite,
//new SQLiteParameter[] here
);
Select
const string s_AddresTreeFromSqlCE = "Select [Id],[ItemType],[ParentId],[Name] From [AddressTree];";
using (SqlCeDataReader rdr = SqlCeHelper.ExecuteReader(s_AddresTreeFromSqlCE ))
{
while (rdr.Read())
{
//Read Data Here
}
}
4. 其他
SQLite性能绝对强悍.四表连接查询,查询200次,SQL CE需要44秒(with index),SQLite只需要3-6秒(with index).
插入,删除更新性能参见http://www.cnblogs.com/egmkang/archive/2009/06/06/1497678.html
PS:最近发现执行sql的时候,最好使用单一的长连接,而不是ConnectionString.原因很简单,嵌入式数据库没有连接池技术,
在进行数据库查询中的链接的打开关闭费用相对来说比较高昂.这一点在写程序的时候注意以下.
还有,有关二进制资源,需要及时释放,例如SqlCommand,这些在写Web 程序的时候体验不是很大,毕竟那种环境拥有大量
的内存,GC的效率又是比较高.