You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
186 lines
5.7 KiB
C#
186 lines
5.7 KiB
C#
using NLog;
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.SQLite;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace POSV.Utils
|
|
{
|
|
public class SqliteUtils
|
|
{
|
|
private static readonly string connectionString;
|
|
|
|
private static Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
|
|
|
static SqliteUtils()
|
|
{
|
|
connectionString = Global.Instance.SQLiteConnectionString;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 执行简单SQL语句
|
|
/// </summary>
|
|
/// <param name="sql">SQL语句</param>
|
|
/// <returns>影响的记录数</returns>
|
|
public static bool Execute(string sql)
|
|
{
|
|
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
|
|
{
|
|
connection.Open();
|
|
using (SQLiteCommand cmd = new SQLiteCommand(sql , connection))
|
|
{
|
|
bool result = true;
|
|
try
|
|
{
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
catch (SQLiteException sqlex)
|
|
{
|
|
result = false;
|
|
|
|
throw new Exception(sqlex.Message);
|
|
}
|
|
finally
|
|
{
|
|
connection.Close();
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void ExecuteTransaction(ConcurrentQueue<string> lists)
|
|
{
|
|
|
|
using (var connection = new SQLiteConnection(connectionString))
|
|
{
|
|
connection.Open();
|
|
|
|
using (var cmd = new SQLiteCommand(connection))
|
|
{
|
|
using (var transaction = connection.BeginTransaction())
|
|
{
|
|
string sql1 = "";
|
|
try
|
|
{
|
|
string sql;
|
|
|
|
while (lists.TryDequeue(out sql))
|
|
{
|
|
try
|
|
{
|
|
sql1 = sql;
|
|
cmd.CommandText = sql;
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.Info(ex.Message);
|
|
logger.Info("插入失败:"+ sql);
|
|
}
|
|
|
|
}
|
|
transaction.Commit();
|
|
|
|
}
|
|
catch (SQLiteException E)
|
|
{
|
|
logger.Error(sql1);
|
|
transaction.Rollback();
|
|
throw new Exception(E.Message);
|
|
}
|
|
finally
|
|
{
|
|
connection.Close();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行过程抛出异常
|
|
/// </summary>
|
|
/// <param name="lists"></param>
|
|
public static void ExecuteTransaction2(ConcurrentQueue<string> lists)
|
|
{
|
|
|
|
using (var connection = new SQLiteConnection(connectionString))
|
|
{
|
|
connection.Open();
|
|
|
|
using (var cmd = new SQLiteCommand(connection))
|
|
{
|
|
using (var transaction = connection.BeginTransaction())
|
|
{
|
|
string sql1 = "";
|
|
try
|
|
{
|
|
string sql;
|
|
|
|
while (lists.TryDequeue(out sql))
|
|
{
|
|
try
|
|
{
|
|
sql1 = sql;
|
|
cmd.CommandText = sql;
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
catch
|
|
{
|
|
throw new Exception("SQL执行异常:"+ sql);
|
|
}
|
|
|
|
}
|
|
transaction.Commit();
|
|
|
|
}
|
|
catch (SQLiteException E)
|
|
{
|
|
logger.Error(sql1);
|
|
transaction.Rollback();
|
|
throw new Exception(E.Message);
|
|
}
|
|
finally
|
|
{
|
|
connection.Close();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static DataSet Query(string sql , string tableName)
|
|
{
|
|
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
|
|
{
|
|
DataSet data = new DataSet();
|
|
try
|
|
{
|
|
connection.Open();
|
|
using(SQLiteDataAdapter command = new SQLiteDataAdapter(sql, connection))
|
|
{
|
|
command.Fill(data , tableName);
|
|
}
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
throw new Exception(ex.Message);
|
|
}
|
|
finally
|
|
{
|
|
connection.Close();
|
|
}
|
|
|
|
return data;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|