using System;
using System.Data;
using JXC_MIS.MyClass ;

namespace JXC_MIS.DataTier
{
	/// <summary>
	/// 连接大型数据库 如SqlServer / Oracal。
	/// </summary>
	public class ClsDataBaseAccess
	{
		//SqlServer数据库
		private System.Data.SqlClient.SqlConnection _sysConn;
		private System.Data.SqlClient.SqlCommand _sysCmd;
		private System.Data.SqlClient.SqlDataAdapter _sysDtApt;
		private System.Data.SqlClient.SqlParameter _sysPar;
		private ClsAdoNet ado;
		static string m_DBServer="localhost";
		static string m_DBName="www.WebMIS.Net.cn";
		static string m_DBUser="sa";
		static string m_DBPassword="";

		public ClsDataBaseAccess()
		{
			if (GlobalData.DBServer!=null)
			{
				m_DBServer=GlobalData.DBServer ;
				m_DBUser=GlobalData.DBUserID;
				m_DBPassword=GlobalData.DBPassword ;
				m_DBName=GlobalData.DBName;
			}
			ado = new ClsAdoNet(true);
			//SqlServer数据库
//			_sysConn	= ado.GetSqlConn("localhost","www.WebMIS.Net.cn","sa",""); //localhost
			_sysConn	= ado.GetSqlConn(m_DBServer,m_DBName,m_DBUser,m_DBPassword); //localhost
			_sysDtApt	= ado.SqlAdapter;
			_sysCmd		= ado.SqlCmd;
			_sysPar		=ado.SqlPar;
			_sysCmd.Connection = _sysConn;

		}
		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		 ~ClsDataBaseAccess()
		{
//			_sysConn.Dispose();	
//			_sysDtApt.Dispose();	
//			_sysCmd.Dispose();	
//			_sysCmd.Connection.Dispose();	
		}

		public System.Data.SqlClient.SqlConnection sysConn
		{	
			get
			{
				return _sysConn;
			}		
		}

		public bool IsSql
		{	
			get
			{				
				return true;
			}		
		}

		public System.Data.SqlClient.SqlDataAdapter sysAdapter
		{	
			get
			{
				return _sysDtApt;
			}		
		}


		public System.Data.SqlClient.SqlCommand sysCmd
		{	
			get
			{
				return _sysCmd;
			}		
		}

		public System.Data.SqlClient.SqlParameter sysPar
		{	
			get
			{
				return _sysPar;
			}		
		}



		//以下与数据库类型无关

		/// <summary>
		/// 开始事务
		/// </summary>
		public void BeginTrans()
		{
			ado.BeginTrans();
		}
		/// <summary>
		/// 提交事务
		/// </summary>
		public void Commit()
		{
			ado.Commit();
		}

		/// <summary>
		/// 事务回滚
		/// </summary>
		public void Rollback()
		{
			ado.Rollback();			
		}

		/// <summary>
		/// 获取事务执行是否成功状态
		/// </summary>
		public bool IsSuccessTrans
		{
			get
			{
				return ado.IsSuccessTrans;
			}
		}


		/// <summary>
		/// 执行操作型SQL或操作型存储过程
		/// </summary>
		public int ExeSql(string strSQL)
		{	
			int intEffected;
			intEffected=ado.ExeSql(strSQL,this._sysConn);
			return intEffected;
		}

		/// <summary>
		/// 关闭数据库连接
		/// </summary>
		public void Close()
		{			
			try
			{
				if (_sysConn.State==ConnectionState.Open)
					_sysConn.Close();
				_sysConn=null;
				ado.CloseDbConn();
			}
			catch{}				
		}
		/// <summary>
		/// 打开数据库连接
		/// </summary>
		public void Open()
		{			
			try
			{
				if (_sysConn.State!=ConnectionState.Open)
					_sysConn.Open ();
			}
			catch{}				
		}
		/// <summary>
		/// 得到指定SQL或存储过程的数据集
		/// </summary>
		/// <param name="strSQL">指定选择查询或存储过程</param>
		/// <returns></returns>
		public System.Data.DataSet GetDataSet(string strSQL)
		{
			if (strSQL=="")  {return null;}

			return ado.GetDataSet(strSQL,_sysConn);	
		}

		public System.Data.DataSet GetDataSet(string strSQL,System.Data.CommandType commandType)
		{
			return ado.GetDataSet(strSQL,_sysConn,commandType);	
		}


		/// <summary>
		/// 得到默认Command的数据集
		/// </summary>
		/// <returns></returns>
		public System.Data.DataSet GetDataSet()
		{
			return ado.GetDataSet(sysCmd);
		}

		public System.Data.DataSet GetDataSet(object Command)
		{
			return ado.GetDataSet(Command);
		}
	}
}
