113 lines
4.0 KiB
C#
113 lines
4.0 KiB
C#
using NX_LogUtility;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.Data.SQLite;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace LeadDataBaseUtility
|
||
{
|
||
/// <summary>
|
||
/// Copyright20181102 (C) sunyalong
|
||
/// 数据访问抽象基础类(基于SQLite)
|
||
/// 允许修改、添加满足自己项目的需要。
|
||
/// 添加、修改后请详细注释。违者会强制删除不予采用。
|
||
/// </summary>
|
||
public abstract class LeadDbHelperSQLite
|
||
{
|
||
public static string connectionString = LeadConfigureConnectionString.getConnectionString("LeadSQLite");
|
||
|
||
/// <summary>
|
||
/// 对SQLite数据库执行增删改操作,返回受影响的行数。
|
||
/// </summary>
|
||
/// <param name="sql">要执行的增删改的SQL语句</param>
|
||
/// <returns></returns>
|
||
public static int ExecuteSql(string sql)
|
||
{
|
||
int affectedRows = 0;
|
||
|
||
try
|
||
{
|
||
using (SQLiteConnection conn = new SQLiteConnection(connectionString))
|
||
{
|
||
conn.Open();
|
||
using (SQLiteCommand cmd = new SQLiteCommand(conn))
|
||
{
|
||
cmd.CommandText = sql;
|
||
affectedRows = cmd.ExecuteNonQuery();
|
||
}
|
||
conn.Close();
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
LogHelper.WriteLog($"数据库运行异常:【{e.Message}】。SQL语句:【{sql}】。", LogTypeEum.DbErr);
|
||
}
|
||
|
||
return affectedRows;
|
||
}
|
||
/// <summary>
|
||
/// 执行查询语句,返回DataSet
|
||
/// </summary>
|
||
/// <param name="SQLString">查询语句</param>
|
||
/// <returns>DataSet</returns>
|
||
public static DataSet Query(string SQLString)
|
||
{
|
||
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
|
||
{
|
||
DataSet ds = new DataSet();
|
||
try
|
||
{
|
||
connection.Open();
|
||
SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
|
||
command.Fill(ds, "ds");
|
||
connection.Close();
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
LogHelper.WriteLog($"数据库运行异常:【{e.Message}】。SQL语句:【{SQLString}】。", LogTypeEum.DbErr);
|
||
}
|
||
return ds;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 执行一条计算查询结果语句,返回查询结果(object)。
|
||
/// </summary>
|
||
/// <param name="SQLString">计算查询结果语句</param>
|
||
/// <returns>查询结果(object)</returns>
|
||
public static object GetSingle(string SQLString)
|
||
{
|
||
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
|
||
{
|
||
using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
|
||
{
|
||
try
|
||
{
|
||
connection.Open();
|
||
object obj = cmd.ExecuteScalar();
|
||
if ((Equals(obj, null)) || (Equals(obj, System.DBNull.Value)))
|
||
{
|
||
connection.Close();
|
||
return null;
|
||
}
|
||
else
|
||
{
|
||
connection.Close();
|
||
return obj;
|
||
}
|
||
}
|
||
catch (System.Data.SQLite.SQLiteException e)
|
||
{
|
||
connection.Close();
|
||
LogHelper.WriteLog($"数据库运行异常:【{e.Message}】。SQL语句:【{SQLString}】。", LogTypeEum.DbErr);
|
||
return null;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|