|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Data.SQLite;
|
|
|
|
|
|
|
|
|
|
namespace JwKdsV.Core.Utils
|
|
|
|
|
{
|
|
|
|
|
public class SQLiteUtils
|
|
|
|
|
{
|
|
|
|
|
private static readonly string connectionString;
|
|
|
|
|
|
|
|
|
|
static SQLiteUtils()
|
|
|
|
|
{
|
|
|
|
|
SQLiteConnectionStringBuilder builder = new SQLiteConnectionStringBuilder();
|
|
|
|
|
builder.DataSource = Global.Instance.DataBaseFullPath;
|
|
|
|
|
//builder.Password = "passwd";
|
|
|
|
|
builder.Version = 3;
|
|
|
|
|
builder.CacheSize = 4000;
|
|
|
|
|
builder.DefaultTimeout = 5000;
|
|
|
|
|
builder.FailIfMissing = false;
|
|
|
|
|
builder.Pooling = true;
|
|
|
|
|
builder.SyncMode = SynchronizationModes.Normal;
|
|
|
|
|
builder.JournalMode = SQLiteJournalModeEnum.Wal;
|
|
|
|
|
|
|
|
|
|
connectionString = builder.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string sql;
|
|
|
|
|
while (lists.TryDequeue(out sql))
|
|
|
|
|
{
|
|
|
|
|
cmd.CommandText = sql;
|
|
|
|
|
cmd.ExecuteNonQuery();
|
|
|
|
|
}
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (SQLiteException E)
|
|
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
SQLiteDataAdapter command = new SQLiteDataAdapter(sql, connection);
|
|
|
|
|
command.Fill(data, tableName);
|
|
|
|
|
}
|
|
|
|
|
catch (SQLiteException ex)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
connection.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|