最后我们来解决点遗留问题
首先是读取Attribute的问题
大致的方法就是这样子。
还有就是如何通过“NChar”等字符串获取System.Data.SqlDBType这个枚举的类型的值
其实也不难,
SqlDbType ptype = (SqlDbType)System.Enum.Parse(typeof(SqlDbType), "VarChar");
这样子就可以了
最后我们实现一个数据库操作组件就可以把上面内容组合起来了,下面贴的代码是我自己写了个很粗糙的例子上的
首先是读取Attribute的问题
Type tp
=
target.GetType();
PropertyInfo pp = tp.GetProperty(s);
foreach ( object o in pp.GetCustomAttributes( false ))
{
if (o.GetType().Equals(typeof(ParamAttribute)))
{
ParamAttribute p = (ParamAttribute)o;
//这里P就是Attribute的对象啦
break;
}
}
PropertyInfo pp = tp.GetProperty(s);
foreach ( object o in pp.GetCustomAttributes( false ))
{
if (o.GetType().Equals(typeof(ParamAttribute)))
{
ParamAttribute p = (ParamAttribute)o;
//这里P就是Attribute的对象啦
break;
}
}
大致的方法就是这样子。
还有就是如何通过“NChar”等字符串获取System.Data.SqlDBType这个枚举的类型的值
其实也不难,
SqlDbType ptype = (SqlDbType)System.Enum.Parse(typeof(SqlDbType), "VarChar");
这样子就可以了
最后我们实现一个数据库操作组件就可以把上面内容组合起来了,下面贴的代码是我自己写了个很粗糙的例子上的
using
System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Reflection;
using Alexander.Xbase.ClassConfig;
using Alexander.Xbase.Xcache;
using Alexander.Xbase.Xresource;
using Alexander.Xbase.Interface;
namespace Alexander.Xbase.SqlExec
{
public class Exec: Alexander.Xbase.Interface.IQueriable
{
private string ConnectionString;
private SqlConnection conn;
private SqlTransaction tran;
private bool TransOpen;
private string configpath;
IQueriable 成员#region IQueriable 成员
public void Init(string connstr,string configbase)
{
ConnectionString = connstr;
configpath = configbase;
conn = new SqlConnection(ConnectionString);
if (conn.State.Equals(ConnectionState.Closed))
{
conn.Open();
}
//throw new Exception("The method or operation is not implemented.");
}
public void OpenTranscation()
{
TransOpen = true;
tran = conn.BeginTransaction();
// throw new Exception("The method or operation is not implemented.");
}
public void Commit()
{
TransOpen = false;
tran.Commit();
tran = null;
//throw new Exception("The method or operation is not implemented.");
}
public void RollBack()
{
TransOpen = false;
tran.Rollback();
tran = null;
//throw new Exception("The method or operation is not implemented.");
}
public object Query4Object(string action, object target)
{
object rs=new object();
ConfigStruct config = new ConfigStruct(new Resource(configpath + target.GetType().Name+".xml").Data);
string sql = config.GetSql(action);
string execsql = MakeExecuteSql(sql);
string storedsql = MakeValueSql(sql,target);
string sKey = target.GetType().Name + ":" + storedsql;
if (config.IsCache(action))
{
if (IsInCache(sKey, rs))
{
return ObjCache.GetObject[sKey];
}
}
SqlCommand comm = new SqlCommand(execsql, conn);
PrepareCommand(comm, execsql, target);
SqlDataReader sr = comm.ExecuteReader();
while (sr.Read())
{
rs=FillObject(sr, target.GetType());
}
sr.Close();
if (config.IsCache(action))
ObjCache.Insert(sKey, rs);
return rs;
//throw new Exception("The method or operation is not implemented.");
}
public void Query4List(string action,ref IList target, object queryparam)
{
object rs=new object();
//SortedList sl = new SortedList();
ConfigStruct config = new ConfigStruct(new Resource(configpath + queryparam.GetType().Name + ".xml").Data);
string sql = config.GetSql(action);
string execsql = MakeExecuteSql(sql);
string storedsql = MakeValueSql(sql,queryparam);
string sKey = queryparam.GetType().Name + ":" + storedsql;
if (config.IsCache(action))
{
if (IsInCache(sKey, rs))
{
target = (IList)ObjCache.GetObject[sKey];
return;
}
}
SqlCommand comm = new SqlCommand(execsql, conn);
PrepareCommand(comm, sql, queryparam);
SqlDataReader sr = comm.ExecuteReader();
int i=0;
while (sr.Read())
{
rs=FillObject(sr, queryparam.GetType());
target.Add(rs);
}
sr.Close();
if (config.IsCache(action))
{
ObjCache.Insert(sKey, target);
}
//return sl;
//throw new Exception("The method or operation is not implemented.");
}
public int Query4Page(string action,ref IList target, object queryparam, int pageid, int pagesize,string sort,string direct)
{
object rs = new object();
ConfigStruct config = new ConfigStruct(new Resource(configpath + queryparam.GetType().Name+".xml").Data);
string sql = config.GetSql(action);
string pagesql = DoPageSql(sql, pageid, pagesize, sort, direct);
string execsql = MakeExecuteSql(pagesql);
string storedsql = MakeValueSql(pagesql, queryparam);
string sKey = queryparam.GetType().Name + ":" + storedsql;
int count = (int)Count(action,DoCountSql(MakeExecuteSql(sql)), queryparam);
if (config.IsCache(action))
{
if (IsInCache(sKey, rs))
{
target = (IList)ObjCache.GetObject[sKey];
return count;
}
}
SqlCommand comm = new SqlCommand(execsql, conn);
PrepareCommand(comm, sql, queryparam);
SqlDataReader sr = comm.ExecuteReader();
int i = 0;
while (sr.Read())
{
rs=FillObject(sr, queryparam.GetType());
target.Add(rs);
}
sr.Close();
if (config.IsCache(action))
{
ObjCache.Insert(sKey, target);
}
return count;
//throw new Exception("The method or operation is not implemented.");
}
public int Query4Update(string action, object target)
{
object rs = new object();
ConfigStruct config = new ConfigStruct(new Resource(configpath + target.GetType().Name + ".xml").Data);
string sql = config.GetSql(action);
string execsql = MakeExecuteSql(sql);
string storedsql = MakeValueSql(sql, target);
string sKey = target.GetType().Name + ":" + storedsql;
SqlCommand comm = new SqlCommand(execsql, conn);
PrepareCommand(comm, sql, target);
int ct=comm.ExecuteNonQuery();
ObjCache.Flash(target.GetType().Name);
return ct;
//throw new Exception("The method or operation is not implemented.");
}
public object Query4Count(string action, object target)
{
ConfigStruct config = new ConfigStruct(new Resource(configpath + target.GetType().Name + ".xml").Data);
string sql = config.GetSql(action);
return Count(action,sql, target);
}
public void Close()
{
conn.Close();
//throw new Exception("The method or operation is not implemented.");
}
#endregion
private string DoPageSql(String command, int pageid, int pagesize, string sor
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Reflection;
using Alexander.Xbase.ClassConfig;
using Alexander.Xbase.Xcache;
using Alexander.Xbase.Xresource;
using Alexander.Xbase.Interface;
namespace Alexander.Xbase.SqlExec
{
public class Exec: Alexander.Xbase.Interface.IQueriable
{
private string ConnectionString;
private SqlConnection conn;
private SqlTransaction tran;
private bool TransOpen;
private string configpath;
IQueriable 成员#region IQueriable 成员
public void Init(string connstr,string configbase)
{
ConnectionString = connstr;
configpath = configbase;
conn = new SqlConnection(ConnectionString);
if (conn.State.Equals(ConnectionState.Closed))
{
conn.Open();
}
//throw new Exception("The method or operation is not implemented.");
}
public void OpenTranscation()
{
TransOpen = true;
tran = conn.BeginTransaction();
// throw new Exception("The method or operation is not implemented.");
}
public void Commit()
{
TransOpen = false;
tran.Commit();
tran = null;
//throw new Exception("The method or operation is not implemented.");
}
public void RollBack()
{
TransOpen = false;
tran.Rollback();
tran = null;
//throw new Exception("The method or operation is not implemented.");
}
public object Query4Object(string action, object target)
{
object rs=new object();
ConfigStruct config = new ConfigStruct(new Resource(configpath + target.GetType().Name+".xml").Data);
string sql = config.GetSql(action);
string execsql = MakeExecuteSql(sql);
string storedsql = MakeValueSql(sql,target);
string sKey = target.GetType().Name + ":" + storedsql;
if (config.IsCache(action))
{
if (IsInCache(sKey, rs))
{
return ObjCache.GetObject[sKey];
}
}
SqlCommand comm = new SqlCommand(execsql, conn);
PrepareCommand(comm, execsql, target);
SqlDataReader sr = comm.ExecuteReader();
while (sr.Read())
{
rs=FillObject(sr, target.GetType());
}
sr.Close();
if (config.IsCache(action))
ObjCache.Insert(sKey, rs);
return rs;
//throw new Exception("The method or operation is not implemented.");
}
public void Query4List(string action,ref IList target, object queryparam)
{
object rs=new object();
//SortedList sl = new SortedList();
ConfigStruct config = new ConfigStruct(new Resource(configpath + queryparam.GetType().Name + ".xml").Data);
string sql = config.GetSql(action);
string execsql = MakeExecuteSql(sql);
string storedsql = MakeValueSql(sql,queryparam);
string sKey = queryparam.GetType().Name + ":" + storedsql;
if (config.IsCache(action))
{
if (IsInCache(sKey, rs))
{
target = (IList)ObjCache.GetObject[sKey];
return;
}
}
SqlCommand comm = new SqlCommand(execsql, conn);
PrepareCommand(comm, sql, queryparam);
SqlDataReader sr = comm.ExecuteReader();
int i=0;
while (sr.Read())
{
rs=FillObject(sr, queryparam.GetType());
target.Add(rs);
}
sr.Close();
if (config.IsCache(action))
{
ObjCache.Insert(sKey, target);
}
//return sl;
//throw new Exception("The method or operation is not implemented.");
}
public int Query4Page(string action,ref IList target, object queryparam, int pageid, int pagesize,string sort,string direct)
{
object rs = new object();
ConfigStruct config = new ConfigStruct(new Resource(configpath + queryparam.GetType().Name+".xml").Data);
string sql = config.GetSql(action);
string pagesql = DoPageSql(sql, pageid, pagesize, sort, direct);
string execsql = MakeExecuteSql(pagesql);
string storedsql = MakeValueSql(pagesql, queryparam);
string sKey = queryparam.GetType().Name + ":" + storedsql;
int count = (int)Count(action,DoCountSql(MakeExecuteSql(sql)), queryparam);
if (config.IsCache(action))
{
if (IsInCache(sKey, rs))
{
target = (IList)ObjCache.GetObject[sKey];
return count;
}
}
SqlCommand comm = new SqlCommand(execsql, conn);
PrepareCommand(comm, sql, queryparam);
SqlDataReader sr = comm.ExecuteReader();
int i = 0;
while (sr.Read())
{
rs=FillObject(sr, queryparam.GetType());
target.Add(rs);
}
sr.Close();
if (config.IsCache(action))
{
ObjCache.Insert(sKey, target);
}
return count;
//throw new Exception("The method or operation is not implemented.");
}
public int Query4Update(string action, object target)
{
object rs = new object();
ConfigStruct config = new ConfigStruct(new Resource(configpath + target.GetType().Name + ".xml").Data);
string sql = config.GetSql(action);
string execsql = MakeExecuteSql(sql);
string storedsql = MakeValueSql(sql, target);
string sKey = target.GetType().Name + ":" + storedsql;
SqlCommand comm = new SqlCommand(execsql, conn);
PrepareCommand(comm, sql, target);
int ct=comm.ExecuteNonQuery();
ObjCache.Flash(target.GetType().Name);
return ct;
//throw new Exception("The method or operation is not implemented.");
}
public object Query4Count(string action, object target)
{
ConfigStruct config = new ConfigStruct(new Resource(configpath + target.GetType().Name + ".xml").Data);
string sql = config.GetSql(action);
return Count(action,sql, target);
}
public void Close()
{
conn.Close();
//throw new Exception("The method or operation is not implemented.");
}
#endregion
private string DoPageSql(String command, int pageid, int pagesize, string sor