using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;

namespace TrainVideoDataDispose
{
   public class connection
    {
        private OracleConnection m_OracleConnectionForMain = null; //计量数据库
        private string m_szRunPath = "";
        /// <summary>
        /// 写日志信息到日志文件
        /// </summary>
        /// <param name="str"></param>
        private void WriteLog(string str)
        {
            m_szRunPath = System.Environment.CurrentDirectory;
            if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
            {
                System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
            }

            string strDate = System.DateTime.Now.ToString("yyyyMMdd");
            System.IO.TextWriter tw = new System.IO.StreamWriter(m_szRunPath + "\\log\\TrainSceneLogic_" + strDate + ".log", true);

            tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            tw.WriteLine(str);
            tw.WriteLine("\r\n");

            tw.Close();
        }
        /// <summary>
        /// 获得计量中心Oracle连接
        /// </summary>
        /// <returns></returns>
        public OracleConnection GetDefaultDatabaseConnectionForMain()
        {
            if (m_OracleConnectionForMain == null || m_OracleConnectionForMain.State != ConnectionState.Open)
            {
                try
                {
                    string formian = GetDefaultDatabaseConnectionStringForMain();
                    m_OracleConnectionForMain = new OracleConnection(formian);
                    m_OracleConnectionForMain.Open();
                }
                catch (Exception exp)
                {
                    WriteLog("获得中心数据库连接出错！" + exp.Message);
                }
            }

            return m_OracleConnectionForMain;
        }
        /// <summary>
        /// 获得计量中心数据库连接字符串
        /// </summary>
        /// <returns></returns>
        private string GetDefaultDatabaseConnectionStringForMain()
        {

            if (m_szRunPath.Length == 0)
            {
                m_szRunPath = System.Environment.CurrentDirectory;
            }

            DataSet ds = new DataSet();
            try
            {
                ds.ReadXml(m_szRunPath + "\\JLDB.cfg");
            }
            catch
            {
                return "";
            }

            foreach (DataRow dr in ds.Tables["database"].Rows)
            {
                if (dr["name"].ToString().Trim().ToUpper() == "JLDB")
                {
                    return dr["connectionString"].ToString().Trim();
                }
            }

            return "";
        }

    }
}
