ldj/myLog/Execution.cs

159 lines
3.4 KiB
C#
Raw Permalink Normal View History

2023-05-23 16:13:17 +08:00
using System;
using System.Data;
//using Oracle.DataAccess.Client;
using System.Data.SqlClient;
//using DeiNiu.Data.DataAccess;
namespace DeiNiu
{
internal class Execution : IDisposable
{
private String _Sql;
private SqlConnection _Conn;
private SqlCommand _sqlCmd;
public Execution()
{
_Conn = new Connection().getSqlCon();
_sqlCmd = new SqlCommand();
_sqlCmd.Connection = _Conn;
}
public Execution(String Sql)
{
_Sql = Sql;
_Conn = new Connection().getSqlCon();
_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
{
return _sqlCmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
_Conn.Close();
throw ex;
}
}
public DataSet getData()
{
try
{
<EFBFBD><EFBFBD>
DataSet ds = new DataSet();
using (SqlDataAdapter oda = new SqlDataAdapter(_sqlCmd))
{
oda.Fill(ds);
}
_Conn.Close();
return ds;
}
catch ( Exception er )
{
throw er;
}finally
{
_Conn.Close();
}
}
public int Execute()
{
try
{
<EFBFBD><EFBFBD>
int affectRows = _sqlCmd.ExecuteNonQuery();
_Conn.Close();
return affectRows;
}
catch (Exception ex)
{
_Conn.Close();
throw ex;
}
finally
{
_Conn.Close();
}
}
public int Execute(bool returnPk)
{
if (!returnPk)
return Execute();
try
{
_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;
int autoPk = Convert.ToInt32(_sqlCmd.ExecuteScalar()); //ExecuteNonQuery();
return autoPk;
}
catch (Exception ex)
{
_Conn.Close();
throw ex;
}
finally
{
_Conn.Close();
}
}
public int getNextSeqValue(String SequenceName)
{
String strSql = "SELECT " + SequenceName + ".nextval from dual";
_sqlCmd.CommandText = strSql;
<EFBFBD><EFBFBD>
DataSet ds = getData();
return int.Parse(ds.Tables[0].Rows[0][0].ToString());
}
public void Dispose()
{
}
}
}