279 lines
		
	
	
		
			7.7 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			279 lines
		
	
	
		
			7.7 KiB
		
	
	
	
		
			C#
		
	
	
	
| using System;
 | |
| using System.Collections.Generic;
 | |
| using System.Text;
 | |
| using System.Data;
 | |
| using System.Data.OleDb;
 | |
| //using Oracle.DataAccess.Client;
 | |
| using System.Data.SqlClient;
 | |
| using System.Web;
 | |
| using DeiNiu.Utils;
 | |
| using DeiNiu.Data.DataAccess;
 | |
| namespace DeiNiu.Data.BaseObject
 | |
| {
 | |
|     [Serializable]
 | |
|     public class Execution : IDisposable
 | |
|     {
 | |
|          
 | |
| 
 | |
|         private String _Sql = null;
 | |
|        // private SqlConnection _Conn = null;
 | |
|         private SqlCommand _sqlCmd = null; 
 | |
|         static bool isSqlLog = false; // System.Configuration.ConfigurationManager.AppSettings["SqlLog"].Equals("On");
 | |
|         private bool isBatchExcute; // true: not to handle connection close.
 | |
|         private enumDbInstance instance;
 | |
|         public Execution(enumDbInstance instance)
 | |
|         {
 | |
|             this.instance = instance;
 | |
|           //  _Conn = new Connection().getSqlCon();
 | |
|             _sqlCmd = new SqlCommand();
 | |
|           //  _sqlCmd.Connection = _Conn;
 | |
|             
 | |
|         }
 | |
| 
 | |
|         public Execution(SqlConnection conn)
 | |
|         {
 | |
|          //   _Conn = conn == null ? new Connection().getSqlCon() : conn;
 | |
|             _sqlCmd = new SqlCommand();
 | |
|         //    _sqlCmd.Connection = _Conn;
 | |
|              
 | |
|             isBatchExcute = conn != null;
 | |
|         }
 | |
|       
 | |
|         public Execution(String Sql)
 | |
|         {
 | |
|             _Sql = Sql;
 | |
|          //   _Conn = new Connection().getSqlCon();
 | |
|             _sqlCmd = new SqlCommand(_Sql);
 | |
|            
 | |
| 
 | |
|         }
 | |
|           /*
 | |
|         public Execution(String Sql, SqlConnection Conn)
 | |
|         {
 | |
|             _Sql = Sql;
 | |
|           //  _Conn = Conn;
 | |
|             _sqlCmd = new SqlCommand(_Sql, _Conn);
 | |
|             
 | |
|         }
 | |
|         */
 | |
| 
 | |
|         public SqlCommand SqlCmd
 | |
|         {
 | |
|             get
 | |
|             {
 | |
|                 return _sqlCmd;
 | |
|             }
 | |
| 
 | |
|         }
 | |
| 
 | |
|         public SqlDataReader GetDataReader()
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 //HttpContext.Current.Session["currentSql"] = _sqlCmd.CommandText;
 | |
|                 logSql();
 | |
|                 using (SqlConnection conn = new Connection().getSqlCon(instance))
 | |
|                 {
 | |
|                     _sqlCmd.Connection = conn;
 | |
|                     return _sqlCmd.ExecuteReader(CommandBehavior.CloseConnection);
 | |
|                 }
 | |
| 
 | |
| 
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 closeConnection();
 | |
|                 throw ex;
 | |
|             }
 | |
| 
 | |
| 
 | |
| 
 | |
|         }
 | |
|         public DataSet getData()
 | |
|         {  
 | |
|             
 | |
|             DataSet ds = new DataSet();
 | |
|            
 | |
|             try
 | |
|             {
 | |
|                 //HttpContext.Current.Session["currentSql"] = _sqlCmd.CommandText;
 | |
|                 logSql();
 | |
| 
 | |
|              
 | |
|                 using (SqlConnection conn = new Connection().getSqlCon(instance))
 | |
|                 {
 | |
|                     _sqlCmd.Connection = conn;
 | |
|                     using (SqlDataAdapter oda = new SqlDataAdapter(_sqlCmd))
 | |
|                     {
 | |
|                         oda.Fill(ds);
 | |
| 
 | |
|                     }
 | |
|                 } 
 | |
|                
 | |
|                
 | |
|                 return ds;
 | |
|             }
 | |
|             catch (Exception er)
 | |
|             {
 | |
|                 LogHelper.WriteLog(typeof(Execution), er);
 | |
| 
 | |
|                  throw er;
 | |
|                // DataTable dt = new DataTable();
 | |
|                // ds.Tables.Add(dt);
 | |
| 
 | |
|             }
 | |
|             finally
 | |
|             {
 | |
|                 closeConnection();
 | |
|             }
 | |
| 
 | |
|           //  return new DataSet();
 | |
|         }
 | |
|         public int Execute()
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 //HttpContext.Current.Session["currentSql"] = _sqlCmd.CommandText;
 | |
|                 logSql();
 | |
|                 int affectRows = 0;
 | |
|                 using (SqlConnection conn = new Connection().getSqlCon(instance))
 | |
|                 {
 | |
|                     _sqlCmd.Connection = conn;
 | |
|                     affectRows = _sqlCmd.ExecuteNonQuery();
 | |
|                 }
 | |
|                  
 | |
|                 //       _Conn.Close();
 | |
|                 return affectRows;
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 closeConnection();
 | |
|                 throw ex;
 | |
|             }
 | |
|             finally
 | |
|             {
 | |
|                 closeConnection();
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public int Execute(bool returnPk)
 | |
|         {
 | |
|             logSql();
 | |
|             if (!returnPk)// || isBatchExcute)
 | |
|                 return Execute();
 | |
|             try
 | |
|             {
 | |
|                 //int rt = Execute();
 | |
|                 int autoPk;
 | |
|                 using (SqlConnection conn = new Connection().getSqlCon(instance))
 | |
|                 {
 | |
|                     _sqlCmd.Connection = conn;
 | |
|                     _sqlCmd.CommandText += "; select scope_identity() as [scope_identity]";   //   scope_identity need to be execute in the same range scope.// set   nocount   on;
 | |
|                     //HttpContext.Current.Session["currentSql"] = _sqlCmd.CommandText;
 | |
| 
 | |
|                     autoPk = Convert.ToInt32(_sqlCmd.ExecuteScalar()); //ExecuteNonQuery();
 | |
| 
 | |
|                 }
 | |
|  
 | |
|                 return autoPk;
 | |
| 
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 closeConnection();
 | |
|                 throw ex;
 | |
|             }
 | |
|             finally
 | |
|             {
 | |
|                 closeConnection();
 | |
|             }
 | |
| 
 | |
|         }
 | |
|         /*  this is obsolete for it won't return expected auto increasement value. the return value is always 0.
 | |
|         int getMssqlIncreamentIntPk()
 | |
|         {
 | |
|         	  String strSql = "select Scope_Identity()";
 | |
|             return getInt(strSql);
 | |
|         }
 | |
|         */
 | |
|         int getInt(String strSql)
 | |
|         {
 | |
|             using (SqlConnection conn = new Connection().getSqlCon(instance))
 | |
|             {
 | |
|                 _sqlCmd.Connection = conn;
 | |
|                 _sqlCmd.CommandText = strSql;
 | |
|                 //HttpContext.Current.Session["currentSql"] = _sqlCmd.CommandText;
 | |
|                 return Convert.ToInt32(_sqlCmd.ExecuteScalar());
 | |
|                 //   return 1;
 | |
|             }
 | |
|         }
 | |
|         string getString(string sql)
 | |
|         {
 | |
|             using (SqlConnection conn = new Connection().getSqlCon(instance))
 | |
|             {
 | |
|                 _sqlCmd.Connection = conn;
 | |
|                 _sqlCmd.CommandText = sql;
 | |
|                 //HttpContext.Current.Session["currentSql"] = _sqlCmd.CommandText;
 | |
|                 return _sqlCmd.ExecuteScalar().ToString();
 | |
|             }
 | |
|         }
 | |
|         public int getNextSeqValue(String SequenceName)
 | |
|         {
 | |
|             String strSql = "SELECT NEXT VALUE FOR " + SequenceName; //TableSequenceID ";// "SELECT " + SequenceName + ".nextval from dual";
 | |
|             return getInt(strSql);
 | |
| 
 | |
|         }
 | |
| 
 | |
|         public string getDateTime()
 | |
|         {
 | |
|             String strSql = "SELECT getdate() "; //TableSequenceID ";// "SELECT " + SequenceName + ".nextval from dual";
 | |
|             return getString(strSql);
 | |
| 
 | |
|         }
 | |
| 
 | |
|         public void Dispose()
 | |
|         {
 | |
| 
 | |
|         }
 | |
|         public SqlConnection getConn
 | |
|         {
 | |
|             get
 | |
|             {
 | |
|                 //if (_Conn == null)
 | |
|                 //{
 | |
|                 //    _Conn = new Connection().getSqlCon();
 | |
|                 //}
 | |
| 
 | |
|                 //return _Conn;
 | |
|                 return new Connection().getSqlCon(instance);
 | |
|             }
 | |
| 
 | |
|         }
 | |
|          
 | |
|         private void closeConnection()
 | |
|         {
 | |
|             //if (_Conn != null && !isBatchExcute)
 | |
|             //{ 
 | |
|             //    _Conn.Close();
 | |
|             //}
 | |
|         }
 | |
| 
 | |
|         private void logSql()
 | |
|         {
 | |
|             if (!pickConstants.LOG_SQL_ON)
 | |
|             {
 | |
|                  return;
 | |
|                //new DeiNiu.RequestLog().RecordLog(_sqlCmd);
 | |
|             }
 | |
|             LogHelper.debug(typeof(Execution), _sqlCmd.CommandText);
 | |
|             string parm = "";
 | |
|             foreach (SqlParameter sp in _sqlCmd.Parameters)
 | |
|             {
 | |
|                 parm += "; " + sp.ParameterName + " : " + sp.Value;
 | |
|             }
 | |
|             LogHelper.debug(typeof(Execution), parm);
 | |
| 
 | |
|         }
 | |
|     }
 | |
| 
 | |
| } |