using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace parkMonitor.Database2
{
    /// <summary>
    /// 创建数据库连接的简单工厂
    /// </summary>
    class DatabaseConnPoolFactory
    {
        /// <summary>
        /// 根据连接字符串创建连接
        /// </summary>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        public static Connection CreateConnection(string connectionString)
        {
            MySql.Data.MySqlClient.MySqlConnection conn = null;
            Connection connection = new Connection();
            try
            {
                conn = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
                conn.Open();
                Console.WriteLine("连接成功");
                connection.mySqlConnection = conn;
                connection.mySqlConnFlag = true;
                connection.state = conn.State;
                return connection;
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                switch (ex.Number)
                {
                    case 0:
                        Console.WriteLine("Cannot connect to server.  Contact administrator");
                        connection.mySqlConnFlag = false;
                        connection.state = conn.State;
                        return connection;
                    case 1045:
                        Console.WriteLine("Invalid username/password, please try again");
                        connection.mySqlConnFlag = false;
                        connection.state = conn.State;
                        return connection;
                    default:
                        Console.WriteLine("database connection exception, please try again");
                        connection.mySqlConnFlag = false;
                        connection.state = conn.State;
                        return connection;
                }
            }
            catch (System.TimeoutException ex)
            {
                connection.mySqlConnFlag = false;
                connection.state = conn.State;
                Console.WriteLine(ex.Message);
                return connection;
            }
            catch (System.IO.IOException ex)
            {
                connection.mySqlConnFlag = false;
                connection.state = conn.State;
                Console.WriteLine(ex.Message);
                return connection;
            }
            catch (System.Net.Sockets.SocketException ex)
            {
                connection.mySqlConnFlag = false;
                connection.state = conn.State;
                Console.WriteLine(ex.Message);
                return connection;
            }
        }
    }
    /// <summary>
    /// 数据库连接类
    /// </summary>
    public class Connection
    {
        /// <summary>
        /// 类的构造函数
        /// </summary>
        public Connection()
        {
            mySqlConnection = null;
            mySqlConnFlag = false;
        }
        public MySql.Data.MySqlClient.MySqlConnection mySqlConnection { get; set; }

        public System.Data.ConnectionState state { get; set; }
        public bool mySqlConnFlag { get; set; }
        /// <summary>
        /// 连接关闭
        /// </summary>
        public void Close()
        {
            try
            {
                if (mySqlConnection != null)
                {
                    mySqlConnection.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        /// <summary>
        /// 连接dispose
        /// </summary>
        public void Dispose()
        {
            try
            {
                if (mySqlConnection != null)
                {
                    mySqlConnection.Dispose();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
