Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2023
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.83 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Data.SQLite;
  3. using System.IO;
  4. using System.Text;
  5.  
  6. namespace EsotericSQL
  7. {
  8.     public static class SQLTools
  9.     {
  10.         public static Dictionary<string, SQLiteConnection> Connections = new Dictionary<string, SQLiteConnection>();
  11.         public static void OpenFile(string FileName, string Key)
  12.         {
  13.             FileName = Path.GetFullPath(FileName);
  14.             if (!File.Exists(FileName))
  15.                 SQLiteConnection.CreateFile(FileName);
  16.             string ConnectionString = string.Format(@"
  17.                Data Source={0};
  18.                Version=3;",
  19.                 FileName
  20.             );
  21.             SQLiteConnection Connection = new SQLiteConnection(ConnectionString);
  22.             Connection.Open();
  23.             if (Connections.ContainsKey(Key))
  24.                 CloseFile(Key);
  25.             Connections.Add(Key, Connection);
  26.         }
  27.         public static void CloseFile(string Key)
  28.         {
  29.             Connections[Key].Close();
  30.             Connections[Key].Dispose();
  31.             Connections.Remove(Key);
  32.         }
  33.         public static void DeleteFile(string FileName)
  34.         {
  35.             SortedSet<string> KeysToClose = new SortedSet<string>();
  36.             foreach (KeyValuePair<string, SQLiteConnection> C in Connections)
  37.                 if (Path.GetFullPath(C.Value.FileName) == Path.GetFullPath(FileName))
  38.                     KeysToClose.Add(C.Key);
  39.             foreach (string Key in KeysToClose)
  40.                 CloseFile(Key);
  41.             File.Delete(FileName);
  42.         }
  43.  
  44.         public static void RunNonQuery(string Key, string Code)
  45.         {
  46.             using (SQLiteCommand CMD = new SQLiteCommand(Code, Connections[Key]))
  47.                 CMD.ExecuteNonQuery();
  48.         }
  49.         public static List<string> RunFullQuery(string Key, string Code)
  50.         {
  51.             List<string> Return = new List<string>();
  52.             using (SQLiteCommand CMD = new SQLiteCommand(Code, Connections[Key]))
  53.             using (SQLiteDataReader Reader = CMD.ExecuteReader())
  54.             {
  55.                 int N = Reader.FieldCount;
  56.                 StringBuilder Row = new StringBuilder(256);
  57.                 while (Reader.Read())
  58.                 {
  59.                     Row.Clear();
  60.                     for (int i = 0; i < N; i++)
  61.                     {
  62.                         if (i != 0) Row.Append(";");
  63.                         Row.Append(Reader.GetValue(i).ToString());
  64.                     }
  65.                     Return.Add(Row.ToString());
  66.                 }
  67.             }
  68.             return Return;
  69.         }
  70.  
  71.         public static List<bool> RunLogicQuery(string Key, string Code)
  72.         {
  73.             List<bool> Return = new List<bool>();
  74.             using (SQLiteCommand CMD = new SQLiteCommand(Code, Connections[Key]))
  75.             using (SQLiteDataReader Reader = CMD.ExecuteReader())
  76.                 while (Reader.Read())
  77.                     Return.Add(Reader.GetBoolean(0));
  78.             return Return;
  79.         }
  80.         public static List<long> RunIntegerQuery(string Key, string Code)
  81.         {
  82.             List<long> Return = new List<long>();
  83.             using (SQLiteCommand CMD = new SQLiteCommand(Code, Connections[Key]))
  84.             using (SQLiteDataReader Reader = CMD.ExecuteReader())
  85.                 while (Reader.Read())
  86.                     Return.Add(Reader.GetInt64(0));
  87.             return Return;
  88.         }
  89.         public static List<double> RunRealQuery(string Key, string Code)
  90.         {
  91.             List<double> Return = new List<double>();
  92.             using (SQLiteCommand CMD = new SQLiteCommand(Code, Connections[Key]))
  93.             using (SQLiteDataReader Reader = CMD.ExecuteReader())
  94.                 while (Reader.Read())
  95.                     Return.Add(Reader.GetDouble(0));
  96.             return Return;
  97.         }
  98.     }
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement