You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
171 lines
3.3 KiB
171 lines
3.3 KiB
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 PSqLiteWrapper
|
|
{
|
|
public class PSqLite
|
|
{
|
|
public static string DBPath { get; set; }
|
|
|
|
private static string ConnString
|
|
{
|
|
get { return $@"Data Source = {DBPath};"; }
|
|
}
|
|
|
|
public static SQLiteConnection Connection
|
|
{
|
|
get
|
|
{
|
|
if (string.IsNullOrEmpty(DBPath))
|
|
throw new ArgumentException("Empty DBPath");
|
|
|
|
try
|
|
{
|
|
return new SQLiteConnection(ConnString);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void CreateDB()
|
|
{
|
|
if (string.IsNullOrEmpty(DBPath))
|
|
throw new ArgumentException("Empty DBPath");
|
|
|
|
try
|
|
{
|
|
if (!File.Exists(DBPath))
|
|
SQLiteConnection.CreateFile(DBPath);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
public static void ExecuteQuery(string query)
|
|
{
|
|
if (string.IsNullOrEmpty(DBPath))
|
|
throw new ArgumentException("Empty DBPath");
|
|
|
|
try
|
|
{
|
|
using (SQLiteConnection conn = Connection)
|
|
{
|
|
conn.Open();
|
|
SQLiteCommand command = new SQLiteCommand(query, conn);
|
|
command.ExecuteNonQuery();
|
|
Connection.Close();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
public static void ExecuteQuery(string[] queries)
|
|
{
|
|
if (string.IsNullOrEmpty(DBPath))
|
|
throw new ArgumentException("Empty DBPath");
|
|
|
|
try
|
|
{
|
|
using (SQLiteConnection conn = Connection)
|
|
{
|
|
conn.Open();
|
|
PSqLite.ExecuteQuery(conn, "BEGIN TRANSACTION;");
|
|
|
|
for (int i = 0; i < queries.Length; i++)
|
|
{
|
|
SQLiteCommand command = new SQLiteCommand(queries[i], conn);
|
|
command.ExecuteNonQuery();
|
|
}
|
|
|
|
PSqLite.ExecuteQuery(conn, "END TRANSACTION;");
|
|
Connection.Close();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
public static void ExecuteQueryAsync(string query)
|
|
{
|
|
if (string.IsNullOrEmpty(DBPath))
|
|
throw new ArgumentException("Empty DBPath");
|
|
|
|
try
|
|
{
|
|
using (SQLiteConnection conn = Connection)
|
|
{
|
|
conn.Open();
|
|
SQLiteCommand command = new SQLiteCommand(query, conn);
|
|
command.ExecuteNonQueryAsync();
|
|
Connection.Close();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
public static void ExecuteQuery(SQLiteConnection openedConn, string query)
|
|
{
|
|
SQLiteCommand command = new SQLiteCommand(query, openedConn);
|
|
command.ExecuteNonQuery();
|
|
}
|
|
|
|
public static void ExecuteQueryAsync(SQLiteConnection openedConn, string query)
|
|
{
|
|
SQLiteCommand command = new SQLiteCommand(query, openedConn);
|
|
command.ExecuteNonQueryAsync();
|
|
}
|
|
|
|
public static DataSet ExecuteSelectQuery(string query)
|
|
{
|
|
if (string.IsNullOrEmpty(DBPath))
|
|
throw new ArgumentException("Empty DBPath");
|
|
|
|
try
|
|
{
|
|
using (SQLiteConnection conn = Connection)
|
|
{
|
|
DataSet dataSet = new DataSet();
|
|
|
|
conn.Open();
|
|
SQLiteDataAdapter adapter = new SQLiteDataAdapter(query, conn);
|
|
adapter.Fill(dataSet);
|
|
Connection.Close();
|
|
|
|
return dataSet;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
public static DataSet ExecuteSelectQuery(SQLiteConnection openedConn, string query)
|
|
{
|
|
DataSet dataSet = new DataSet();
|
|
SQLiteDataAdapter adapter = new SQLiteDataAdapter(query, openedConn);
|
|
adapter.Fill(dataSet);
|
|
|
|
return dataSet;
|
|
}
|
|
}
|
|
}
|
|
|