主要是摘自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_Salaryemps.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;