前端之家收集整理的这篇文章主要介绍了
SQLite访问封装类,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
http://www.cnblogs.com/jackdong/archive/2010/10/08/1845787.html
sqlite作为windows mobile的数据库,是一种不错的选择。sqlite的下载地址为:SQLite,sqlite的ADO.net Provider下载地址为:System.Data.SQLite ,在发布程序时,请别忘记拷贝sqlite.Interop.06.DLL、system.data.sqlite.dll两个文件到安装目录下。
本文非原创,而是参考egmkang撰写的WM下访问SQLite(一种替代SQL CE的解决方案),并根据我自己的编程需要和习惯做了少许修改,错误在所难免。下面实现的是一个SQLiteHelper封装类:
|
using System.Data.sqlite; |
|
namespace MyHelper.DataAccess |
|
public class sqliteHelper |
|
private static string password = "***" ; |
|
private static string dbFilePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly() |
|
.GetName().CodeBase) + "//***.db" ; |
|
private static string connectString = string .Format( "Data Source =/"{}/"" ,dbFilePath,password); |
|
private static sqliteConnection myConnect = new sqliteConnection(connectString); |
|
/// <returns>当前sqlite连接</returns> |
|
public static sqliteConnection GetConnection() |
|
/// <param name="commandString">sql语句</param> |
|
/// <param name="parameters">sql语句参数</param> |
|
/// <returns>受影响的行数</returns> |
|
public static int ExecuteNonQuery( string commandString, params sqliteParameter[] parameters) |
|
using (sqliteCommand command = new sqliteCommand()) |
|
PrepareCommand(command, null ,commandString,parameters); |
|
result = command.ExecuteNonQuery(); |
|
command.Parameters.Clear(); |
|
/// <param name="transaction">sql事务</param> |
|
/// <param name="commandString">sql语句</param> |
|
/// <param name="parameters">sql语句参数</param> |
|
/// <returns>受影响的行数</returns> |
|
public static int ExecuteNonQuery(sqliteTransaction transaction, string commandString, |
|
params sqliteParameter[] parameters) |
|
using (sqliteCommand command = new sqliteCommand()) |
|
PrepareCommand(command,transaction,parameters); |
|
result = command.ExecuteNonQuery(); |
|
command.Parameters.Clear(); |
|
/// 执行查询,并返回结果集的第一行第一列的值,忽略其它所有的行和列 |
|
/// <param name="commandString">sql语句</param> |
|
/// <param name="parameters">sql语句参数</param> |
|
/// <returns>第一行第一列的值</returns> |
|
public static object ExecuteScalar( string commandString, params sqliteParameter[] parameters) |
|
using (sqliteCommand command = new sqliteCommand()) |
|
PrepareCommand(command,parameters); |
|
result = command.ExecuteScalar(); |
|
/// 执行sql语句,返回结果集的DataReader |
|
/// <param name="commandString">sql语句</param> |
|
/// <param name="parameters">sql语句参数</param> |
|
/// <returns>结果集的DataReader</returns> |
|
public static sqliteDataReader ExecuteReader( string commandString, params sqliteParameter[] parameters) |
|
sqliteCommand command = new sqliteCommand(); |
|
PrepareCommand(command,parameters); |
|
sqliteDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection); |
|
command.Parameters.Clear(); |
|
/// 预处理Command对象,数据库链接,事务,需要执行的对象,参数等的初始化 |
|
/// <param name="command">Command对象</param> |
|
/// <param name="transaction">transaction对象</param> |
|
/// <param name="commandString">sql语句</param> |
|
/// <param name="parameters">sql语句参数</param> |
|
private static void PrepareCommand(sqliteCommand command,sqliteTransaction transaction, |
|
string commandString, params sqliteParameter[] parameters) |
|
if (myConnect.State != ConnectionState.Open) |
|
command.Connection = myConnect; |
|
command.CommandText = commandString; |
|
command.Transaction = transaction; |
|
if (parameters != null && parameters.Length > ) |
|
command.Parameters.AddRange(parameters); |
为了提高效率,我将数据库连接修改为长连接。这样,只有第一次连接时会比较耗时,以后的操作就比较快了。如果本文有错误的地方,请大家参考egmkang原文。
原文链接:https://www.f2er.com/sqlite/202764.html