Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Data;
- using System.Data.SQLite;
- using System.Windows;
- namespace SQLiteINI
- {
- class SQLiteIni
- {
- private const String dbJournalPostfix = "-journal";
- private SQLiteConnection dbConnection;
- public bool Connected {
- get { return dbConnection != null && dbConnection.State != ConnectionState.Open; }
- }
- public SQLiteIni(String FileName, String Password = "")
- {
- try
- {
- if (File.Exists(FileName + dbJournalPostfix))
- File.Delete(FileName + dbJournalPostfix);
- if (!File.Exists(FileName))
- SQLiteConnection.CreateFile(FileName);
- dbConnection = new SQLiteConnection("DataSource=\"" + FileName + "\";Version=3;AutoVacuum=True;");
- dbConnection.Open();
- if (!string.IsNullOrEmpty(Password))
- dbConnection.ChangePassword(Password);
- if (dbConnection.State == ConnectionState.Open)
- {
- SQLiteCommand dbCommand = new SQLiteCommand(dbConnection);
- dbCommand.CommandText = "VACUUM";
- dbCommand.ExecuteNonQuery();
- }
- }
- catch (SQLiteException ex)
- {
- dbConnection.Close();
- MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- ~SQLiteIni()
- {
- /*if (dbConnection != null && dbConnection.State == ConnectionState.Open)
- {
- SQLiteCommand dbCommand = new SQLiteCommand(dbConnection);
- dbCommand.CommandText = "VACUUM";
- dbCommand.ExecuteNonQuery();
- dbConnection.Close();
- }*/
- }
- public dynamic Read(String Section, String Variable, dynamic Default)
- {
- if (dbConnection != null && dbConnection.State == ConnectionState.Open)
- {
- try
- {
- DataTable dTable = new DataTable();
- String sqlQuery = string.Format("SELECT * FROM [sqlite_master] WHERE [name] = '{0}' and [type] = 'table'", Section);
- SQLiteDataAdapter adapter = new SQLiteDataAdapter(sqlQuery, dbConnection);
- adapter.Fill(dTable);
- if (dTable.Rows.Count > 0)
- {
- dTable.Clear();
- dTable.Columns.Clear();
- sqlQuery = string.Format("SELECT [Value] FROM [{0}] WHERE [Variable] = '{1}'", Section, Variable);
- adapter = new SQLiteDataAdapter(sqlQuery, dbConnection);
- adapter.Fill(dTable);
- if (dTable.Rows.Count > 0)
- return dTable.Rows[0][0];
- else
- return Default;
- }
- else
- return Default;
- }
- catch (SQLiteException ex)
- {
- MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
- return Default;
- }
- } else
- return Default;
- }
- public void Write(String Section, String Variable, dynamic Value)
- {
- if (dbConnection != null && dbConnection.State == ConnectionState.Open)
- {
- try
- {
- DataTable dTable = new DataTable();
- SQLiteCommand dbCommand = new SQLiteCommand(dbConnection);
- String sqlQuery = string.Format("SELECT * FROM [sqlite_master] WHERE [name] = '{0}' and [type] = 'table'", Section);
- SQLiteDataAdapter adapter = new SQLiteDataAdapter(sqlQuery, dbConnection);
- adapter.Fill(dTable);
- if (dTable.Rows.Count <= 0)
- {
- dbCommand.CommandText = string.Format("CREATE TABLE IF NOT EXISTS [{0}]([Variable] TEXT PRIMARY KEY NOT NULL, [Value] TEXT);", Section);
- dbCommand.ExecuteNonQuery();
- }
- dTable.Clear();
- dTable.Columns.Clear();
- sqlQuery = string.Format("SELECT [Value] FROM [{0}] WHERE [Variable] = '{1}'", Section, Variable);
- adapter = new SQLiteDataAdapter(sqlQuery, dbConnection);
- adapter.Fill(dTable);
- if (dTable.Rows.Count > 0)
- {
- dbCommand.CommandText = string.Format("UPDATE [{0}] SET [Value] = '{1}' WHERE [Variable] = '{2}'", Section, Value, Variable);
- dbCommand.ExecuteNonQuery();
- }
- else
- {
- dbCommand.CommandText = string.Format("INSERT INTO [{0}]([Variable], [Value]) VALUES('{1}', '{2}')", Section, Variable, Value);
- dbCommand.ExecuteNonQuery();
- }
- }
- catch (SQLiteException ex)
- {
- MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- }
- public byte[] Read(String Section, String Variable, byte[] Default)
- {
- if (dbConnection != null && dbConnection.State == ConnectionState.Open)
- {
- try
- {
- DataTable dTable = new DataTable();
- SQLiteCommand dbCommand = new SQLiteCommand(dbConnection);
- String sqlQuery = string.Format("SELECT * FROM [sqlite_master] WHERE [name] = '{0}' and [type] = 'table'", Section);
- SQLiteDataAdapter adapter = new SQLiteDataAdapter(sqlQuery, dbConnection);
- adapter.Fill(dTable);
- if (dTable.Rows.Count > 0)
- {
- dTable.Clear();
- dTable.Columns.Clear();
- dbCommand.CommandText = string.Format("SELECT [Value] FROM [{0}] WHERE [Variable] = '{1}'", Section, Variable);
- using (var reader = dbCommand.ExecuteReader())
- {
- if (reader.Read())
- {
- const int CHUNK_SIZE = 2 * 1024;
- byte[] buffer = new byte[CHUNK_SIZE];
- long bytesRead;
- long fieldOffset = 0;
- using (MemoryStream stream = new MemoryStream())
- {
- while ((bytesRead = reader.GetBytes(0, fieldOffset, buffer, 0, buffer.Length)) > 0)
- {
- stream.Write(buffer, 0, (int)bytesRead);
- fieldOffset += bytesRead;
- }
- return stream.ToArray();
- }
- }
- else
- return Default;
- }
- }
- else
- return Default;
- }
- catch (SQLiteException ex)
- {
- MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
- return Default;
- }
- }
- else
- return Default;
- }
- public void Write(String Section, String Variable, byte[] Value)
- {
- if (dbConnection != null && dbConnection.State == ConnectionState.Open)
- {
- try
- {
- DataTable dTable = new DataTable();
- SQLiteCommand dbCommand = new SQLiteCommand(dbConnection);
- String sqlQuery = string.Format("SELECT * FROM [sqlite_master] WHERE [name] = '{0}' and [type] = 'table'", Section);
- SQLiteDataAdapter adapter = new SQLiteDataAdapter(sqlQuery, dbConnection);
- adapter.Fill(dTable);
- if (dTable.Rows.Count <= 0)
- {
- dbCommand.CommandText = string.Format("CREATE TABLE IF NOT EXISTS [{0}]([Variable] TEXT PRIMARY KEY NOT NULL, [Value] BLOB);", Section);
- dbCommand.ExecuteNonQuery();
- }
- dTable.Clear();
- dTable.Columns.Clear();
- sqlQuery = string.Format("SELECT [Value] FROM [{0}] WHERE [Variable] = '{1}'", Section, Variable);
- adapter = new SQLiteDataAdapter(sqlQuery, dbConnection);
- adapter.Fill(dTable);
- if (dTable.Rows.Count > 0)
- {
- dbCommand.CommandText = string.Format("UPDATE [{0}] SET [Value] = @Value WHERE [Variable] = '{1}'", Section, Variable);
- dbCommand.Parameters.Add("@Value", DbType.Binary, Value.Length).Value = Value;
- dbCommand.ExecuteNonQuery();
- }
- else
- {
- dbCommand.CommandText = string.Format("INSERT INTO [{0}]([Variable], [Value]) VALUES('{1}', @Value)", Section, Variable);
- dbCommand.Parameters.Add("@Value", DbType.Binary, Value.Length).Value = Value;
- dbCommand.ExecuteNonQuery();
- }
- }
- catch (SQLiteException ex)
- {
- MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement