123 lines
4.4 KiB
C#
123 lines
4.4 KiB
C#
using System;
|
|
using System.Data;
|
|
using System.Data.SQLite;
|
|
using System.Diagnostics;
|
|
using System.Collections;
|
|
|
|
|
|
using BCG;
|
|
using System.Linq;
|
|
using System.Data.Entity;
|
|
using System.Collections.Generic;
|
|
using System.Data.SQLite;
|
|
|
|
|
|
namespace bts.SilverPine {
|
|
|
|
public static class SQLiteHelper {
|
|
public static string location = "C:\\Users\\m_tah\\development\\data\\";
|
|
public static string db_name = "stream_data.db";
|
|
|
|
public static DataTable execute_select_query(string connection_string, string query) {
|
|
// Create a new DataTable to store the results
|
|
DataTable dataTable = new DataTable();
|
|
|
|
// Create and open a SQLite connection using the provided connection string
|
|
using (SQLiteConnection connection = new SQLiteConnection(connection_string)) {
|
|
try {
|
|
connection.Open();
|
|
|
|
// Create a SQLite command with the provided query
|
|
using (SQLiteCommand command = new SQLiteCommand(query, connection)) {
|
|
// Use SQLiteDataAdapter to fill the DataTable
|
|
using (SQLiteDataAdapter adapter = new SQLiteDataAdapter(command)) {
|
|
adapter.Fill(dataTable);
|
|
}
|
|
}
|
|
} catch (Exception ex) {
|
|
Console.WriteLine("An error occurred: " + ex.Message);
|
|
}
|
|
}
|
|
// Return the populated DataTable
|
|
return dataTable;
|
|
}
|
|
|
|
public static List<Dictionary<string, object>> ExecuteBulkRead(string connectionString, string query) {
|
|
var result = new List<Dictionary<string, object>>();
|
|
|
|
using (var connection = new SQLiteConnection(connectionString)) {
|
|
connection.Open();
|
|
|
|
// Begin a transaction to optimize read speed
|
|
using (var transaction = connection.BeginTransaction())
|
|
using (var command = new SQLiteCommand(query, connection, transaction))
|
|
using (var reader = command.ExecuteReader()) {
|
|
while (reader.Read()) {
|
|
var row = new Dictionary<string, object>();
|
|
|
|
for (int i = 0; i < reader.FieldCount; i++) {
|
|
string columnName = reader.GetName(i);
|
|
object columnValue = reader.IsDBNull(i) ? null : reader.GetValue(i);
|
|
row[columnName] = columnValue;
|
|
}
|
|
|
|
result.Add(row);
|
|
}
|
|
|
|
// Commit the transaction
|
|
transaction.Commit();
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public static List<Dictionary<string, object>> get_table(string connectionString, string query) {
|
|
var result = new List<Dictionary<string, object>>();
|
|
|
|
using (var connection = new SQLiteConnection(connectionString)) {
|
|
try {
|
|
connection.Open();
|
|
|
|
using (var command = new SQLiteCommand(query, connection))
|
|
using (var reader = command.ExecuteReader()) {
|
|
while (reader.Read()) {
|
|
var row = new Dictionary<string, object>();
|
|
|
|
for (int i = 0; i < reader.FieldCount; i++) {
|
|
string columnName = reader.GetName(i);
|
|
object columnValue = reader.IsDBNull(i) ? null : reader.GetValue(i);
|
|
row[columnName] = columnValue;
|
|
}
|
|
|
|
result.Add(row);
|
|
}
|
|
}
|
|
} catch (Exception ex) {
|
|
Console.WriteLine("An error occurred: " + ex.Message);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static IEnumerable<Dictionary<string, object>> get_table_2(string connectionString, string query) {
|
|
using (var connection = new SQLiteConnection(connectionString)) {
|
|
connection.Open();
|
|
using (var command = new SQLiteCommand(query, connection))
|
|
using (var reader = command.ExecuteReader()) {
|
|
while (reader.Read()) {
|
|
var row = new Dictionary<string, object>();
|
|
for (int i = 0; i < reader.FieldCount; i++) {
|
|
string columnName = reader.GetName(i);
|
|
object columnValue = reader.IsDBNull(i) ? null : reader.GetValue(i);
|
|
row[columnName] = columnValue;
|
|
}
|
|
yield return row;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|