Initial commit
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
using System;
|
||||
using System.Windows.Forms;
|
||||
using System.Data;
|
||||
using System.Data.OleDb;
|
||||
using System.Text;
|
||||
|
||||
namespace Database {
|
||||
static public class OleDBHandler {
|
||||
static private OleDbConnection connection;
|
||||
static public OleDbConnection Connection {
|
||||
get {
|
||||
if (connection == null) {
|
||||
InitializeConnection();
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
}
|
||||
static private void InitializeConnection() {
|
||||
try {
|
||||
using (var ofDlg = new OpenFileDialog()) {
|
||||
ofDlg.InitialDirectory = "c:\\";
|
||||
ofDlg.Filter = "Access database files (*.accdb;*.mdb)|*.accdb;*.mdb";
|
||||
ofDlg.RestoreDirectory = true;
|
||||
ofDlg.CheckFileExists = true;
|
||||
|
||||
if (ofDlg.ShowDialog() == DialogResult.OK) {
|
||||
connection = new OleDbConnection(string.Format(
|
||||
"Provider = Microsoft.ACE.Oledb.12.0; Data Source = {0}"
|
||||
, ofDlg.FileName
|
||||
));
|
||||
}
|
||||
else {
|
||||
Environment.Exit(0);
|
||||
}
|
||||
}
|
||||
connection.Open();
|
||||
}
|
||||
catch (Exception e) {
|
||||
MessageBox.Show(
|
||||
e.Message
|
||||
, "Database connection error"
|
||||
, MessageBoxButtons.OK
|
||||
, MessageBoxIcon.Error
|
||||
);
|
||||
Environment.Exit(e.HResult);
|
||||
}
|
||||
}
|
||||
public enum ExecuteType { NonQuery, Scalar };
|
||||
public static object Execute(string command, ExecuteType type)
|
||||
{
|
||||
using (var statement = new OleDbCommand(command, Connection))
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case ExecuteType.NonQuery:
|
||||
return statement.ExecuteNonQuery();
|
||||
case ExecuteType.Scalar:
|
||||
return statement.ExecuteScalar();
|
||||
default:
|
||||
throw new ArgumentException("An unsupported method is specified");
|
||||
}
|
||||
}
|
||||
}
|
||||
public static OleDbCommand SelectCommand(string tableName, string fields)
|
||||
{
|
||||
var command = $"SELECT {fields} FROM [{tableName}];";
|
||||
return new OleDbCommand(command, Connection);
|
||||
}
|
||||
public static bool Delete(string tableName, int id) {
|
||||
string command = $"DELETE FROM [{tableName}] WHERE [id] = {id};";
|
||||
var result = Execute(command, ExecuteType.NonQuery);
|
||||
return Convert.ToBoolean(result);
|
||||
}
|
||||
private static string UpdatePart(params (string, object)[] listPairColumnNameValue) {
|
||||
StringBuilder result = new StringBuilder(string.Empty);
|
||||
var size = listPairColumnNameValue.Length;
|
||||
if (size < 1)
|
||||
return result.ToString();
|
||||
foreach (var pair in listPairColumnNameValue)
|
||||
if(pair.Item2 is string)
|
||||
result.Append($"[{pair.Item1}]='{pair.Item2}',");
|
||||
else
|
||||
result.Append($"[{pair.Item1}]={pair.Item2},");
|
||||
|
||||
return result.ToString().TrimEnd(',');
|
||||
}
|
||||
public static bool Update(string tableName, int id, params (string, object)[] listPairColumnNameValue)
|
||||
{
|
||||
string partCommand = UpdatePart(listPairColumnNameValue);
|
||||
if (partCommand.Length < 1)
|
||||
return true;
|
||||
string command = $"UPDATE [{tableName}] SET {partCommand} WHERE [id] = {id};";
|
||||
var result = Execute(command, ExecuteType.NonQuery);
|
||||
return Convert.ToBoolean(result);
|
||||
}
|
||||
private static string InsertPart(params (string, object)[] listPairColumnNameValue)
|
||||
{
|
||||
var size = listPairColumnNameValue.Length;
|
||||
if (size < 1)
|
||||
return string.Empty;
|
||||
|
||||
StringBuilder left = new StringBuilder("(");
|
||||
StringBuilder right = new StringBuilder("VALUES (");
|
||||
foreach (var pair in listPairColumnNameValue) {
|
||||
left.Append('[');
|
||||
left.Append(pair.Item1);
|
||||
left.Append("],");
|
||||
if (pair.Item2 is string)
|
||||
right.Append('\'' + pair.Item2.ToString() + '\'' + ',');
|
||||
else
|
||||
right.Append(pair.Item2.ToString() + ',');
|
||||
}
|
||||
var leftResult = left.ToString().TrimEnd(',') + ')';
|
||||
var rightResult = right.ToString().TrimEnd(',') + ')';
|
||||
return leftResult + rightResult;
|
||||
}
|
||||
public static bool Insert(string tableName, params (string, object)[] listPairColumnNameValue)
|
||||
{
|
||||
string partCommand = InsertPart(listPairColumnNameValue);
|
||||
if (partCommand.Length < 1)
|
||||
return true;
|
||||
string command = $"INSERT INTO [{tableName}] {partCommand};";
|
||||
var result = Execute(command, ExecuteType.NonQuery);
|
||||
return Convert.ToBoolean(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user