Files
marechai/Cicm.Database/Mysql.cs

112 lines
3.2 KiB
C#
Raw Normal View History

2018-04-12 10:05:42 +01:00
using System;
using System.Data;
using MySql.Data.MySqlClient;
2018-04-12 05:35:47 +01:00
2018-04-12 06:43:45 +01:00
namespace Cicm.Database
2018-04-12 05:35:47 +01:00
{
2018-04-12 10:05:42 +01:00
public class Mysql : IDbCore
2018-04-12 05:35:47 +01:00
{
MySqlConnection connection;
public Mysql(string server, string user, string database, ushort port, string password)
{
string connectionString =
$"server={server};user={user};database={database};port={port};password={password}";
2018-04-12 10:05:42 +01:00
2018-04-12 05:35:47 +01:00
connection = new MySqlConnection(connectionString);
}
2018-04-12 10:05:42 +01:00
public Operations Operations { get; private set; }
public long LastInsertRowId
{
get
{
MySqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT LAST_INSERT_ID()";
IDataReader reader = command.ExecuteReader();
if(reader == null || !reader.Read()) return 0;
long id = reader.GetInt64(0);
reader.Close();
return id;
}
}
public bool OpenDb(string server, string user, string database, string password, ushort port = 3306)
{
try
{
string connectionString =
$"server={server};user={user};database={database};port={port};password={password}";
connection = new MySqlConnection(connectionString);
Operations = new Operations(connection, this);
bool res = Operations.UpdateDatabase();
if(res) return true;
connection = null;
return false;
}
catch(MySqlException ex)
{
Console.WriteLine("Error opening database.");
Console.WriteLine(ex);
connection = null;
return false;
}
}
public void CloseDb()
2018-04-12 05:35:47 +01:00
{
connection?.Close();
2018-04-12 10:05:42 +01:00
connection = null;
}
public bool CreateDb(string database, string server, string user, string password, ushort port = 3306)
{
try
{
string connectionString =
$"server={server};user={user};database={database};port={port};password={password}";
connection = new MySqlConnection(connectionString);
IDbCommand command = connection.CreateCommand();
command.CommandText = $"CREATE DATABASE `{database}`;";
command.ExecuteNonQuery();
command.CommandText = $"USE `{database}`;";
command.ExecuteNonQuery();
Operations = new Operations(connection, this);
bool res = Operations.InitializeNewDatabase();
if(res) return true;
connection = null;
return false;
}
catch(MySqlException ex)
{
Console.WriteLine("Error opening database.");
Console.WriteLine(ex);
connection = null;
return false;
}
}
public IDbDataAdapter GetNewDataAdapter()
{
return new MySqlDataAdapter();
}
~Mysql()
{
CloseDb();
2018-04-12 05:35:47 +01:00
}
}
}