MyGeneration学习笔记(7) :dOOdad的String Properties和动态查询

         主要是摘自dOOdad文档,另外加了些自己的注释,纠正了个别描述不准确的地方。

1.   String Properties

        dOOdad另一个非常有用的特点就是“String Properties”,实现了统一处理字符串类型列和非字符串类型列的空值问题。For each data column in the dOOdad, there is a string property in addition to the column property。(ps:虽然文档上是这么说的,但是在数据库中数据类型为Picture的列,在dOOdad中是没有string Property的)
    例如:

emps.Salary 和emps.s_Salary
emps.HireDate 和 emps.s_HireDate
检查空值:
if (emps.s_Salary  ==   "" )
if (emps.s_HireDate  ==   "" )
设置空值:
emps.s_LastName 
=   "" ;
emps.s_HireDate 
=   ""

 

2.    动态查询:
            dOOdad也提供了动态查询的功能,使用动态查询功能,可以避免写一些散杂的查询存储过程来进行查询。
   另外,dOOdad提供的动态查询是生成参数化的查询语句,从而可以避免SQL注入攻击。
  2.1    WhereParameter类 中的Enum:
          (1) 联合:(WhereParameter.Conj)
                            And
                            Or
                            UseDefault
          (2) 排序:(WhereParameter.Dir)
                             ASC
                            DESC
          (3) 运算:(WhereParameter.Operand)
                             Between
                             Equal
                             GreaterThan
                             GreaterThanOrEqual
                              In
                              IsNotNull
                              IsNull
                              LessThan
                              LessThanOrEqual
                              Like
                              NotEqual
                              NotIn
                              NotLike

  2.2 使用动态查询:
     (1).  查询符合条件的所有行:

emps.Where.LastName.Value  =   " %A% " ;
emps.Where.LastName.Operator 
=  WhereParameter.Operand.Like;
// Note: Conjuction not Conjunction
emps.Where.HireDate.Conjuction  =  WhereParameter.Conj.Or;
emps.Where.HireDate.BetweenBeginValue 
=   " 2001-01-01 00:00:00.0 " ;
emps.Where.HireDate.BetweenEndValue 
=   " 2001-12-31 23:59:59.9 " ;
emps.Where.HireDate.Operator 
=  WhereParameter.Operand.Between;
// 根据上面的条件,dOOdad会自动生成查询语句
emps.Query.Load();
// ps:Load()方法有重载,我们也可以给Load方法传入Sql查询条件(Sql SELECT语句的Where子句),而不用像上面一样写一堆的赋值。

  
     (2).  返回指定的列集(此时不能调用Save(),因为除此之外的其他列在数据库中存在,而在emps对象的_dataTable中不存在,而在Save中,需要提供所有列的数据作为存储过程的参数):
 emps.Query.AddResultColumn(Employees.ColumnNames.EmployeeID);
 emps.Query.AddResultColumn(Employees.ColumnNames.LastName);
 emps.Query.Load();

     (3). 排序
          emps.Query.AddOrderBy(Employees.ColumnNames.HireDate,WhereParameter.Dir.DESC);

     (4). Select Distinct
          emps.Query.Distinct = true;

     (5).Select Top N
          emps.Query.Top = n;
 
     (6).Parentheses
          emps.Query.OpenParenthesis();
          emps.Query.CloseParenthesis();

     (7).GenerateSQL
        提供诊断动态查询SQL语句的功能,返回SQL语句。
         A diagnostic function that returns the SQL statement created for the dynamic query.
         After calling this you cannot load the object. Better to use LastQuery.

     (8).最后一次查询
         A string property that contains the SQL text of the most recently generated SQL statement.
 
     (9).返回Reader
         SqlDataReader reader = emps.Query.ReturnReader() as SqlDataReader;

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值