手把手教你写ORM(八)

最后我们来解决点遗留问题
首先是读取Attribute的问题
None.gif Type tp  =  target.GetType();
None.gifPropertyInfo pp 
=  tp.GetProperty(s);
None.gif
foreach  ( object  o  in  pp.GetCustomAttributes( false ))
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if (o.GetType().Equals(typeof(ParamAttribute)))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        ParamAttribute p 
= (ParamAttribute)o;
InBlock.gif        
//这里P就是Attribute的对象啦
InBlock.gif
        break;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

大致的方法就是这样子。

还有就是如何通过“NChar”等字符串获取System.Data.SqlDBType这个枚举的类型的值
其实也不难,
SqlDbType ptype = (SqlDbType)System.Enum.Parse(typeof(SqlDbType), "VarChar");
这样子就可以了

最后我们实现一个数据库操作组件就可以把上面内容组合起来了,下面贴的代码是我自己写了个很粗糙的例子上的
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.Data;
None.gif
using  System.Data.SqlClient;
None.gif
using  System.Collections;
None.gif
using  System.Reflection;
None.gif
using  Alexander.Xbase.ClassConfig;
None.gif
using  Alexander.Xbase.Xcache;
None.gif
using  Alexander.Xbase.Xresource;
None.gif
using  Alexander.Xbase.Interface;
None.gif
None.gif
namespace  Alexander.Xbase.SqlExec
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class Exec: Alexander.Xbase.Interface.IQueriable
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
private string ConnectionString;
InBlock.gif        
private SqlConnection conn;
InBlock.gif        
private SqlTransaction tran;
InBlock.gif        
private bool TransOpen;
InBlock.gif        
private string configpath;
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IQueriable 成员#region IQueriable 成员
InBlock.gif
InBlock.gif        
public void Init(string connstr,string configbase)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ConnectionString 
= connstr;
InBlock.gif            configpath 
= configbase;
InBlock.gif            conn 
= new SqlConnection(ConnectionString);
InBlock.gif            
if (conn.State.Equals(ConnectionState.Closed))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                conn.Open();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
//throw new Exception("The method or operation is not implemented.");
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
public void OpenTranscation()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            TransOpen 
= true;
InBlock.gif            tran 
= conn.BeginTransaction();
InBlock.gif           
// throw new Exception("The method or operation is not implemented.");
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
public void Commit()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            TransOpen 
= false;
InBlock.gif            tran.Commit();
InBlock.gif            tran 
= null;
InBlock.gif            
//throw new Exception("The method or operation is not implemented.");
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
public void RollBack()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            TransOpen 
= false;
InBlock.gif            tran.Rollback();
InBlock.gif            tran 
= null;
InBlock.gif            
//throw new Exception("The method or operation is not implemented.");
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
public object Query4Object(string action, object target)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object rs=new object();
InBlock.gif            ConfigStruct config 
= new ConfigStruct(new Resource(configpath + target.GetType().Name+".xml").Data);
InBlock.gif            
string sql = config.GetSql(action);
InBlock.gif            
string execsql = MakeExecuteSql(sql);
InBlock.gif            
string storedsql = MakeValueSql(sql,target);
InBlock.gif            
string sKey = target.GetType().Name + ":" + storedsql;
InBlock.gif            
if (config.IsCache(action))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (IsInCache(sKey, rs))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return ObjCache.GetObject[sKey];
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            SqlCommand comm 
= new SqlCommand(execsql, conn);
InBlock.gif            PrepareCommand(comm, execsql, target);
InBlock.gif            SqlDataReader sr 
= comm.ExecuteReader();
InBlock.gif            
while (sr.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                rs
=FillObject(sr, target.GetType());
ExpandedSubBlockEnd.gif            }

InBlock.gif            sr.Close();
InBlock.gif            
if (config.IsCache(action))
InBlock.gif                ObjCache.Insert(sKey, rs);
InBlock.gif            
return rs;
InBlock.gif            
//throw new Exception("The method or operation is not implemented.");
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
public void Query4List(string action,ref IList target, object queryparam)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object rs=new object();
InBlock.gif            
//SortedList sl = new SortedList();
InBlock.gif
            ConfigStruct config = new ConfigStruct(new Resource(configpath + queryparam.GetType().Name + ".xml").Data);
InBlock.gif            
string sql = config.GetSql(action);
InBlock.gif            
string execsql = MakeExecuteSql(sql);
InBlock.gif            
string storedsql = MakeValueSql(sql,queryparam);
InBlock.gif            
string sKey = queryparam.GetType().Name + ":" + storedsql;
InBlock.gif            
if (config.IsCache(action))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (IsInCache(sKey, rs))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    target 
= (IList)ObjCache.GetObject[sKey];
InBlock.gif                    
return;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            SqlCommand comm 
= new SqlCommand(execsql, conn);
InBlock.gif            PrepareCommand(comm, sql, queryparam);
InBlock.gif           
InBlock.gif            SqlDataReader sr 
= comm.ExecuteReader();
InBlock.gif            
int i=0;
InBlock.gif            
while (sr.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                rs
=FillObject(sr, queryparam.GetType());
InBlock.gif                target.Add(rs);
ExpandedSubBlockEnd.gif            }

InBlock.gif            sr.Close();
InBlock.gif            
if (config.IsCache(action))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ObjCache.Insert(sKey, target);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//return sl;
InBlock.gif            
//throw new Exception("The method or operation is not implemented.");
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
public int Query4Page(string action,ref IList target, object queryparam, int pageid, int pagesize,string sort,string direct)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object rs = new object();
InBlock.gif            
InBlock.gif            ConfigStruct config 
= new ConfigStruct(new Resource(configpath + queryparam.GetType().Name+".xml").Data);
InBlock.gif            
string sql = config.GetSql(action);
InBlock.gif            
string pagesql = DoPageSql(sql, pageid, pagesize, sort, direct);
InBlock.gif            
string execsql = MakeExecuteSql(pagesql);
InBlock.gif            
string storedsql = MakeValueSql(pagesql, queryparam);
InBlock.gif            
string sKey = queryparam.GetType().Name + ":" + storedsql;
InBlock.gif            
int count = (int)Count(action,DoCountSql(MakeExecuteSql(sql)), queryparam);
InBlock.gif            
if (config.IsCache(action))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (IsInCache(sKey, rs))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    target 
= (IList)ObjCache.GetObject[sKey];
InBlock.gif                    
return count;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            SqlCommand comm 
= new SqlCommand(execsql, conn);
InBlock.gif            PrepareCommand(comm, sql, queryparam);
InBlock.gif           
InBlock.gif            SqlDataReader sr 
= comm.ExecuteReader();
InBlock.gif            
int i = 0;
InBlock.gif            
while (sr.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                rs
=FillObject(sr, queryparam.GetType());
InBlock.gif                target.Add(rs);
ExpandedSubBlockEnd.gif            }

InBlock.gif            sr.Close();
InBlock.gif            
if (config.IsCache(action))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ObjCache.Insert(sKey, target);
ExpandedSubBlockEnd.gif            }
     
InBlock.gif            
return count;
InBlock.gif            
//throw new Exception("The method or operation is not implemented.");
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
public int Query4Update(string action, object target)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object rs = new object();
InBlock.gif            ConfigStruct config 
= new ConfigStruct(new Resource(configpath + target.GetType().Name + ".xml").Data);
InBlock.gif            
string sql = config.GetSql(action);
InBlock.gif            
string execsql = MakeExecuteSql(sql);
InBlock.gif            
string storedsql = MakeValueSql(sql, target);
InBlock.gif            
string sKey = target.GetType().Name + ":" + storedsql;
InBlock.gif            SqlCommand comm 
= new SqlCommand(execsql, conn);
InBlock.gif            PrepareCommand(comm, sql, target);
InBlock.gif            
int ct=comm.ExecuteNonQuery();
InBlock.gif            ObjCache.Flash(target.GetType().Name);
InBlock.gif            
return ct;
InBlock.gif            
//throw new Exception("The method or operation is not implemented.");
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
public object Query4Count(string action, object target)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ConfigStruct config 
= new ConfigStruct(new Resource(configpath + target.GetType().Name + ".xml").Data);
InBlock.gif            
string sql = config.GetSql(action);
InBlock.gif            
return Count(action,sql, target);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void Close()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            conn.Close();
InBlock.gif            
//throw new Exception("The method or operation is not implemented.");
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private string DoPageSql(String command, int pageid, int pagesize, string sor
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值