using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
namespace Common.Data.SqlLite
{
///
/// 功能:SQLServer事务操作类
/// 作者:王昊昇
/// 时间:2012.2.8
///
public class SqlLiteRoutine : IDataRoutine
{
///
/// 使用一个已打开的数据源连接创建事务实例
///
///
public SqlLiteRoutine(IDbConnection conn)
{
this.conn = conn;
usePooling = true;
BeginTrans();
}
///
/// 使用一个数据源连接字符串创建数据连接并创建事务实例
///
///
public SqlLiteRoutine(string connStr)
{
conn = new SQLiteConnection(connStr);
conn.Open();
BeginTrans();
}
private IDbConnection conn = null;
private IDbCommand comm = null;
private SQLiteTransaction trans = null;
private bool usePooling = false;//是否是使用的连接池的资源
///
/// 开始事务
///
private void BeginTrans()
{
this.comm = new SQLiteCommand();
this.comm.Connection = this.conn;
this.trans = (SQLiteTransaction)conn.BeginTransaction();
this.comm.Transaction = trans;
}
///
/// 处理命令
///
/// 命令
/// 参数
private void PrepareCommand(string txt, IDataParameter[] param)
{
if (param != null)
{
foreach (IDataParameter parm in param)
{
comm.Parameters.Add(parm);
}
}
}
///
/// 执行更新语句,失败时回滚操作
///
/// 更新语句
/// 参数列表
///
public int ExecuteSql(string sql, params System.Data.IDataParameter[] param)
{
PrepareCommand(sql, param);
try
{
return comm.ExecuteNonQuery();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
finally
{
comm.Parameters.Clear();
}
}
///
/// 执行更新语句,失败时回滚操作
///
/// 更新语句
///
public int ExecuteSql(string sql)
{
comm.CommandText = sql;
try
{
return comm.ExecuteNonQuery();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
}
///
/// 执行查询语句,返回查询结果的第一行第一列的数据。失败时回滚操作
///
/// 查询语句
/// 结果对象
public object GetSingle(string sql)
{
try
{
object obj = comm.ExecuteScalar();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
}
///
/// 执行带参数的查询语句,返回查询结果的第一行第一列的数据。失败时回滚操作
///
/// 查询语句
/// 参数
/// 结果对象
public object GetSingle(string sql, params System.Data.IDataParameter[] param)
{
PrepareCommand(sql, (SQLiteParameter[])param);
try
{
object obj = comm.ExecuteScalar();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
finally
{
comm.Parameters.Clear();
}
}
///
/// 执行数据处理事务。失败时回滚操作
///
/// 执行语句列表
public void ExecuteSqlTran(List list)
{
try
{
foreach (var s in list)
{
if (s.Trim().Length > 0)
{
comm.CommandText = s;
comm.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
}
///
/// 执行带参数的数据查询。失败时回滚操作
///
/// 查询语句
/// 参数
/// 数据集
public System.Data.DataSet Query(string sql, params System.Data.IDataParameter[] param)
{
PrepareCommand(sql, (SQLiteParameter[])param);
using (SQLiteDataAdapter da = new SQLiteDataAdapter((SQLiteCommand)comm))
{
System.Data.DataSet ds = new System.Data.DataSet();
try
{
da.Fill(ds, "ds");
return ds;
}
catch (Exception ex)
{
trans.Rollback();
ds.Dispose();
ds = null;
throw ex;
}
finally
{
comm.Parameters.Clear();
}
}
}
///
/// 执行数据查询。失败时回滚操作
///
/// 查询语句
/// 数据集
public System.Data.DataSet Query(string sql)
{
using (SQLiteDataAdapter da = new SQLiteDataAdapter((SQLiteCommand)comm))
{
comm.CommandText = sql;
System.Data.DataSet ds = new System.Data.DataSet();
try
{
da.Fill(ds, "ds");
return ds;
}
catch (Exception ex)
{
trans.Rollback();
ds.Dispose();
ds = null;
throw ex;
}
}
}
///
/// 执行数据查询。失败时回滚操作
///
/// 查询语句
/// 数据表
public System.Data.DataTable QueryReturnDataTable(string sql)
{
using (SQLiteDataAdapter da = new SQLiteDataAdapter((SQLiteCommand)comm))
{
comm.CommandText = sql;
System.Data.DataTable table = new System.Data.DataTable();
try
{
da.Fill(table);
return table;
}
catch (Exception ex)
{
trans.Rollback();
table.Dispose();
table = null;
throw ex;
}
}
}
///
/// 执行数据查询。失败时回滚操作
///
/// 查询语句
/// 参数
/// 数据表
public System.Data.DataTable QueryReturnDataTable(string sql, params System.Data.IDataParameter[] param)
{
PrepareCommand(sql, (SQLiteParameter[])param);
using (SQLiteDataAdapter command = new SQLiteDataAdapter((SQLiteCommand)comm))
{
System.Data.DataTable dt = new System.Data.DataTable();
try
{
command.Fill(dt);
return dt;
}
catch (Exception ex)
{
trans.Rollback();
dt.Dispose();
dt = null;
throw ex;
}
finally
{
comm.Parameters.Clear();
}
}
}
///
/// 提交事务,关闭资源
///
public void Dispose()
{
trans.Commit();
trans.Dispose();
trans = null;
comm.Dispose();
comm = null;
if (!usePooling)
{
conn.Close();
conn.Dispose();
conn = null;
}
}
///
/// 回滚事务
///
public void RollBack()
{
trans.Rollback();
}
///
/// 提交事务
///
public void Commit()
{
trans.Commit();
}
}
}