-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Text;
-
using System.Threading.Tasks;
-
using System.Configuration;
-
using System.Data.SQLite;
-
using System.Data;
-
namespace factory.DAL
-
{
-
public class SqlliteHelper
-
{
-
//连接字符串
-
private static readonly string str = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
-
-
//方法
-
/// <summary>
-
/// 增删改 都可以
-
/// </summary>
-
/// <param name="sql">sql语句</param>
-
/// <param name="ps">sql语句中的参数</param>
-
/// <returns>返回受影响的行数</returns>
-
public static int ExecuteNonQuery(string sql, params SQLiteParameter[] ps)
-
{
-
try
-
{
-
using (SQLiteConnection con = new SQLiteConnection(str))
-
{
-
using (SQLiteCommand cmd = new SQLiteCommand(sql, con))
-
{
-
if (ps != null)
-
{
-
cmd.Parameters.AddRange(ps);
-
}
-
con.Open();
-
return cmd.ExecuteNonQuery();
-
}
-
}
-
}
-
catch (Exception ex)
-
{
-
throw ex;
-
}
-
-
}
-
/// <summary>
-
/// 查询首行首列
-
/// </summary>
-
/// <param name="sql">sql语句</param>
-
/// <param name="ps">参数</param>
-
/// <returns>首行首列object</returns>
-
public static object ExecuteScalar(string sql, params SQLiteParameter[] ps)
-
{
-
try
-
{
-
using (SQLiteConnection con = new SQLiteConnection(str))
-
{
-
using (SQLiteCommand cmd = new SQLiteCommand(sql, con))
-
{
-
con.Open();
-
if (ps != null)
-
{
-
cmd.Parameters.AddRange(ps);
-
}
-
return cmd.ExecuteScalar();
-
}
-
}
-
}
-
catch (Exception ex)
-
{
-
throw ex;
-
}
-
-
}
-
-
/// <summary>
-
/// 查询的
-
/// </summary>
-
/// <param name="sql"></param>
-
/// <param name="ps"></param>
-
/// <returns></returns>
-
public static SQLiteDataReader ExecuteReader(string sql, params SQLiteParameter[] ps)
-
{
-
SQLiteConnection con = new SQLiteConnection(str);
-
try
-
{
-
using (SQLiteCommand cmd = new SQLiteCommand(sql, con))
-
{
-
if (ps != null)
-
{
-
cmd.Parameters.AddRange(ps);
-
}
-
try
-
{
-
con.Open();
-
return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
-
}
-
catch (Exception ex)
-
{
-
con.Close();
-
con.Dispose();
-
throw ex;
-
}
-
}
-
}
-
catch (Exception ex)
-
{
-
-
throw ex;
-
}
-
-
}
-
-
/// <summary>
-
/// 查询的是一个表
-
/// </summary>
-
/// <param name="sql">sql语句</param>
-
/// <param name="ps">sql语句中的参数</param>
-
/// <returns>一个表</returns>
-
public static DataTable ExecuteTable(string sql, params SQLiteParameter[] ps)
-
{
-
try
-
{
-
DataTable dt = new DataTable();
-
using (SQLiteDataAdapter sda = new SQLiteDataAdapter(sql, str))
-
{
-
if (ps != null)
-
{
-
sda.SelectCommand.Parameters.AddRange(ps);
-
-
}
-
sda.Fill(dt);
-
-
}
-
return dt;
-
}
-
catch (Exception ex)
-
{
-
-
throw ex;
-
}
-
-
}
-
-
}
-
}