ldj/Data/BaseObject/Execution.cs

263 lines
7.1 KiB
C#
Raw Normal View History

2023-05-23 16:13:17 +08:00
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.
public Execution()
{
// _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);
// _sqlCmd = new SqlCommand(_Sql, _Conn);
}
/*
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())
{
_sqlCmd.Connection = conn;
return _sqlCmd.ExecuteReader(CommandBehavior.CloseConnection);
}
}
catch (Exception ex)
{
closeConnection();
throw ex;
}
}
public DataSet getData()
{
try
{
//HttpContext.Current.Session["currentSql"] = _sqlCmd.CommandText;
logSql();
DataSet ds = new DataSet();
using (SqlConnection conn = new Connection().getSqlCon())
{
_sqlCmd.Connection = conn;
using (SqlDataAdapter oda = new SqlDataAdapter(_sqlCmd))
{
oda.Fill(ds);
}
}
closeConnection();
return ds;
}
catch (Exception er)
{
throw er;
}
finally
{
closeConnection();
}
}
public int Execute()
{
try
{
//HttpContext.Current.Session["currentSql"] = _sqlCmd.CommandText;
logSql();
int affectRows = 0;
using (SqlConnection conn = new Connection().getSqlCon())
{
_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())
{
_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())
{
_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())
{
_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();
}
}
private void closeConnection()
{
//if (_Conn != null && !isBatchExcute)
//{
// _Conn.Close();
//}
}
private void logSql()
{
if (isSqlLog)
{
new DeiNiu.RequestLog().RecordLog(_sqlCmd);
}
}
}
}