主要是摘自dOOdad文檔,另外加了些自己的注釋?zhuān)m正了個(gè)別描述不準(zhǔn)確的地方。
1. String Properties
dOOdad另一個(gè)非常有用的特點(diǎn)就是“String Properties”,實(shí)現(xiàn)了統(tǒng)一處理字符串類(lèi)型列和非字符串類(lèi)型列的空值問(wèn)題。For each data column in the dOOdad, there is a string property in addition to the column property。(ps:雖然文檔上是這么說(shuō)的,但是在數(shù)據(jù)庫(kù)中數(shù)據(jù)類(lèi)型為Picture的列,在dOOdad中是沒(méi)有string Property的)
例如:
emps.Salary 和emps.s_Salary
emps.HireDate 和 emps.s_HireDate
檢查空值:
if(emps.s_Salary == "")
if(emps.s_HireDate == "")
設(shè)置空值:
emps.s_LastName = "";
emps.s_HireDate = "";
2. 動(dòng)態(tài)查詢(xún):
dOOdad也提供了動(dòng)態(tài)查詢(xún)的功能,使用動(dòng)態(tài)查詢(xún)功能,可以避免寫(xiě)一些散雜的查詢(xún)存儲(chǔ)過(guò)程來(lái)進(jìn)行查詢(xún)。
另外,dOOdad提供的動(dòng)態(tài)查詢(xún)是生成參數(shù)化的查詢(xún)語(yǔ)句,從而可以避免SQL注入攻擊。
2.1 WhereParameter類(lèi) 中的Enum:
(1) 聯(lián)合:(WhereParameter.Conj)
And
Or
UseDefault
(2) 排序:(WhereParameter.Dir)
ASC
DESC
(3) 運(yùn)算:(WhereParameter.Operand)
Between
Equal
GreaterThan
GreaterThanOrEqual
In
IsNotNull
IsNull
LessThan
LessThanOrEqual
Like
NotEqual
NotIn
NotLike
2.2 使用動(dòng)態(tài)查詢(xún):
(1). 查詢(xún)符合條件的所有行:
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;
//根據(jù)上面的條件,dOOdad會(huì)自動(dòng)生成查詢(xún)語(yǔ)句
emps.Query.Load();
//ps:Load()方法有重載,我們也可以給Load方法傳入Sql查詢(xún)條件(Sql SELECT語(yǔ)句的Where子句),而不用像上面一樣寫(xiě)一堆的賦值。
(2). 返回指定的列集(此時(shí)不能調(diào)用Save(),因?yàn)槌酥獾钠渌性跀?shù)據(jù)庫(kù)中存在,而在emps對(duì)象的_dataTable中不存在,而在Save中,需要提供所有列的數(shù)據(jù)作為存儲(chǔ)過(guò)程的參數(shù)):
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
提供診斷動(dòng)態(tài)查詢(xún)SQL語(yǔ)句的功能,返回SQL語(yǔ)句。
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).最后一次查詢(xún)
A string property that contains the SQL text of the most recently generated SQL statement.
(9).返回Reader
SqlDataReader reader = emps.Query.ReturnReader() as SqlDataReader;
浙公網(wǎng)安備 33010602011771號(hào)