using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


namespace HISClinic.DataServices.Database
{
	/// <summary>
	/// DatabaseConnection 的摘要说明。
	/// </summary>
	internal class HISDBConfig:IDisposable
	{
		static string _ConnectionString;
		static SqlConnection _sqlConnection=null;
		static HISDBConfig()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
			GetConnectString();
		}
		
		/// <summary>
		/// 获取数据连接
		/// </summary>
		/// <returns></returns>
		public static SqlConnection GetConnection()
		{
			if(_sqlConnection==null)
			{
				_sqlConnection=new SqlConnection(_ConnectionString);
			}

			if(_sqlConnection.ConnectionString.Trim().Length<=0)
			{
				_sqlConnection.ConnectionString=_ConnectionString;
			};

			return _sqlConnection;
		}
        
		/// <summary>
		/// 获取新的数据库连接
		/// </summary>
		/// <returns></returns>
		public static SqlConnection GetNewConection()
		{
			return new SqlConnection(_ConnectionString);
		}

		/// <summary>
		/// 从配置文件中获取数据库连接字符串
		/// </summary>
		private static void GetConnectString()
		{
			_ConnectionString=ConfigurationSettings.AppSettings["DBConnectionString"];
		}
		#region IDisposable 成员

		public void Dispose()
		{
			// 释放掉连接
			if(_sqlConnection!=null)
			{
				if(_sqlConnection.State==ConnectionState.Open)
				{
					_sqlConnection.Close();
				}
				_sqlConnection.Dispose();
			}
		}

		#endregion
	}
	
}
