entlib5.0
早就出來了,在這個版本中提供了一些很好用的特性,今天小試一下,用用
sprocaccessor
和sqlstringaccessor
這兩個類,這兩個類能夠將返回的
dataset, datatable
樣式的資料集轉換為實體資料的形式。
1.原始的查詢方式。
database db = databasefactory.createdatabase();
dbconnection connection = db.createconnection();
dbcommand cmd = connection.createcommand();
cmd.commandtype = commandtype.text;
cmd.commandtext = "
select * from person
";dataset ds = db.executedataset(cmd);
2.採用預設的對映。這種對映資料庫列和實體資料的屬性是一一對應的。
資料庫中的列
實體型別:person
public
class person
public
string lastname
public
string firstname
public datetime? hiredate
public datetime? enrollmentdate
public
override
string tostring()
,name:-
", personid, firstname, lastname);}}
接下來看看我們如何查詢
string sql1 = "
select * from person
";//
查詢全部資料
dataaccessoraccessor = db.createsqlstringaccessor(sql1);
listpersons = accessor.execute().tolist();
persons.foreach(p =>
);3.自定義對映。
單行資料的對映,考慮到實際專案的情況,我們前段展示的資料形式(viewmodel)可能和原始的model有些不一樣,這裡我們重新定義個實體型別,可能和資料庫列不存在一一對應的關係
//////
資料傳輸物件
///public
class personcoursegradedto
public
string coursename
public
double grade
public
override
string tostring()
,coursename:,grade:
", personname, coursename, grade);}}
public
firstname
");string firstname = row.getvalue(col) == dbnull.value ? "" : row.getvalue(col).tostring();
col = row.getordinal("
lastname
");string lastname = row.getvalue(col) == dbnull.value ? "" : row.getvalue(col).tostring();
col = row.getordinal("
title
");string title = row.getvalue(col) == dbnull.value ? "" : row.getvalue(col).tostring();
col = row.getordinal("
grade
");double grade = row.getvalue(col) == dbnull.value ? 0 : convert.todouble(row.getvalue(col));
dto.personname = firstname + "
-" + lastname;
dto.coursename = title;
dto.grade = grade;
return dto;}}
如何呼叫?
多行資料的對映
public
firstname
");string firstname = reader.getvalue(col) == dbnull.value ? "" : reader.getvalue(col).tostring();
col = reader.getordinal("
lastname
");string lastname = reader.getvalue(col) == dbnull.value ? "" : reader.getvalue(col).tostring();
col = reader.getordinal("
title
");string title = reader.getvalue(col) == dbnull.value ? "" : reader.getvalue(col).tostring();
col = reader.getordinal("
grade
");double grade = reader.getvalue(col) == dbnull.value ? 0 : convert.todouble(reader.getvalue(col));
dto.personname = firstname + "
-" + lastname;
dto.coursename = title;
dto.grade = grade;
yield
return dto;}}
}如何查詢?
入門文章,給初次接觸企業庫的朋友!
unity之資料查詢
嗨,歡迎來到我們的 狗刨網,我們今天講了很多內容,有資料庫,還有如何連線資料庫,每天我們都會更新新的內容,每天我們都該充實自己的生 活,每天都要有進步。一 資料查詢 基礎 查詢全部的列 select from 表 查詢部分行 select name,age from student where na...
Hive之資料查詢
發布於 2013 年 10 月 11 日 由 aaron 發布於 hive 一,排序和聚合 對於排序有兩種方式,一種是order by 一種是sort by order by 會對所有的資料進行排序,所以最後會只有乙個reducer來處理,如果資料量非常大,效率會非常差勁 sort by是部分排序,...
mysql資料查詢之連線查詢
連線查詢概念 1 交叉連線 最後得到的結果是拼在一起的,所謂的笛卡爾積的形式,這個沒什麼用 select from student cross join class 2 內連線 從左表中取出每條資料,和右表中的所有資料進行匹配,當左表和右表的值相同時,結果才保留 select from studen...