www.pudn.com > WorkflowDesigner.rar > MSSqlDataAccess.cs, change:2007-11-16,size:3905b


using System; 
using System.Data; 
using System.Data.SqlClient; 
using System.Xml; 
 
namespace DataAccess 
{ 
	/// <summary> 
	/// MSSqlDataAccess  
	/// </summary> 
	public sealed class MSSqlDataAccess : AbstractDataAccess 
	{ 
		#region Constructor 
		public MSSqlDataAccess(SqlConnection conn) 
		{ 
			this.m_DbConnection=conn; 
		} 
 
		public MSSqlDataAccess(string connectionString) 
		{ 
			this.m_DbConnection=new SqlConnection(connectionString); 
		} 
		#endregion 
		 
		#region DataAccess  
 
		#region Support Property & method 
		public override DatabaseType DatabaseType 
		{ 
			get{return DatabaseType.MSSQLServer;} 
		}	 
		private SqlConnection m_DbConnection; 
		public override IDbConnection DbConnection 
		{ 
			get 
			{ 
				return m_DbConnection; 
			} 
		} 
		 
		private SqlTransaction trans=null; 
		public override IDbTransaction BeginTransaction() 
		{ 
			trans=m_DbConnection.BeginTransaction(); 
			return trans; 
		} 
 
		#endregion Support Property & method 
 
		#region ExecuteNonQuery 
		public override int ExecuteNonQuery(CommandType commandType, string commandText, QueryParameterCollection commandParameters) 
		{ 
			SqlCommand cmd=new SqlCommand(); 
			PrepareCommand(cmd,commandType, commandText,commandParameters); 
			int tmpValue=cmd.ExecuteNonQuery(); 
			SyncParameter(commandParameters); 
			cmd.Parameters.Clear(); 
			return tmpValue; 
		} 
		#endregion ExecuteNonQuery 
 
		#region ExecuteDataSet 
 
		public override DataSet ExecuteDataset(CommandType commandType, string commandText, QueryParameterCollection commandParameters,DataSet ds,string tableName) 
		{ 
			SqlCommand cmd=new SqlCommand(); 
			PrepareCommand(cmd,commandType, commandText,commandParameters); 
 
			SqlDataAdapter da=new SqlDataAdapter(cmd); 
			if(Object.Equals(tableName,null) || (tableName.Length<1)) 
				da.Fill(ds); 
			else 
				da.Fill(ds,tableName); 
 
			SyncParameter(commandParameters); 
			cmd.Parameters.Clear(); 
			return ds; 
		} 
 
		#endregion ExecuteDataSet 
 
		#region ExecuteReader	 
		public override IDataReader ExecuteReader(CommandType commandType, string commandText, QueryParameterCollection commandParameters) 
		{ 
			SqlCommand cmd=new SqlCommand(); 
			PrepareCommand(cmd,commandType, commandText,commandParameters); 
			SqlDataReader dr=cmd.ExecuteReader();			 
			SyncParameter(commandParameters); 
			cmd.Parameters.Clear(); 
			return dr; 
		} 
		#endregion ExecuteReader	 
 
		#region ExecuteScalar	 
		public override object ExecuteScalar(CommandType commandType, string commandText, QueryParameterCollection commandParameters) 
		{ 
			SqlCommand cmd=new SqlCommand(); 
			PrepareCommand(cmd,commandType, commandText,commandParameters); 
			object tmpValue=cmd.ExecuteScalar(); 
			SyncParameter(commandParameters); 
			cmd.Parameters.Clear(); 
			return tmpValue; 
		} 
		#endregion ExecuteScalar	 
 
		#region ExecuteXmlReader	 
		public override XmlReader ExecuteXmlReader(CommandType commandType, string commandText, QueryParameterCollection commandParameters) 
		{ 
			SqlCommand cmd=new SqlCommand(); 
			PrepareCommand(cmd,commandType, commandText,commandParameters); 
			XmlReader reader=cmd.ExecuteXmlReader(); 
			SyncParameter(commandParameters); 
			cmd.Parameters.Clear(); 
			return reader; 
		} 
		#endregion ExecuteXmlReader 
 
		#endregion 
 
		private void PrepareCommand(SqlCommand cmd,CommandType commandType, string commandText, QueryParameterCollection commandParameters) 
		{ 
			cmd.CommandType=commandType; 
			cmd.CommandText=commandText; 
			cmd.Connection=this.m_DbConnection; 
			cmd.Transaction=trans; 
			if((commandParameters!=null) && (commandParameters.Count>0) ) 
			{ 
				for(int i=0;i<commandParameters.Count;i++) 
				{ 
					commandParameters[i].InitRealParameter(DatabaseType.MSSQLServer); 
					cmd.Parameters.Add(commandParameters[i].RealParameter as SqlParameter); 
				} 
			} 
		} 
	} 
}