using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Reflection;
using System.Data;
using irisMemory;
using irisHelper;

namespace irisDataBase
{
    public class DatabaseLocal
    {
        public static MySqlConnection mySqlConnect = null;

        /// <summary>
        /// 打开数据库连接     
        /// </summary>
        /// <param name="sConnection"> connection </param>
        /// <returns> 0 on success </returns>
        public static int OpenDB(DbLocalConfig dbLocalConfig)
        {
            int iRetval = -1;
            try
            {
                string sConnection = "server=localhost"
                                   + ";port=" + dbLocalConfig.LocalDatabasePort
                                   + ";user=" + dbLocalConfig.LocalDatabaseUser
                                   + ";database=" + dbLocalConfig.LocalDatabaseName
                                   + ";password=" + dbLocalConfig.LocalDatabasePassword
                                   + ";Charset=utf8"
                                   + ";Allow User Variables=True";
                mySqlConnect = new MySqlConnection(sConnection);
                mySqlConnect.Open();
                iRetval = 0;
            }
            catch (Exception ex)
            {
                iRetval = -1;
                LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, "  Local_OpenDB : " + ex.Message);
            }

            return iRetval;
        }
        
        /// <summary>
        /// 关闭数据库连接    
        /// </summary>
        /// <returns> 0 on success </returns>
        public static int CloseDB()
        {
            int iRetval = -1;
            try
            {
                mySqlConnect.Close();
            }
            catch (MySqlException exSql)
            {
                iRetval = exSql.ErrorCode;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, "  Local_CloseDB : " + ex.Message);
            }

            return iRetval;
        }


    }
}