using System;

namespace JXC_MIS.DataTier
{
	/// <summary>
	/// 有关数据库的操作如打开、关闭、事务、操作、查询、绑定。如不特别指出，构造函数默认SqlConnection数据库连接。
	/// 本类最新跨数据库版本请到www.WebMIS.com.cn下载。
	/// </summary>
	public class ClsAdoNet
	{
			/// <summary>
			/// 公有枚举成员-----数据库类型
			/// </summary>
			public enum DataBaseType{ACCESS97 = 0,ACCESS2KX = 1,SQLSERVER = 2,ORACLE = 3,ODBC = 4};		

			//***私有成员变量***私有成员变量***私有成员变量***私有成员变量***私有成员变量***私有成员变量***私有成员变量***私有成员变量***私有成员变量***私有成员变量***私有成员变量***私有成员变量***
			private bool	m_blnDefaultSqlConnection;				//默认连接SQL数据库，在构造函数中指出

			private string	m_strConnectionString;					//数据库连接字符串
			private bool	m_blnTransSuccess;						//事务处理是否成功

			/*
			 *******************************************************************************
			 *定义最常用的对象，以支持跨数据库、独立于 SqlClient、OleDb的类库
			 *******************************************************************************
			*/

			//连接
			private System.Data.SqlClient.SqlConnection		_SqlConn	= new System.Data.SqlClient.SqlConnection();		//Sql数据库连接
			private System.Data.OleDb.OleDbConnection		_OleDbConn	= new System.Data.OleDb.OleDbConnection();			//Oledb数据库连接
			//适配器
			private System.Data.SqlClient.SqlDataAdapter 	_SqlDtAdpt	= new System.Data.SqlClient.SqlDataAdapter();		//Sql数据适配器
			private System.Data.OleDb.OleDbDataAdapter 		_OleDbAdpt	= new System.Data.OleDb.OleDbDataAdapter();			//Oledb数据库连接
			//命令
			private System.Data.SqlClient.SqlCommand		_SqlCmd		= new System.Data.SqlClient.SqlCommand();			//定义Command对象，以支持事务处理(ExeSql)
			private System.Data.OleDb.OleDbCommand			_OleDbCmd	= new System.Data.OleDb.OleDbCommand();				//定义Command对象，以支持事务处理(ExeSql)
			//参数
			private System.Data.SqlClient.SqlParameter		_SqlPar		= new System.Data.SqlClient.SqlParameter();			//定义Parameter对象
			private System.Data.OleDb.OleDbParameter		_OleDbPar	= new System.Data.OleDb.OleDbParameter();			//定义Parameter对象
			//事务
			private System.Data.SqlClient.SqlTransaction	_SqlTran ;														//定义Transaction对象，以支持事务处理
			private System.Data.OleDb.OleDbTransaction		_OleDbTran;														//定义Transaction对象，以支持事务处理



		
			//***公有成员函数***公有成员函数***公有成员函数***公有成员函数***公有成员函数***公有成员函数***

			/// <summary>
			/// 构造函数默认SqlConnection数据库连接
			/// </summary>
			public ClsAdoNet()
			{
				m_blnDefaultSqlConnection = true;
			}

			/// <summary>
			/// 构造函数要指定是否默认SqlConnection数据库连接
			/// </summary>
			/// <param name="p_DefaultSqlConnection">true：SqlClient类库、false：OleDb的类库</param>
			public ClsAdoNet(bool p_DefaultSqlConnection)
			{
				m_blnDefaultSqlConnection = p_DefaultSqlConnection;
			}


			/// <summary>
			/// 根据联接串，打开并返回 SqlConnection/OleDbConnection 数据库联接
			/// </summary>
			/// <param name="p_ConnectionString">数据库联接字符串。如果不含有"PROVIDER"或者含有"SQLOLEDB"将返回SqlConnection的object，否则返回OleDbConnection的object</param>
			/// <returns></returns>
			public object GetDbConn(string p_ConnectionString)
			{
				object objConn;
				objConn = this.GetDbConnection(p_ConnectionString);

				if (objConn.GetType().ToString() == "System.Data.SqlClient.SqlConnection")
				{
					this._SqlConn = (System.Data.SqlClient.SqlConnection)objConn;
					m_strConnectionString = p_ConnectionString;
					return this._SqlConn;
				}
				else
				{
					this._OleDbConn = (System.Data.OleDb.OleDbConnection)objConn;
					m_strConnectionString = p_ConnectionString;
					return this._OleDbConn;
				}
			}

			/// <summary>
			/// 用ConnectionString打开并返回数据库连接
			/// </summary>
			/// <returns></returns>
			public object GetDbConn()
			{
				if (m_strConnectionString != "")
					return GetDbConn(m_strConnectionString);
				else
					return null;
			}

        
			/// <summary>
			/// 打开并返回 SqlConnection 联接
			/// </summary>
			///		<param name="Computer">装有SqlServer数据库的计算机名</param>
			///		<param name="DefaultDataBase">将要打开的默认数据库</param>
			///		<param name="UserID">用户名</param>
			///		<param name="PWD">密码</param>

			public System.Data.SqlClient.SqlConnection GetSqlConn(string Computer,string DefaultDataBase,string UserID,string PWD)
			{
				try
				{
					this._SqlConn = this.GetSqlConnection(Computer,DefaultDataBase,UserID,PWD);
					m_strConnectionString = this._SqlConn.ConnectionString;
					return this._SqlConn;
				}
				catch
				{
				return null;
				}
			}

			/// <summary>
			/// 用指定联接字符串打开并返回SqlConnection
			/// </summary>
			/// <param name="p_ConnectionString">数据库联接字符串。如果不含有"PROVIDER"或者含有"SQLOLEDB"将返回SqlConnection的object，否则返回OleDbConnection的object</param>
			/// <returns></returns>
			public System.Data.SqlClient.SqlConnection GetSqlConn(string p_ConnectionString)
			{
				if (IsSqlConnection(p_ConnectionString) == true)
					return (System.Data.SqlClient.SqlConnection)GetDbConn(p_ConnectionString);
				else
					throw new System.Exception("方法GetSqlConn(string)参数含有\"PROVIDER\"或者含有\"SQLOLEDB\"，不能返回SqlConnection");
				//return null;
			}

			/// <summary>
			/// 用ConnectionString打开并返回数据库连接
			/// </summary>
			/// <returns></returns>
			public System.Data.SqlClient.SqlConnection GetSqlConn()
			{
				if (_SqlConn.State == System.Data.ConnectionState.Open) 
				{
					return _SqlConn;
				}
				else
				{
					if (m_strConnectionString != "")			
					{
						return this.GetSqlConn(m_strConnectionString);
					}
					else
					{
						return null;
					}
				}
			}


			/// <summary>
			/// 打开并返回 OleDbConnection 联接
			/// </summary>
			///		<param name="DataBase">数据库类型enum DataBaseType{ACCESS97 = 0,ACCESS2KX = 1,SQLSERVER = 2,ORACLE = 3,ODBC = 4}</param>
			///		<param name="DataSourceOrAccessPath">数据源。装有SqlServer数据库的计算机名/Oracel的Net8连接串/Access文件路径/ODBC数据源</param>
			///		<param name="DefaultDataBaseOrAccessDatabasePassword">SqlServer默认数据库/Access数据库的密码</param>
			///		<param name="UserID">用户名</param>
			///		<param name="PWD">密码</param>
			public System.Data.OleDb.OleDbConnection GetOleDbConn(DataBaseType DataBase,string DataSourceOrAccessPath,string DefaultDataBaseOrAccessDatabasePassword,string UserID,string PWD)
			{
				this._OleDbConn = this.GetOleDbConnection(DataBase,DataSourceOrAccessPath,DefaultDataBaseOrAccessDatabasePassword, UserID, PWD);
				m_strConnectionString = this._OleDbConn.ConnectionString;
				return this._OleDbConn;
			}
			public  DataBaseType GetDataBaseType(int db)
			{
				//			public enum DataBaseType{ACCESS97 = 0,ACCESS2KX = 1,SQLSERVER = 2,ORACLE = 3,ODBC = 4};		
				switch (db)
				{
					case 0:
						return DataBaseType.ACCESS97;
					case 1:
						return DataBaseType.ACCESS2KX;
					case 2:
						return DataBaseType.SQLSERVER ;
					case 3:
						return DataBaseType.ORACLE ;
					case 4:
						return DataBaseType.ODBC ;
					default:
						return DataBaseType.ACCESS2KX;

				}//结束switch
			}

			/// <summary>
			/// 用指定联接字符串打开并返回OleDbConnection
			/// </summary>
			/// <param name="p_ConnectionString">数据库联接字符串。如果不含有"PROVIDER"或者含有"SQLOLEDB"将返回SqlConnection的object，否则返回OleDbConnection的object</param>
			/// <returns></returns>
			public System.Data.OleDb.OleDbConnection GetOleDbConn(string p_ConnectionString)
			{
				return (System.Data.OleDb.OleDbConnection)GetDbConn(p_ConnectionString);
			}

			/// <summary>
			/// 用ConnectionString打开并返回数据库连接
			/// </summary>
			/// <returns></returns>
			public System.Data.OleDb.OleDbConnection GetOleDbConn()
			{
				if (_OleDbConn.State == System.Data.ConnectionState.Open) 
				{
					return _OleDbConn;
				}
				else
				{
					if (m_strConnectionString != "")			
					{
						return this.GetOleDbConn(m_strConnectionString);
					}
					else
					{
						return null;
					}
				}
			}

		
			/// <summary>
			/// 关闭数据库连接
			/// </summary>
			public void CloseDbConn()
			{			
				try
				{
					if (_SqlConn.State == System.Data.ConnectionState.Open)
						_SqlConn.Close();

					if (_OleDbConn.State == System.Data.ConnectionState.Open)
						_OleDbConn.Close();

					_SqlConn = null;
					_OleDbConn = null;

				}
				catch{}				
			}


			/// <summary>
			/// 开始事务
			/// </summary>
			public void BeginTrans()
			{
				m_blnTransSuccess = true;	//默认执行成功，一旦ExeSql中有执行不成功SQL，m_blnTransSuccess = false

				if (m_blnDefaultSqlConnection == true)
				{
					_SqlTran = SqlConn.BeginTransaction();
					_SqlCmd.Transaction = _SqlTran;
				}
				else
				{
					_OleDbTran = _OleDbConn.BeginTransaction();
					_OleDbCmd.Transaction = _OleDbTran;
				}
			}
			/// <summary>
			/// 提交事务
			/// </summary>
			public void Commit()
			{
				if (m_blnDefaultSqlConnection  == true)
				{
					_SqlTran.Commit();
				}
				else
				{
					_OleDbTran.Commit();
				}
			}
		
			/// <summary>
			/// 事务回滚
			/// </summary>
			public void Rollback()
			{
				if (m_blnDefaultSqlConnection == true)
				{
					_SqlTran.Rollback();
				}
				else
				{
					_OleDbTran.Rollback();
				}
			}


			/// <summary>
			/// 根据默认连接数据库类型(默认SqlConnection)隐示获取或设置数据库连接
			/// </summary>
			public object DbConn
			{
				get
				{
					if (m_blnDefaultSqlConnection == true)
						return _SqlConn;
					else
						return _OleDbConn;
				}
				set
				{
					if (m_blnDefaultSqlConnection == true)
						_SqlConn = (System.Data.SqlClient.SqlConnection)value;
					else
						_OleDbConn = (System.Data.OleDb.OleDbConnection)value;
				}		
			}
		
		
			/// <summary>
			/// 显示获取或设置SqlConn
			/// </summary>
			public System.Data.SqlClient.SqlConnection SqlConn
			{
				get
				{
					return _SqlConn;
				}
				set
				{
					_SqlConn = value;
				}		
			}

			/// <summary>
			/// 显示获取或设置OleDbConn
			/// </summary>
			public System.Data.OleDb.OleDbConnection OleDbConn
			{
				get
				{
					return _OleDbConn;
				}
				set
				{
					_OleDbConn = value;
				}		
			}






			/// <summary>
			/// 根据默认连接数据库类型(默认SqlConnection)隐示获取或设置数据适配器
			/// </summary>
			public object DbAdapter
			{
				get
				{
					if (m_blnDefaultSqlConnection == true)
						return _SqlDtAdpt;
					else
						return _OleDbAdpt;
				}
				set
				{
					if (m_blnDefaultSqlConnection == true)
						_SqlDtAdpt = (System.Data.SqlClient.SqlDataAdapter)value;
					else
						_OleDbAdpt = (System.Data.OleDb.OleDbDataAdapter)value;
				}		
			}
		
		
			/// <summary>
			/// 显示获取或设置SqlDataAdapter
			/// </summary>
			public System.Data.SqlClient.SqlDataAdapter SqlAdapter
			{
				get
				{
					return _SqlDtAdpt;
				}
				set
				{
					_SqlDtAdpt = value;
				}		
			}

			/// <summary>
			/// 显示获取或设置OleDbDataAdapter
			/// </summary>
			public System.Data.OleDb.OleDbDataAdapter OleDbAdapter
			{
				get
				{
					return _OleDbAdpt;
				}
				set
				{
					_OleDbAdpt = value;
				}		
			}


			/// <summary>
			/// 根据默认连接数据库类型(默认SqlConnection)隐示获取或设置数据库Command对象
			/// </summary>
			public object DbCmd
			{
				get
				{
					if (m_blnDefaultSqlConnection == true)
						return _SqlCmd;
					else
						return _OleDbCmd;
				}
				set
				{
					if (m_blnDefaultSqlConnection == true)
						_SqlCmd=(System.Data.SqlClient.SqlCommand)value;
					else
						_OleDbCmd=(System.Data.OleDb.OleDbCommand)value;
				}		
			}


        
			/// <summary>
			/// 显示获取或设置SqlCommand
			/// </summary>
			public System.Data.SqlClient.SqlCommand SqlCmd
			{
				get
				{
					return _SqlCmd;
				}
				set
				{
					_SqlCmd=value;
				}		
			}

			/// <summary>
			/// 显示获取或设置OleDbCommand
			/// </summary>
			public System.Data.OleDb.OleDbCommand OleDbCmd
			{
				get
				{
					return _OleDbCmd;
				}
				set
				{
					_OleDbCmd=value;
				}		
			}


			/// <summary>
			/// 根据默认连接数据库类型(默认SqlConnection)隐示获取或设置数据库Parameter对象
			/// </summary>
			public object DbPar
			{
				get
				{
					if (m_blnDefaultSqlConnection == true)
						return _SqlPar;
					else
						return _OleDbPar;
				}
				set
				{
					if (m_blnDefaultSqlConnection == true)
						_SqlPar=(System.Data.SqlClient.SqlParameter)value;
					else
						_OleDbPar=(System.Data.OleDb.OleDbParameter)value;
				}		
			}

		
			/// <summary>
			/// 显示获取或设置SqlParameter
			/// </summary>
			public System.Data.SqlClient.SqlParameter SqlPar
			{
				get
				{
					return _SqlPar;
				}
				set
				{
					_SqlPar = value;
				}		
			}

			/// <summary>
			/// 显示获取或设置OleDbParameter
			/// </summary>
			public System.Data.OleDb.OleDbParameter OleDbPar
			{
				get
				{
					return _OleDbPar;
				}
				set
				{
					_OleDbPar = value;
				}		
			}

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

			/// <summary>
			/// 执行SQL,返回值：-1:失败    >=0:成功，并返回执行的行数
			/// </summary>
			/// <param name="p_CommandText">指定的操作型SQL语句(DELETE/INSERT/UPDATE)或操作型存储过程</param>
			/// <param name="p_SqlConnection">SqlConnection连接对象</param>
			/// <returns>int 返回值：-1:失败    >=0:成功</returns>
			public int ExeSql(string p_CommandText, System.Data.SqlClient.SqlConnection p_SqlConnection)
			{
				int intEffect;
				intEffect=ExecuteSql(p_CommandText,p_SqlConnection);
				//设置事务成功执行状态
				if (intEffect == -1)
					m_blnTransSuccess=false;
				return intEffect;
			}

			/// <param name="p_CommandText">指定的操作型SQL语句(DELETE/INSERT/UPDATE)或操作型存储过程</param>
			/// <param name="p_OleDbConnection">OleDbConnection连接对象</param>
			public int ExeSql(string p_CommandText, System.Data.OleDb.OleDbConnection p_OleDbConnection)
			{
				int intEffect;
				intEffect=ExecuteSql(p_CommandText,p_OleDbConnection);
				//设置事务成功执行状态
				if (intEffect == -1)
					m_blnTransSuccess=false;
				return intEffect;
			}

			/// <param name="p_CommandText">指定的操作型SQL语句(DELETE/INSERT/UPDATE)或操作型存储过程</param>
			/// <param name="connection">隐示Connection连接对象</param>
			public int ExeSql(string p_CommandText, object p_Connection)
			{
				if (p_Connection.GetType().ToString() == "System.Data.SqlClient.SqlConnection")
				{
					return ExeSql(p_CommandText,(System.Data.SqlClient.SqlConnection)p_Connection);
				}
				else
				{
					return ExeSql(p_CommandText,(System.Data.OleDb.OleDbConnection)p_Connection);
				}
			}

			/// <param name="p_CommandText">用已打开的数据库执行操作型SQL语句(DELETE/INSERT/UPDATE)或操作型存储过程</param>
			/// <returns></returns>
			public int ExeSql(string p_CommandText)
			{
				if (m_blnDefaultSqlConnection == true)
					if (_SqlConn.State == System.Data.ConnectionState.Open)
						return ExeSql(p_CommandText,_SqlConn);
					else
						return ExeSql(p_CommandText,GetSqlConn());
				else
					if (_OleDbConn.State == System.Data.ConnectionState.Open)
					return ExeSql(p_CommandText,_OleDbConn);
				else
					return ExeSql(p_CommandText,GetOleDbConn());
			}

			/// <summary>
			/// 静态方法，适合不启动事务的一次执行更新查询SQL语句(DELETE/INSERT/UPDATE)或执行型存储过程任务。返回值：-1:失败    >=0:成功，并返回执行的行数
			/// </summary>
			/// <Info>
			///		<作者>周方勇</作者>
			///		<创建日期>2002-10-05</创建日期>
			///		<修改日期>2003-04-15</修改日期>
			///		<其它说明>无</其它说明>
			/// </Info>
			/// <Input>
			///		<param name="strSQL">更新查询SQL语句(DELETE/INSERT/UPDATE)或执行型存储过程任务</param>
			///		<param name="ConnectionString">数据库连接字符串，方法会根据字符串特征自动优化执行数据库</param>
			/// </Input>   
			/// <Other>
			/// 无
			/// </Other>
			/// <returns>返回值：-1:失败    >=0:成功，并返回执行的行数</returns>
			public static int ExeSql(string strSQL, string ConnectionString)
			{
				int intEffect=-1;
				string strErrInfo="";
				System.Data.SqlClient.SqlCommand sqlCmd=new System.Data.SqlClient.SqlCommand();
				System.Data.OleDb.OleDbCommand oleCmd=new System.Data.OleDb.OleDbCommand();
				try
				{				
					if (ConnectionString.ToUpper().IndexOf("PROVIDER") < 0 || (ConnectionString.ToUpper().IndexOf("PROVIDER") != -1 && ConnectionString.ToUpper().IndexOf("SQLOLEDB") != -1))
					{				
						//SQLServer连接
						//ConnectionString.ToUpper.IndexOf("PROVIDER")=-1 没找到
						sqlCmd.Connection = new System.Data.SqlClient.SqlConnection();
						sqlCmd.Connection.ConnectionString = ConnectionString;
						sqlCmd.Connection.Open();
						sqlCmd.CommandText = strSQL;
						sqlCmd.CommandType = System.Data.CommandType.Text;
						intEffect = sqlCmd.ExecuteNonQuery();
						sqlCmd.Connection.Close();
					}
					else
					{
						//Oledb连接
						oleCmd.Connection = new System.Data.OleDb.OleDbConnection();
						oleCmd.Connection.ConnectionString = ConnectionString;
						oleCmd.Connection.Open();
						oleCmd.CommandText = strSQL;
						oleCmd.CommandType = System.Data.CommandType.Text;
						intEffect = oleCmd.ExecuteNonQuery();
						oleCmd.Connection.Close();
					}
				}
				catch(System.Exception ex)
				{
					strErrInfo="错误提示：更新数据库失败！" + "\n" + ex.Message;
				}
				finally
				{
					if (strErrInfo!="")
						System.Windows.Forms.MessageBox.Show(strErrInfo+"\n"+strSQL,"提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning);

					sqlCmd=null;
					oleCmd=null;
				}
				return intEffect;
			}//End Function






			//实现方法(可以移植，独立编译，只需注意说明即可)
			//-------------------------------------------------------------------------------------------------------------------------

			/// <summary>
			/// 根据联接串，打开并返回 SqlConnection/OleDbConnection 数据库联接
			/// </summary>
			/// <Info>
			///		<作者>周方勇</作者>
			///		<创建日期>2002-10-05</创建日期>
			///		<修改日期>2003-04-15</修改日期>
			///		<其它说明>无</其它说明>
			/// </Info>
			/// <Input>
			///		<param name="strConnectionString">数据库联接字符串。如果不含有"PROVIDER"或者含有"SQLOLEDB"将返回SqlConnection的object，否则返回OleDbConnection的object</param>
			/// </Input>   
			/// <Other>无</Other>
			/// <returns>返回 object类型的 SqlConnection/OleDbConnection 数据库联接</returns>
			public object GetDbConnection(string strConnectionString)
			{
				//标记是否为SqlServer数据库连接
				bool blnSql;	
	
				//定义Sql数据库连接
				System.Data.SqlClient.SqlConnection sqlConn=new System.Data.SqlClient.SqlConnection();

				//定义OleDb数据库连接
				System.Data.OleDb.OleDbConnection oleDbConn=new System.Data.OleDb.OleDbConnection();
			
				blnSql=IsSqlConnection(strConnectionString);			
				//        '清空错误
				//        Err.Clear()

				//打开数据库
				try
				{
					if (blnSql == true)
					{
						sqlConn.ConnectionString=strConnectionString;
						sqlConn.Open();
					
					}
					else
					{
						oleDbConn.ConnectionString=strConnectionString;
						oleDbConn.Open();
					}
				}
				catch(System.Exception ex)
				{
					System.Windows.Forms.MessageBox.Show("错误提示：数据库打开失败，请与管理员联系！" + "\n" + "详细信息：" + ex.Message.ToString(), "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
				}

				if (blnSql == true)
					return sqlConn;
				else
					return oleDbConn;
			}//GetDbConnection()结束
		
		
			/// <summary>
			/// 打开并返回 SqlConnection 联接
			/// </summary>
			/// <Info>
			///		<作者>周方勇</作者>
			///		<创建日期>2002-10-05</创建日期>
			///		<修改日期>2003-04-15</修改日期>
			///		<其它说明>无</其它说明>
			/// </Info>
			/// <Input>
			///		<param name="strComputer">装有SqlServer数据库的计算机名</param>
			///		<param name="strDefaultDataBase">将要打开的默认数据库</param>
			///		<param name="strUserID">用户名</param>
			///		<param name="strPWD">密码</param>
			/// </Input>   
			/// <Other>无</Other>
			/// <returns>返回 SqlConnection 联接</returns>
		
			public System.Data.SqlClient.SqlConnection GetSqlConnection(string strComputer,string strDefaultDataBase,string strUserID,string strPWD)
			{
				//定义连接字符串
				string strConn;
				//定义SQLServer数据库连接
				System.Data.SqlClient.SqlConnection sqlConn=new System.Data.SqlClient.SqlConnection();
				strConn = "Data Source=" + strComputer +						
					";Initial Catalog=" + strDefaultDataBase +
					";Pooling = true" +			//启用链接池
					";Min Pool Size = 1" +		//可设置最小、最大池数
					";User ID=" + strUserID +
					";Password=" + strPWD;

				//        '清空错误
				//        Err.Clear()

				//打开数据库
				try
				{
					sqlConn.ConnectionString=strConn;
					sqlConn.Open();
				}
				catch(System.Exception ex)
				{
					System.Windows.Forms.MessageBox.Show("错误提示：数据库打开失败，请与管理员联系！" + "\n" + "详细信息：" + ex.Message.ToString(), "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
					sqlConn=null;	//对应VB的Nothing，null 关键字是表示不引用任何对象的空引用的文字值。null 是引用类型变量的默认值。
				}
			
				//返回
				return sqlConn;			
			}//GetSqlConn()结束

			/// <summary>
			/// 打开并返回 OleDbConnection 联接
			/// </summary>
			/// <Info>
			///		<作者>周方勇</作者>
			///		<创建日期>2002-10-05</创建日期>
			///		<修改日期>2003-04-15</修改日期>
			///		<其它说明>无</其它说明>
			/// </Info>
			/// <Input>
			///		<param name="DataBase">数据库类型enum DataBaseType{ACCESS97 = 0,ACCESS2KX = 1,SQLSERVER = 2,ORACLE = 3,ODBC = 4}</param>
			///		<param name="StrDataSourceOrAccessPath">数据源。装有SqlServer数据库的计算机名/Oracel的Net8连接串/Access文件路径/ODBC数据源</param>
			///		<param name="strDefaultDataBaseOrAccessDatabasePassword">SqlServer默认数据库/Access数据库的密码</param>
			///		<param name="strUserID">用户名</param>
			///		<param name="strPWD">密码</param>
			/// </Input>   
			/// <Other>
			/// 要独立运行，定义 public enum DataBaseType{ACCESS97 = 0,ACCESS2KX = 1,SQLSERVER = 2,ORACLE = 3,ODBC = 4};
			/// </Other>
			/// <returns>返回 OleDbConnection 联接</returns>
			public System.Data.OleDb.OleDbConnection GetOleDbConnection(DataBaseType DataBase,string StrDataSourceOrAccessPath,string strDefaultDataBaseOrAccessDatabasePassword,string strUserID,string strPWD)
			{
				//定义连接字符串
				string strConn="";
				//定义OleDb数据库连接
				System.Data.OleDb.OleDbConnection oleDbConn=new System.Data.OleDb.OleDbConnection();
				switch (DataBase)
				{
					case DataBaseType.ACCESS97:
						strConn = "Provider=Microsoft.Jet.OLEDB.3.51;" + "Password=" + strPWD + ";User ID=" + strUserID + ";Data Source=" + StrDataSourceOrAccessPath + ";Jet OLEDB:Database Password=" + strDefaultDataBaseOrAccessDatabasePassword;
						break;
					case DataBaseType.ACCESS2KX:
						strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Password=" + strPWD + ";User ID=" + strUserID + ";Data Source=" + StrDataSourceOrAccessPath + ";Jet OLEDB:Database Password=" + strDefaultDataBaseOrAccessDatabasePassword;
						break;
					case DataBaseType.SQLSERVER:
						strConn = "Provider=SQLOLEDB.1;Password=" + strPWD + ";User ID=" + strUserID + ";Data Source=" + StrDataSourceOrAccessPath + ";Initial Catalog=" + strDefaultDataBaseOrAccessDatabasePassword + ";Persist Security Info=True";
						break;
					case DataBaseType.ORACLE:
						strConn = "Provider=MSDAORA.1;Password=" + strPWD + ";User ID=" + strUserID + ";Data Source=" + StrDataSourceOrAccessPath + ";Persist Security Info=True";
						break;
					case DataBaseType.ODBC:
						strConn = "DSN=" + StrDataSourceOrAccessPath + ";UID=" + strUserID + ";PWD=" + strPWD;
						break;
						//default:
				}//结束switch

				//        '清空错误
				//        Err.Clear()

				//打开数据库
				try
				{
					oleDbConn.ConnectionString=strConn;
					oleDbConn.Open();
				}
				catch(System.Exception ex)
				{
					System.Windows.Forms.MessageBox.Show("错误提示：数据库打开失败，请与管理员联系！" + "\n" + "详细信息：" + ex.Message.ToString(), "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
					oleDbConn=null;	//对应VB的Nothing，null 关键字是表示不引用任何对象的空引用的文字值。null 是引用类型变量的默认值。
				}
			
				//返回
				return oleDbConn;			
			}//GetOleDbConn()结束

			/// <summary>
			/// 用指定连接执行操作更新查询SQL语句(DELETE/INSERT/UPDATE),返回值：-1:失败    >=0:成功，并返回执行的行数
			/// </summary>
			/// <Info>
			///		<作者>周方勇</作者>
			///		<创建日期>2002-10-05</创建日期>
			///		<修改日期>2003-04-15</修改日期>
			///		<其它说明>无</其它说明>
			/// </Info>
			/// <Input>
			///		<param name="strSQL">更新查询SQL语句(DELETE/INSERT/UPDATE)或执行型存储过程</param>
			///		<param name="Connection">数据库连接</param>
			/// </Input>   
			/// <Other>
			/// 这里将定义的_SqlCmd、_OleDbCmd注释掉主要是用已定义类的command对象，便于事务执行，要独立运行，去掉定义注释。
			/// </Other>
			/// <returns>返回值：-1:失败    >=0:成功，并返回执行的行数</returns>

			public int ExecuteSql(string strSQL , object Connection)
			{
				//define string variable for get SQL type...
				string strType;
				//define effected values for Exeute SQL . Default Value is -1 that show execute failuere.....
				int intEffect=-1;
				//define Err info...
				string strErrInfo="";
				//define bool variable for check SQL is StoredProcedure or not
				bool blnStoredProcedure;

				//define commmad object for Exeute SQL...
				/*
				System.Data.SqlClient.SqlCommand _SqlCmd=new System.Data.SqlClient.SqlCommand();
				System.Data.OleDb.OleDbCommand _OleDbCmd= new System.Data.OleDb.OleDbCommand();
				*/

				//检查第一参数//DELETE/INSERT/UPDATE			
				strType = strSQL.Substring(0, 6).ToUpper();
			
				if (strType != "DELETE" && strType != "INSERT" && strType != "UPDATE")
				{
					//return -1;
					blnStoredProcedure = true;
				}
				else
				{
					blnStoredProcedure = false;
				}

				//Err.Clear()

				//检查第二参数是否为数据库连接对象
				if (Connection.GetType().ToString()!="System.Data.SqlClient.SqlConnection" && Connection.GetType().ToString()!="System.Data.OleDb.OleDbConnection")				 
					throw new System.Exception("方法ExecuteSql(string,object)第二参数\n必须是类型为System.Data.SqlClient.SqlConnection或者\nSystem.Data.OleDb.OleDbConnection的数据库连接对象。");

				try
				{
					if (Connection.GetType().ToString()  ==  "System.Data.SqlClient.SqlConnection")
					{
						_SqlCmd.Connection = (System.Data.SqlClient.SqlConnection)Connection;
						
						if (_SqlCmd.Connection.State!=System.Data.ConnectionState.Open)
							return -1;

						_SqlCmd.CommandText = strSQL;
					
						if (blnStoredProcedure  ==  true)
							_SqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
						else
							_SqlCmd.CommandType = System.Data.CommandType.Text;

						intEffect = _SqlCmd.ExecuteNonQuery();
					}
					else if(Connection.GetType().ToString()  ==  "System.Data.OleDb.OleDbConnection")
					{
						_OleDbCmd.Connection = (System.Data.OleDb.OleDbConnection)Connection;
						if (_OleDbCmd.Connection.State!=System.Data.ConnectionState.Open)
							return -1;
						
						_OleDbCmd.CommandText = strSQL;

						if (blnStoredProcedure  ==  true)
							_OleDbCmd.CommandType = System.Data.CommandType.StoredProcedure;
						else
							_OleDbCmd.CommandType = System.Data.CommandType.Text;

						intEffect = _OleDbCmd.ExecuteNonQuery();
					}
				}

				catch(System.Exception ex)
				{			                
					strErrInfo="错误提示：更新数据库失败！" + "\n" + ex.Message;
				}	

				finally
				{
					if (strErrInfo!="")
						System.Windows.Forms.MessageBox.Show(strErrInfo,"提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning);
					/*
					 //释放资源，返回
					_SqlCmd = null;
					_OleDbCmd = null;
					*/
				}
				return intEffect;
			}//End Function


			/// <summary>
			/// 判断是否为SqlConnection连接字符串
			/// </summary>
			/// <param name="strConnectionString">连接字符串</param>
			/// <returns>true：SqlConnection连接字符串 false：OleDbConnection连接字符串</returns>
			private bool IsSqlConnection(string strConnectionString)
			{
				//标记是否为SqlServer数据库连接
				bool blnSql;	
	
				if (strConnectionString.Length<=0)
					return false;
		
				//if (strConnectionString.ToUpper().IndexOf("PROVIDER") < 0 || (strConnectionString.ToUpper().IndexOf("PROVIDER") != -1 && strConnectionString.ToUpper().IndexOf("SQLOLEDB") != -1))
				if (strConnectionString.ToUpper().IndexOf("PROVIDER") < 0)
				{
					//SQLServer连接
					//strConnectionString.ToUpper.IndexOf("PROVIDER")=-1 没找到
					blnSql=true;		
				}
				else
				{
					//OleDb连接
					blnSql=false;		
				}
				return blnSql;
			}//End Function		
		
			//-------------------------------------------------------------------------------------------------------------------------

			/// <summary>
			/// 根据Command，返回数据集
			/// </summary>
			/// <param name="Command">Command对象</param>
			/// <returns></returns>
			public System.Data.DataSet GetDataSet(object Command)
			{
				//define Err info...
				string strErrInfo="";

				//定义数据集对象
				System.Data.DataSet dst = new System.Data.DataSet();

				//define DataAdapter object for fill dataset...
				System.Data.SqlClient.SqlDataAdapter SqlDtAdpt = new System.Data.SqlClient.SqlDataAdapter();
				System.Data.OleDb.OleDbDataAdapter OleDbDtAdpt = new System.Data.OleDb.OleDbDataAdapter();
				


				//Err.Clear()

				//检查参数是否为Command
				if (Command.GetType().ToString()!="System.Data.SqlClient.SqlCommand" && Command.GetType().ToString()!="System.Data.OleDb.OleDbCommand")				 
					throw new System.Exception("方法GetDataSet(object)第一参数\n必须是类型为System.Data.SqlClient.SqlCommand或者\nSystem.Data.OleDb.OleCommand的数据库连接对象。");

				try
				{
					if (Command.GetType().ToString()  ==  "System.Data.SqlClient.SqlCommand")
					{
						SqlDtAdpt.SelectCommand = (System.Data.SqlClient.SqlCommand)Command;
						//Command.Connection = (System.Data.SqlClient.SqlConnection)Connection;
						
						//填充数据集
						SqlDtAdpt.Fill(dst);
					}
					else if(Command.GetType().ToString()  ==  "System.Data.OleDb.OleDbCommand")
					{

						OleDbDtAdpt.SelectCommand = (System.Data.OleDb.OleDbCommand)Command;
						//Command.Connection = (System.Data.OleDb.OleDbConnection)Connection;

						//填充数据集
						OleDbDtAdpt.Fill(dst);
					}
				}

				catch(System.Exception ex)
				{			                
					strErrInfo="错误提示：获取数据信息失败！" + "\n" + ex.Message;
				}	

				finally
				{
					if (strErrInfo!="")
						System.Windows.Forms.MessageBox.Show(strErrInfo,"提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning);

					//释放资源，返回
					SqlDtAdpt = null;
					OleDbDtAdpt =null;
				}
				return dst;
			
			}

		public System.Data.DataSet GetDataSet(string strSqlOrTableOrView,object Connection)
		{
			//define string variable for get SQL type...
//			string strType;
			string strSQL = strSqlOrTableOrView;
//			strType = strSqlOrTableOrView.Trim().Substring(0, 6).ToUpper();

			System.Data.CommandType commandType;
			
//			if (strType != "SELECT")
//			{
//				strSQL = "SELECT * FROM " + strSqlOrTableOrView;
//			}

			commandType = System.Data.CommandType.Text;

			return GetDataSet(strSQL,Connection,commandType);
		}

		/// <summary>
		/// 根据SELECT语句或查询型存储过程，返回数据集
		/// </summary>
		/// <param name="strSQL">SELECT语句或存储过程</param>
		/// <param name="strDataTable">数据集表名</param>
		/// <param name="Connection">数据库连接对象</param>
		/// <returns></returns>
		public System.Data.DataSet GetDataSet(string strSQL,object Connection,System.Data.CommandType commandType)
		{
			//define Err info...
			string strErrInfo="";

			//定义数据集对象
			System.Data.DataSet dst = new System.Data.DataSet();

			//define commmad object for Exeute SQL...
			System.Data.SqlClient.SqlCommand SqlCmd=new System.Data.SqlClient.SqlCommand();
			System.Data.OleDb.OleDbCommand OleDbCmd= new System.Data.OleDb.OleDbCommand();

			//define DataAdapter object for fill dataset...
			System.Data.SqlClient.SqlDataAdapter SqlDtAdpt = new System.Data.SqlClient.SqlDataAdapter();
			System.Data.OleDb.OleDbDataAdapter OleDbDtAdpt = new System.Data.OleDb.OleDbDataAdapter();


			//Err.Clear()

			//检查第二参数是否为数据库连接对象
			if (Connection.GetType().ToString()!="System.Data.SqlClient.SqlConnection" && Connection.GetType().ToString()!="System.Data.OleDb.OleDbConnection")				 
				throw new System.Exception("方法ExecuteSql(string,object)第二参数\n必须是类型为System.Data.SqlClient.SqlConnection或者\nSystem.Data.OleDb.OleDbConnection的数据库连接对象。");

			try
			{
				if (Connection.GetType().ToString()  ==  "System.Data.SqlClient.SqlConnection")
				{
					SqlDtAdpt.SelectCommand = SqlCmd;
					SqlCmd.Connection = (System.Data.SqlClient.SqlConnection)Connection;
						
					if (SqlCmd.Connection.State!=System.Data.ConnectionState.Open)
						return null;

					SqlCmd.CommandText = strSQL;
					SqlCmd.CommandType = commandType;
					
					//填充数据集
					SqlDtAdpt.Fill(dst);
				}
				else if(Connection.GetType().ToString()  ==  "System.Data.OleDb.OleDbConnection")
				{

					OleDbDtAdpt.SelectCommand = OleDbCmd;
					OleDbCmd.Connection = (System.Data.OleDb.OleDbConnection)Connection;
					if (OleDbCmd.Connection.State!=System.Data.ConnectionState.Open)
						return null;
						
					OleDbCmd.CommandText = strSQL;
					OleDbCmd.CommandType = commandType;
	
					//填充数据集
					OleDbDtAdpt.Fill(dst);
				}
			}

			catch(System.Exception ex)
			{			                
				strErrInfo="错误提示：获取数据信息失败！" + "\n" + ex.Message;
			}	

			finally
			{
				if (strErrInfo!="")
					System.Windows.Forms.MessageBox.Show(strErrInfo,"提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning);

				//释放资源，返回
				SqlCmd = null;
				OleDbCmd = null;
				SqlDtAdpt = null;
				OleDbDtAdpt =null;
			}
			return dst;

		}

	}
}
