基本信息
源码名称:C# sqlLite连接数据库
源码大小:45.56M
文件格式:.rar
开发语言:C#
更新时间:2019-05-23
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
public class HelpSQLlite
{
private SQLiteConnection conn = null;
private SQLiteCommand cmd = null;
private SQLiteDataReader sdr = null;
/// <summary>
/// 构造函数
/// </summary>
public HelpSQLlite()
{
conn = new SQLiteConnection(@"Data Source =G:\LeiShenDB.db; Pooling = true");
}
/// <summary>
/// 获取连接结果,未连接打开连接
/// 连接结果
/// </summary>
/// <returns></returns>
private SQLiteConnection GetConn()
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
return conn;
}
/// 该方法执行传入的增删改SQL语句
/// 要执行的增删改SQL语句
/// 返回更新的记录数
public int ExecuteNonQuery(string sql)
{
int res;
try
{
cmd = new SQLiteCommand(sql, GetConn());
res = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
res = -1;
throw ex;
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
return res;
}
/// 执行带参数的SQL增删改语句
/// SQL增删改语句
/// 参数集合
/// 返回更新的记录数
public int ExecuteNonQuery(string sql, SQLiteParameter[] paras)
{
int res;
using (cmd = new SQLiteCommand(sql, GetConn()))
{
cmd.Parameters.AddRange(paras);
res = cmd.ExecuteNonQuery();
}
return res;
}
/// 该方法执行传入的SQL查询语句
/// SQL查询语句
/// 返回数据集合
public DataTable ExecuteQuery(string sql)
{
DataTable dt = new DataTable();
cmd = new SQLiteCommand(sql, GetConn());
using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(sdr);
}
return dt;
}
/// 执行带参数的SQL查询语句
/// SQL查询语句
/// 参数集合
/// 返回数据集合
public DataTable ExecuteQuery(string sql, SQLiteParameter[] paras)
{
DataTable dt = new DataTable();
cmd = new SQLiteCommand(sql, GetConn());
cmd.Parameters.AddRange(paras);
using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(sdr);
}
return dt;
}
/// 执行带参数的SQL查询判断语句
/// SQL查询语句
/// 参数集合
/// 返回是否为空
public bool BoolExecuteQuery(string sql, SQLiteParameter[] paras)
{
DataTable dt = new DataTable();
cmd = new SQLiteCommand(sql, GetConn());
cmd.Parameters.AddRange(paras);
try
{
using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(sdr);
}
}
catch (Exception e)
{
throw e;
}
DataRow[] rows = dt.Select();
bool temp = false;
if (rows.Length > 0)
{
temp = true;
}
return temp;
}
/// 该方法执行传入的SQL查询判断语句
/// SQL查询语句
/// 返回是否为空
public bool BoolExecuteQuery(string sql)
{
DataTable dt = new DataTable();
cmd = new SQLiteCommand(sql, GetConn());
using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(sdr);
}
DataRow[] rows = dt.Select();
bool temp = false;
if (rows.Length > 0)
{
temp = true;
}
return temp;
}
}