Lots of updates
This commit is contained in:
115
PWAPPv2/Source/Database/DatabaseConnection.cs
Normal file
115
PWAPPv2/Source/Database/DatabaseConnection.cs
Normal file
@@ -0,0 +1,115 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using MySqlConnector;
|
||||
|
||||
namespace PWAPPv2.Source.Database
|
||||
{
|
||||
class DatabaseConnection
|
||||
{
|
||||
|
||||
private MySqlConnection Connection;
|
||||
private string SqlString;
|
||||
DatabaseConfig Config;
|
||||
|
||||
public DatabaseConnection(DatabaseConfig config)
|
||||
{
|
||||
Config = config;
|
||||
SqlString = "server=" + config.host + ";Uid=" + config.user + ";database=" + config.database + ";Pwd=" + config.password;
|
||||
Connection = new MySqlConnection(SqlString);
|
||||
}
|
||||
|
||||
public void Connect()
|
||||
{
|
||||
try
|
||||
{
|
||||
Connection.Open();
|
||||
}
|
||||
catch(Exception)
|
||||
{
|
||||
throw new DatabaseConnectionException();
|
||||
}
|
||||
}
|
||||
|
||||
public List<string> QueryDatabase(string query)
|
||||
{
|
||||
try
|
||||
{
|
||||
Connection.Open();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw new CouldNotOpenConnectionException();
|
||||
}
|
||||
|
||||
MySqlCommand Command = new MySqlCommand(query, Connection);
|
||||
MySqlDataReader reader;
|
||||
try
|
||||
{
|
||||
reader = Command.ExecuteReader();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Connection.Close();
|
||||
throw new ReaderException();
|
||||
}
|
||||
List<string> result = new List<string>();
|
||||
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
int n = reader.VisibleFieldCount;
|
||||
for (int i = 0; i < reader.VisibleFieldCount; i++)
|
||||
{
|
||||
var type = reader.GetFieldType(i);
|
||||
if (type.Name == "Int64")
|
||||
{
|
||||
result.Add(reader.GetInt64(i).ToString());
|
||||
}
|
||||
if (type.Name == "Int32")
|
||||
{
|
||||
result.Add(reader.GetInt32(i).ToString());
|
||||
}
|
||||
if (type.Name == "String")
|
||||
{
|
||||
result.Add(reader.GetString(i));
|
||||
|
||||
}
|
||||
if (type.Name == "Byte")
|
||||
{
|
||||
result.Add(reader.GetByte(i).ToString());
|
||||
}
|
||||
if (type.Name == "DateTime")
|
||||
{
|
||||
result.Add(reader.GetDateTime(i).ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
Connection.Close();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class DatabaseConfigurationException : Exception
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
class DatabaseConnectionException : Exception
|
||||
{ }
|
||||
|
||||
class CouldNotOpenConnectionException : Exception
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
class ReaderException : Exception
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user