361 lines
9.9 KiB
C#
361 lines
9.9 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Text;
|
||
//using System.Data.OracleClient;
|
||
using System.Data.SqlClient;
|
||
using System.Data;
|
||
using System.Data.Common;
|
||
using DeiNiu.Utils;
|
||
|
||
using System.Data.OleDb;
|
||
|
||
namespace DeiNiu.Data.BaseObject
|
||
{
|
||
[Serializable]
|
||
public class BaseModel_Imp : BaseOperModel_Imp
|
||
{
|
||
internal string connString = "";
|
||
internal int operid = 0;
|
||
protected String _strSql = "";
|
||
protected int _op_flag;
|
||
protected int _cust_op_flag;
|
||
protected BaseModel modelObj;
|
||
protected int _rownumStart;
|
||
protected int _rownumEnd=100;
|
||
protected int _totalCount;
|
||
internal SqlConnection _Conn = null;
|
||
protected internal int Add(BaseModel obj)
|
||
{
|
||
modelObj = obj;
|
||
_op_flag = (int)op_flag.add;
|
||
return ExecuteNoneQuery();
|
||
|
||
}
|
||
protected internal int Update(BaseModel obj)
|
||
{
|
||
modelObj = obj;
|
||
_op_flag = (int)op_flag.update;
|
||
return ExecuteNoneQuery();
|
||
}
|
||
protected internal int UpdateNoCompare(BaseModel obj)
|
||
{
|
||
modelObj = obj;
|
||
_op_flag = (int)op_flag.updateAll;
|
||
return ExecuteNoneQuery();
|
||
}
|
||
protected internal int Delete(BaseModel obj)
|
||
{
|
||
modelObj = obj;
|
||
_op_flag = (int)op_flag.delete;
|
||
return ExecuteNoneQuery();
|
||
}
|
||
protected internal DataSet Query(BaseModel exampleObj)
|
||
{
|
||
|
||
modelObj = exampleObj;
|
||
_op_flag = (int)op_flag.queryExample;
|
||
return ExecuteDsQuery();
|
||
|
||
|
||
}
|
||
protected internal DataSet QueryActived()
|
||
{
|
||
_op_flag = (int)op_flag.queryActived;
|
||
return ExecuteDsQuery();
|
||
}
|
||
public DataSet QueryCount()
|
||
{
|
||
_op_flag = (int)op_flag.getCount;
|
||
return ExecuteDsQuery();
|
||
|
||
}
|
||
protected internal DataSet Query()
|
||
{
|
||
|
||
_op_flag = (int)op_flag.queryAll;
|
||
return ExecuteDsQuery();
|
||
|
||
}
|
||
protected internal DataSet CustQuery(String strSql)
|
||
{
|
||
_strSql = strSql;
|
||
return new Execution(_strSql).getData();
|
||
}
|
||
//protected internal DataSet CustQuery(String strSql, SqlConnection conn)
|
||
//{
|
||
// _strSql = strSql;
|
||
// return new Execution(_strSql, conn).getData();
|
||
//}
|
||
|
||
protected internal DataSet CustQuery(BaseModel exampleObj)
|
||
{
|
||
modelObj = exampleObj;
|
||
_cust_op_flag = exampleObj.custOperFlag;
|
||
return ExecuteDsQuery();
|
||
}
|
||
|
||
protected internal int CustOper(BaseModel exampleObj)
|
||
{
|
||
modelObj = exampleObj;
|
||
_cust_op_flag = exampleObj.custOperFlag;
|
||
return ExecuteNoneQuery();
|
||
}
|
||
|
||
//it must not be overrided
|
||
internal void getModel(BaseModel obj)
|
||
{
|
||
modelObj = obj;
|
||
if (obj.custOperFlag != 0) // customzation method
|
||
{
|
||
_cust_op_flag = obj.custOperFlag;
|
||
}
|
||
else
|
||
{
|
||
_op_flag = (int)op_flag.getObj; //to get the object by key word
|
||
}
|
||
|
||
DataTable dt = ExecuteDsQuery().Tables[0];
|
||
foreach (DataRow dr in dt.Rows)
|
||
{
|
||
getModel(obj, dr);
|
||
break;
|
||
}
|
||
// Assem_Model(ExecuteRdQuery(), obj);
|
||
}
|
||
internal void getModel(BaseModel obj,DataRow dr)
|
||
{
|
||
datarowToModel(dr, obj);
|
||
}
|
||
|
||
//to set object's attribute from ds.
|
||
//it must be overrided
|
||
protected virtual void Assem_Model(SqlDataReader reader, BaseModel obj)
|
||
{
|
||
return;
|
||
}
|
||
protected virtual void datarowToModel(DataRow dr, BaseModel obj) { }
|
||
/*
|
||
protected virtual void CmdPrepare( OleDbCommand SqlCmd)
|
||
{
|
||
//SqlCmd.CommandText = "insert into a values(@a)";
|
||
// SqlCmd.Parameters.Add("@a","a");
|
||
return;
|
||
}
|
||
*/
|
||
protected virtual void CmdPrepare(SqlCommand sqlCmd)
|
||
{
|
||
//sqlCmd.CommandText = "insert into a values(@a)";
|
||
// sqlCmd.Parameters.Add("@a","a");
|
||
return;
|
||
}
|
||
|
||
|
||
/*
|
||
private int ExecuteNoneQuery()
|
||
{
|
||
using (oledbExecution Exe = new oledbExecution())
|
||
{
|
||
CmdPrepare(Exe.OleCmd);
|
||
this._cust_op_flag = 0;
|
||
this._op_flag = 0;
|
||
return Exe.Execute();
|
||
}
|
||
|
||
}
|
||
|
||
private DataSet ExecuteDsQuery()
|
||
{
|
||
using (oledbExecution Exe = new oledbExecution())
|
||
{
|
||
CmdPrepare(Exe.OleCmd);
|
||
this._cust_op_flag = 0;
|
||
this._op_flag = 0;
|
||
return Exe.getData();
|
||
}
|
||
}
|
||
protected int getSequence(String SequenceName)
|
||
{
|
||
|
||
using (oledbExecution Exe = new oledbExecution())
|
||
{
|
||
return Exe.getNextSeqValue(SequenceName);
|
||
}
|
||
}
|
||
|
||
|
||
* */
|
||
private int ExecuteNoneQuery()
|
||
{
|
||
using (Execution Exe = new Execution(instance))
|
||
{
|
||
CmdPrepare(Exe.SqlCmd);
|
||
|
||
if (Exe.SqlCmd.CommandText.Length == 0) return 0;
|
||
int returnValue = 0;
|
||
if (_op_flag == (int)op_flag.add) //insert, to get pk.
|
||
{
|
||
modelObj.custOperFlag = 0;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƻ<EFBFBD><C6BB><EFBFBD>ѯ<EFBFBD><D1AF>־
|
||
|
||
modelObj.ID = Exe.Execute(true);
|
||
returnValue = modelObj.ID;
|
||
/*
|
||
Exe.Execute();
|
||
modelObj.ID=getPk();
|
||
returnValue = modelObj.ID;
|
||
*/
|
||
// getModel(modelObj);
|
||
}
|
||
else //update
|
||
{
|
||
returnValue = Exe.Execute();
|
||
}
|
||
_cust_op_flag = 0;
|
||
_op_flag = 0;
|
||
|
||
return returnValue;
|
||
|
||
|
||
}
|
||
|
||
}
|
||
|
||
private SqlDataReader ExecuteRdQuery()
|
||
{
|
||
using (Execution Exe = new Execution(instance))
|
||
{
|
||
CmdPrepare(Exe.SqlCmd);
|
||
_cust_op_flag = 0;
|
||
_op_flag = 0;
|
||
return Exe.GetDataReader();
|
||
}
|
||
}
|
||
private DataSet ExecuteDsQuery()
|
||
{
|
||
using (Execution Exe = new Execution(instance))
|
||
{
|
||
CmdPrepare(Exe.SqlCmd);
|
||
this._cust_op_flag = 0;
|
||
this._op_flag = 0;
|
||
if (Exe.SqlCmd.CommandText.Length == 0)
|
||
{
|
||
DataSet ds = new DataSet();
|
||
ds.Tables.Add(new DataTable());
|
||
return ds;
|
||
}
|
||
return Exe.getData();
|
||
|
||
|
||
}
|
||
}
|
||
internal int getSequence(String SequenceName)
|
||
{
|
||
|
||
using (Execution Exe = new Execution(instance))
|
||
{
|
||
int seq = 0;
|
||
try
|
||
{
|
||
seq = Exe.getNextSeqValue(SequenceName);
|
||
}catch(SqlException er)
|
||
{
|
||
createSeq(SequenceName);
|
||
seq = Exe.getNextSeqValue(SequenceName);
|
||
}
|
||
return seq ;
|
||
}
|
||
}
|
||
|
||
private void createSeq(string sequenceName)
|
||
{
|
||
_strSql = " CREATE SEQUENCE[dbo].[" + sequenceName + "] AS[bigint] START WITH 0 INCREMENT BY 1 MINVALUE 0 MAXVALUE 100000 CYCLE CACHE";
|
||
using (Execution Exe = new Execution(instance))
|
||
{
|
||
Exe.SqlCmd.CommandText= _strSql;
|
||
Exe.Execute();
|
||
}
|
||
}
|
||
// protected int getPk(SqlConnection conn)
|
||
protected int getPk()
|
||
{
|
||
|
||
_op_flag = (int)op_flag.getPk;
|
||
string tmp = ExecuteDsQuery().Tables[0].Rows[0][0].ToString();
|
||
return tmp != string.Empty
|
||
? Convert.ToInt32(tmp)
|
||
: 0;
|
||
/*
|
||
|
||
string tmp = CustQuery("select Scope_Identity()",conn).Tables[0].Rows[0][0].ToString();
|
||
return tmp != string.Empty
|
||
? Convert.ToInt32(tmp)
|
||
: 0;
|
||
*/
|
||
|
||
}
|
||
|
||
internal string getDateTime()
|
||
{
|
||
using (Execution Exe = new Execution(instance))
|
||
{
|
||
return Exe.getDateTime();
|
||
}
|
||
|
||
|
||
|
||
}
|
||
|
||
protected object VerifyDbnull(object obj)
|
||
{
|
||
if (obj is string)
|
||
{
|
||
return (string)obj == string.Empty ? DBNull.Value : obj;
|
||
}
|
||
return obj==null? DBNull.Value : obj;
|
||
}
|
||
|
||
|
||
protected internal int rownumStart
|
||
{
|
||
get
|
||
{
|
||
|
||
return _rownumStart;
|
||
}
|
||
set
|
||
{
|
||
_rownumStart = value;
|
||
if (_rownumStart < 0)
|
||
{
|
||
_rownumStart = 0;
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
protected internal int rownumEnd
|
||
{
|
||
get
|
||
{
|
||
return _rownumEnd;
|
||
}
|
||
set
|
||
{
|
||
_rownumEnd = value;
|
||
if (_rownumEnd <= 0)
|
||
{
|
||
_rownumEnd = WmsConstants.PAGER_SIZE;
|
||
}
|
||
}
|
||
}
|
||
|
||
// protected enumDbInstance _di = enumDbInstance.platForm;
|
||
|
||
protected virtual enumDbInstance instance
|
||
{
|
||
get
|
||
{
|
||
return enumDbInstance.platForm;
|
||
}
|
||
}
|
||
}
|
||
} |