121 lines
3.2 KiB
C#
121 lines
3.2 KiB
C#
using MySqlConnector;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace PWAPPv2.Source.Database
|
|
{
|
|
/// <summary>
|
|
/// Class <c>DatabaseConnection</c> creates and maintains a connection to and OpenDental MySqlDatabase.
|
|
/// </summary>
|
|
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 DatabaseConnection(Config.Configuration config)
|
|
{
|
|
SqlString = "server=" + config.Get("ODhost") + ";Uid=" + config.Get("ODuser") + ";database=" + config.Get("ODdatabase") + ";Pwd=" + config.Get("ODpassword");
|
|
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
|
|
{
|
|
|
|
}
|
|
}
|