Files
CleanMachine/LeadDataBaseUtility/LeadDbHelperSQLite.cs

124 lines
4.0 KiB
C#
Raw Normal View History

2021-06-05 14:17:30 +08:00
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
{
2022-01-18 14:42:23 +08:00
public static string connectionString = LeadConfigureConnectionString.getConnectionString("LeadSQLite");
2021-06-05 14:17:30 +08:00
/// <summary>
/// 对SQLite数据库执行增删改操作,返回受影响的行数。
/// </summary>
/// <param name="sql">要执行的增删改的SQL语句</param>
/// <returns></returns>
public static int ExecuteSql(string sql)
{
int affectedRows = 0;
2022-05-05 13:14:07 +08:00
#if RELEASE
2021-06-05 14:17:30 +08:00
try
{
2022-01-18 20:20:10 +08:00
#endif
using (SQLiteConnection conn = new SQLiteConnection(connectionString))
{
conn.Open();
using (SQLiteCommand cmd = new SQLiteCommand(conn))
2021-06-05 14:17:30 +08:00
{
2022-01-18 20:20:10 +08:00
cmd.CommandText = sql;
affectedRows = cmd.ExecuteNonQuery();
2021-06-05 14:17:30 +08:00
}
2022-01-18 20:20:10 +08:00
conn.Close();
2021-06-05 14:17:30 +08:00
}
2022-05-05 13:14:07 +08:00
#if RELEASE
2022-01-18 20:20:10 +08:00
}
2021-06-05 14:17:30 +08:00
catch (Exception e)
{
LogHelper.WriteLog($"数据库运行异常:【{e.Message}】。SQL语句:【{sql}】。", LogTypeEum.DbErr);
}
2022-01-18 20:20:10 +08:00
#endif
2021-06-05 14:17:30 +08:00
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();
2022-05-05 13:14:07 +08:00
#if RELEASE
2021-06-05 14:17:30 +08:00
try
{
2022-01-18 20:20:10 +08:00
#endif
connection.Open();
SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
connection.Close();
2022-05-05 13:14:07 +08:00
#if RELEASE
2022-01-18 20:20:10 +08:00
}
2021-06-05 14:17:30 +08:00
catch (Exception e)
2022-01-18 20:20:10 +08:00
{
LogHelper.WriteLog($"数据库运行异常:【{e.Message}】。SQL语句:【{SQLString}】。", LogTypeEum.DbErr);
}
#endif
2021-06-05 14:17:30 +08:00
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))
{
2022-05-05 13:14:07 +08:00
#if RELEASE
2021-06-05 14:17:30 +08:00
try
{
2022-01-18 20:20:10 +08:00
#endif
connection.Open();
object obj = cmd.ExecuteScalar();
if ((Equals(obj, null)) || (Equals(obj, System.DBNull.Value)))
2021-06-05 14:17:30 +08:00
{
connection.Close();
return null;
}
2022-01-18 20:20:10 +08:00
else
{
connection.Close();
return obj;
}
2022-05-05 13:14:07 +08:00
#if RELEASE
2022-01-18 20:20:10 +08:00
}
catch (System.Data.SQLite.SQLiteException e)
{
connection.Close();
LogHelper.WriteLog($"数据库运行异常:【{e.Message}】。SQL语句:【{SQLString}】。", LogTypeEum.DbErr);
return null;
}
#endif
2021-06-05 14:17:30 +08:00
}
}
}
}
}