一个基础数据访问库
在新的公司一切都非常原始,不知能不能改变.坚持一下看下不能改变实际不行再选其他路:
似乎公司现有员工对持久层不能愿意,为了方便自己开发只好写一个helper(公司用.net1.1,我的框架也无用武之地).虽然是个ado的helper但出发点还把command抽象一次来实现DAL的数据库无关性.
代码直接贴出来了,有需要朋友可以得到一个思路.
似乎公司现有员工对持久层不能愿意,为了方便自己开发只好写一个helper(公司用.net1.1,我的框架也无用武之地).虽然是个ado的helper但出发点还把command抽象一次来实现DAL的数据库无关性.
代码直接贴出来了,有需要朋友可以得到一个思路.
public abstract class ConnBase:IDisposable
{
public ConnBase(string connectionstring)
{
mConnection = CreateConnection(connectionstring);
mConnection.Open();
}
abstract
public IDataParameter CreateParameter(string name,object value)
{
IDataParameter dp = CreateParameter();
dp.ParameterName = NamePrefix+ name;
dp.Value = value;
return dp;
}
public IDataParameter CreateParameter(string name,object value,ParameterDirection pd)
{
IDataParameter dp = CreateParameter(name,value);
dp.Direction = pd;
return dp;
}
public IDbCommand CreateCommand(String sql,CommandType ct, params IDataParameter[] paramters)
{
IDbCommand cmd = CreateCommand(sql,paramters);
cmd.CommandType = ct;
return cmd;
}
public IDbCommand CreateCommand(string sql,params IDataParameter[] paramters)
{
IDbCommand cmd = CreateCommand(sql);
if(paramters != null)
{
foreach(IDataParameter dp in paramters)
{
cmd.Parameters.Add(dp);
}
}
return cmd;
}
private IDbConnection mConnection;
protected IDbConnection Connection
{
get
{
return mConnection;
}
}
private IDbTransaction mTransaction;
protected IDbTransaction Transaction
{
get
{
return mTransaction;
}
}
public void BeginTran()
{
mTransaction = Connection.BeginTransaction();
}
public void Commit()
{
mTransaction.Commit();
mTransaction = null;
}
public int ExecuteNonQuery(Command cmd)
{
return cmd.CreateCommand(this).ExecuteNonQuery();
}
public IDataReader ExecuteReader(Command cmd)
{
return cmd.CreateCommand(this).ExecuteReader();
}
public object ExecuteScalar(Command cmd)
{
return cmd.CreateCommand(this).ExecuteScalar();
}
public DataSet ExecuteDataSet(Command cmd)
{
IDbDataAdapter adapter = CreateAdapter();
adapter.SelectCommand = cmd.CreateCommand(this);
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds;
}
public IDataParameter[] CopyToParameter(IList parameters)
{
Array ap = Array.CreateInstance(typeof(IDataParameter),parameters.Count);
parameters.CopyTo(ap,0);
return (IDataParameter[])ap;
}
IDisposable 成员
}
{
public ConnBase(string connectionstring)
{
mConnection = CreateConnection(connectionstring);
mConnection.Open();
}
abstract
public IDataParameter CreateParameter(string name,object value)
{
IDataParameter dp = CreateParameter();
dp.ParameterName = NamePrefix+ name;
dp.Value = value;
return dp;
}
public IDataParameter CreateParameter(string name,object value,ParameterDirection pd)
{
IDataParameter dp = CreateParameter(name,value);
dp.Direction = pd;
return dp;
}
public IDbCommand CreateCommand(String sql,CommandType ct, params IDataParameter[] paramters)
{
IDbCommand cmd = CreateCommand(sql,paramters);
cmd.CommandType = ct;
return cmd;
}
public IDbCommand CreateCommand(string sql,params IDataParameter[] paramters)
{
IDbCommand cmd = CreateCommand(sql);
if(paramters != null)
{
foreach(IDataParameter dp in paramters)
{
cmd.Parameters.Add(dp);
}
}
return cmd;
}
private IDbConnection mConnection;
protected IDbConnection Connection
{
get
{
return mConnection;
}
}
private IDbTransaction mTransaction;
protected IDbTransaction Transaction
{
get
{
return mTransaction;
}
}
public void BeginTran()
{
mTransaction = Connection.BeginTransaction();
}
public void Commit()
{
mTransaction.Commit();
mTransaction = null;
}
public int ExecuteNonQuery(Command cmd)
{
return cmd.CreateCommand(this).ExecuteNonQuery();
}
public IDataReader ExecuteReader(Command cmd)
{
return cmd.CreateCommand(this).ExecuteReader();
}
public object ExecuteScalar(Command cmd)
{
return cmd.CreateCommand(this).ExecuteScalar();
}
public DataSet ExecuteDataSet(Command cmd)
{
IDbDataAdapter adapter = CreateAdapter();
adapter.SelectCommand = cmd.CreateCommand(this);
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds;
}
public IDataParameter[] CopyToParameter(IList parameters)
{
Array ap = Array.CreateInstance(typeof(IDataParameter),parameters.Count);
parameters.CopyTo(ap,0);
return (IDataParameter[])ap;
}
IDisposable 成员
}
public class Command
{
public Command(string text)
{
Text = text;
}
private string mText;
public string Text
{
get
{
return mText;
}
set
{
mText = value;
}
}
private CommandType mCommandType;
public CommandType CommandType
{
get
{
return mCommandType;
}
set
{
mCommandType = value;
}
}
private ArrayList mParameters = new ArrayList();
public ArrayList Parameters
{
get
{
return mParameters;
}
}
public void AddParameter(string name,string value)
{
AddParameter(name,value,ParameterDirection.Input);
}
public void AddParameter(string name,string value,ParameterDirection pd)
{
Parameter p = new Parameter();
p.Name = name;
p.Value = value;
p.Direction = pd;
Parameters.Add(p);
}
public class Parameter
{
private string mName;
public string Name
{
get
{
return mName;
}
set
{
mName = value;
}
}
private object mValue;
public object Value
{
get
{
return mValue;
}
set
{
mValue = value;
}
}
private ParameterDirection mDirection = ParameterDirection.Input;
public ParameterDirection Direction
{
get
{
return mDirection;
}
set
{
mDirection = value;
}
}
}
public IDbCommand CreateCommand(ConnBase conn)
{
IDbCommand cmd = conn.CreateCommand(Text);
cmd.CommandType = CommandType;
foreach(Parameter p in Parameters)
{
cmd.Parameters.Add(conn.CreateParameter(p.Name,p.Value,p.Direction));
}
return cmd;
}
}
{
public Command(string text)
{
Text = text;
}
private string mText;
public string Text
{
get
{
return mText;
}
set
{
mText = value;
}
}
private CommandType mCommandType;
public CommandType CommandType
{
get
{
return mCommandType;
}
set
{
mCommandType = value;
}
}
private ArrayList mParameters = new ArrayList();
public ArrayList Parameters
{
get
{
return mParameters;
}
}
public void AddParameter(string name,string value)
{
AddParameter(name,value,ParameterDirection.Input);
}
public void AddParameter(string name,string value,ParameterDirection pd)
{
Parameter p = new Parameter();
p.Name = name;
p.Value = value;
p.Direction = pd;
Parameters.Add(p);
}
public class Parameter
{
private string mName;
public string Name
{
get
{
return mName;
}
set
{
mName = value;
}
}
private object mValue;
public object Value
{
get
{
return mValue;
}
set
{
mValue = value;
}
}
private ParameterDirection mDirection = ParameterDirection.Input;
public ParameterDirection Direction
{
get
{
return mDirection;
}
set
{
mDirection = value;
}
}
}
public IDbCommand CreateCommand(ConnBase conn)
{
IDbCommand cmd = conn.CreateCommand(Text);
cmd.CommandType = CommandType;
foreach(Parameter p in Parameters)
{
cmd.Parameters.Add(conn.CreateParameter(p.Name,p.Value,p.Direction));
}
return cmd;
}
}
public class OracleConn:ConnBase
{
public OracleConn(string connstring):base(connstring){}
public override IDbDataAdapter CreateAdapter()
{
return new OracleDataAdapter();
}
public override IDbCommand CreateCommand(string sql)
{
IDbCommand cmd= new OracleCommand(sql);
cmd.Connection = Connection;
if(Transaction != null)
cmd.Transaction = Transaction;
return cmd;
}
protected override IDbConnection CreateConnection(string connectionstring)
{
return new OracleConnection(connectionstring);
}
public override IDataParameter CreateParameter()
{
return new OracleParameter();
}
protected override string NamePrefix
{
get
{
return ":";
}
}
}
public class MSSql:ConnBase
{
public MSSql(string connstring):base(connstring){}
public override IDbDataAdapter CreateAdapter()
{
return new SqlDataAdapter() ;
}
public override IDbCommand CreateCommand(string sql)
{
IDbCommand cmd= new SqlCommand(sql);
cmd.Connection = Connection;
if(Transaction != null)
cmd.Transaction = Transaction;
return cmd;
}
protected override IDbConnection CreateConnection(string connectionstring)
{
return new SqlConnection(connectionstring);
}
public override IDataParameter CreateParameter()
{
return new SqlParameter();
}
protected override string NamePrefix
{
get
{
return "@";
}
}
}
public class Access:ConnBase
{
public Access(string connstring):base(connstring){}
public override IDbDataAdapter CreateAdapter()
{
return new OleDbDataAdapter();
}
public override IDbCommand CreateCommand(string sql)
{
IDbCommand cmd = new OleDbCommand(sql);
cmd.Connection = Connection;
if(Transaction != null)
cmd.Transaction = Transaction;
return cmd;
}
protected override IDbConnection CreateConnection(string connectionstring)
{
return new OleDbConnection(connectionstring);
}
public override IDataParameter CreateParameter()
{
return new OleDbParameter();
}
protected override string NamePrefix
{
get
{
return "@";
}
}
}
{
public OracleConn(string connstring):base(connstring){}
public override IDbDataAdapter CreateAdapter()
{
return new OracleDataAdapter();
}
public override IDbCommand CreateCommand(string sql)
{
IDbCommand cmd= new OracleCommand(sql);
cmd.Connection = Connection;
if(Transaction != null)
cmd.Transaction = Transaction;
return cmd;
}
protected override IDbConnection CreateConnection(string connectionstring)
{
return new OracleConnection(connectionstring);
}
public override IDataParameter CreateParameter()
{
return new OracleParameter();
}
protected override string NamePrefix
{
get
{
return ":";
}
}
}
public class MSSql:ConnBase
{
public MSSql(string connstring):base(connstring){}
public override IDbDataAdapter CreateAdapter()
{
return new SqlDataAdapter() ;
}
public override IDbCommand CreateCommand(string sql)
{
IDbCommand cmd= new SqlCommand(sql);
cmd.Connection = Connection;
if(Transaction != null)
cmd.Transaction = Transaction;
return cmd;
}
protected override IDbConnection CreateConnection(string connectionstring)
{
return new SqlConnection(connectionstring);
}
public override IDataParameter CreateParameter()
{
return new SqlParameter();
}
protected override string NamePrefix
{
get
{
return "@";
}
}
}
public class Access:ConnBase
{
public Access(string connstring):base(connstring){}
public override IDbDataAdapter CreateAdapter()
{
return new OleDbDataAdapter();
}
public override IDbCommand CreateCommand(string sql)
{
IDbCommand cmd = new OleDbCommand(sql);
cmd.Connection = Connection;
if(Transaction != null)
cmd.Transaction = Transaction;
return cmd;
}
protected override IDbConnection CreateConnection(string connectionstring)
{
return new OleDbConnection(connectionstring);
}
public override IDataParameter CreateParameter()
{
return new OleDbParameter();
}
protected override string NamePrefix
{
get
{
return "@";
}
}
}