04 chuong 4 - sap xep, tim kiem, loc du lieu2. 2
N畛i dung
S畉p x畉p, t狸m ki畉m, l畛c trong DataTable
L畛p DataRelation
L畛p DataView
3. 3
S畉p x畉p, t狸m ki畉m, l畛c trong DataTable
T狸m 1 d嘆ng
Ch畛n m畛t s畛 d嘆ng
C畛t t鱈nh to叩n
Hm nh坦m tr棚n c畛t
4. 4
S畉p x畉p, t狸m ki畉m, l畛c trong DataTable
T狸m 1 d嘆ng
T狸m 1 d嘆ng: L l畉y th担ng tin chi ti畉t c畛a 1 ID
hay kh坦a ch鱈nh no 坦
V鱈 d畛:
T狸m th担ng tin chi ti畉t c畛a m畛t kh叩ch hng c坦
m達 kh叩ch hng cho tr動畛c
T狸m th担ng tin chi ti畉t c畛a h坦a 董n c坦 m達 h坦a
董n cho tr動畛c
5. 5
S畉p x畉p, t狸m ki畉m, l畛c trong DataTable
T狸m 1 d嘆ng
Database: Th畛c thi c但u l畛nh SQL
Select
From tenBang
Where CotKhoaChinh=giatri
DataTable: D湛ng ph動董ng th畛c Find c畛a
DataTable.Rows (畛i t動畛ng c畛a l畛p
DataRowCollection
6. 6
S畉p x畉p, t狸m ki畉m, l畛c trong DataTable
T狸m 1 d嘆ng
C炭 ph叩p ph動董ng th畛c Find
DataRow DataRowCollection.Find(object key)
Ch炭 箪
Hm Find t狸m gi叩 tr畛 key ch畛 tr棚n c畛t kh坦a
ch鱈nh table ph畉i c坦 kh坦a ch鱈nh
畛 t畉o kh坦a ch鱈nh:
D湛ng DataSet c坦 ki畛u m畉nh
D湛ng hm FillSchema
T畛 t畉o kh坦a b畉ng code
7. 7
S畉p x畉p, t狸m ki畉m, l畛c trong DataTable
T狸m 1 d嘆ng
V鱈 d畛
myTable.PrimaryKey = new DataColumn[]
{ myTable.Columns["CustomerID"] };
string s = "primaryKeyValue";
DataRow foundRow = myTable.Rows.Find(s);
if (foundRow != null)
MessageBox.Show(foundRow[1].ToString());
else
MessageBox.Show("A row with the primary key of " +
s + " could not be found");
8. 8
S畉p x畉p, t狸m ki畉m, l畛c trong DataTable
T狸m 1 d嘆ng
DataSet ki畛u m畉nh: Cung c畉p s畉n ph動董ng
th畛c FindBy<TenCotkhoaChinh>(...)
NhanVienDataTable nv;
string s = "primaryKeyValue";
NhanVienRow foundRow = nv.FindByMaNV(s);
if (foundRow != null)
MessageBox.Show(foundRow.MaNV);
else
MessageBox.Show("A row with the primary key of " +
s + " could not be found");
9. 9
S畉p x畉p, t狸m ki畉m, l畛c trong DataTable
Ch畛n m畛t s畛 d嘆ng
Ch畛n m畛t s畛 d嘆ng: truy v畉n 1 s畛 d嘆ng t畛 1
b畉ng cho tr動畛c theo 1 i畛u ki畛n no 坦
Database: Th畛c thi c但u l畛nh SQL
Select
From tenBang
Where tenCot=giatri
Select *
From NhanVien
Where Ten like Ta%
V鱈 d畛:
10. 10
S畉p x畉p, t狸m ki畉m, l畛c trong DataTable
Ch畛n m畛t s畛 d嘆ng
DataTable: D湛ng ph動董ng th畛c Select c畛a 畛i
t動畛ng DataTable
DataRow[] rows = tenBang.Select();
DataRow[] rows = tenBang.Select(string dk);
DataRow[] rows = tenBang.Select(string dk, string sort);
DataRow[] rows = tenBang.Select(string dk, string sort,
DataRowState state);
DataRow[] rows = dt.Select("ho like '%Tr%'");
DataRow[] rows = dt.Select("ho like '%Tr%', ten asc);
V鱈 d畛
11. 11
S畉p x畉p, t狸m ki畉m, l畛c trong DataTable
C畛t t鱈nh to叩n
C畛t t鱈nh to叩n: l c畛t 動畛c sinh ra t畛 c叩c
ph辿p to叩n tr棚n c叩c c畛t kh叩c
Database: Th畛c thi c但u l畛nh SQL
Select , cot1 <pheptoan> cot2 as TenCot,
From tenBang
V鱈 d畛
Select ProductID,Price,Tax,(Price+Price*Tax) as TotalPrice
From Products
12. 12
S畉p x畉p, t狸m ki畉m, l畛c trong DataTable
C畛t t鱈nh to叩n
DataTable: Ch畛 ra expression (bi畛u th畛c) cho
c畛t m畛i (thu畛c t鱈nh Expression c畛a l畛p
DataColumn)
DataTable table;
DataColumn col = new DataColumn(TenCot);
col.Expression = bieu thuc;
table.Columns.Add(col);
V鱈 d畛
DataColumn totalPrice = new DataColumn(Total Price);
totalPrice.Expression = Price + Price*Tax;
table.Columns.Add(col);
13. 13
S畉p x畉p, t狸m ki畉m, l畛c trong DataTable
C畛t t鱈nh to叩n
Ch炭 箪
C畛t t鱈nh to叩n tr棚n DataSet c坦 ki畛u lm t動董ng t畛
i畛u ki畛n r畉t quan tr畛ng 畛 c畛t t鱈nh to叩n c坦 th畛
ho畉t 畛ng 動畛c l c叩c c畛t tham gia vo bi畛u
th畛c ph畉i c坦 ki畛u t動董ng 畛ng v畛i ph辿p to叩n (c叩c
c畛t m畉c 畛nh c坦 ki畛u string)
D湛ng DataSet c坦 ki畛u
Ch畛 r探 ki畛u b畉ng thu畛c t鱈nh DataType c畛a l畛p
DataColumn
DataColumn col;
col.DataType = typeof(kieu);
14. 14
S畉p x畉p, t狸m ki畉m, l畛c trong DataTable
Hm nh坦m tr棚n c畛t
Database: Th畛c thi c但u l畛nh SQL
Select min() From
Select max() From
Select avg() From
Select count() From
Select sum() From
Select sum(price)
From Products;
15. 15
S畉p x畉p, t狸m ki畉m, l畛c trong DataTable
Hm nh坦m tr棚n c畛t
DataTable: D湛ng ph動董ng th畛c Compute c畛a
l畛p DataTable
object table.Compute(bi畛u th畛c, b畛 l畛c)
bi畛u th畛c: gi畛ng m畛nh 畛 select
Max(c畛t)
Min(c畛t)
Sum(c畛t)
Count(c畛t)
Avg(c畛t)
b畛 l畛c: gi畛ng m畛nh 畛 where
16. 16
S畉p x畉p, t狸m ki畉m, l畛c trong DataTable
Hm nh坦m tr棚n c畛t
V鱈 d畛:
int p;
p = int.Parse(table.Compute(sum(Price), );
Ch炭 箪
D湛ng l畛p Convert hay hm Parse 畛 chuy畛n
sang ki畛u d畛 li畛u t動董ng 畛ng
int p;
p = int.Parse(table.Compute(sum(Price), Price<100);
17. 17
L畛p DataRelation
Database
Cho ph辿p t畉o quan h畛 gi畛a c叩c b畉ng
Cho ph辿p k畉t (join) gi畛a c叩c b畉ng
DataTable:
Cho ph辿p t畉o quan h畛 gi畛a c叩c b畉ng b畉ng 畛i
t動畛ng DataRelation
Kh担ng cho ph辿p k畉t (join) gi畛a c叩c b畉ng
Cho ph辿p l畉y c叩c d嘆ng con c畛a 1 d嘆ng
Cho ph辿p l畉y d嘆ng cha c畛a 1 d嘆ng
18. 18
L畛p DataRelation
L畉y c叩c d嘆ng con v d嘆ng cha
DataRow[] rows;
DataRow row;
rows = parentTable.Rows[index].GetChildRows(relation);
row = childTable.Rows[index].GetParentRow(relation);
T畉o quan h畛 (Ch動董ng 5)
ds.Relations.Add(new DataRelation());
19. 19
L畛p DataView
Kh叩i ni畛m DataView
T畉o DataView
S畉p x畉p, t狸m ki畉m, l畛c trong DataView
Chuy畛n DataView thnh DataTable
20. 20
L畛p DataView
Kh叩i ni畛m DataView
畛i t動畛ng DataView thay th畉 cho c但u l畛nh
Select c畛a 畛i t動畛ng DataTable
畉c i畛m DataView
DataView c坦 th畛 binding vo control (K畉t qu畉
c畛a c但u l畛nh Select th狸 kh担ng th畛)
DataView ch畛 cho ph辿p lm vi畛c tr棚n 1 b畉ng
kh担ng cho ph辿p ch畛n 1 t畉p con c叩c c畛t
N畉u DataTable g畛m c坦 c叩c DataRows th狸
DataView g畛m c坦 c叩c DataRowViews
21. 21
L畛p DataView
T畉o DataView
C叩c c叩ch t畉o DataView
DataView vw = new DataView();
DataView vw = new DataView(table);
DataView vw = new DataView(table, KLoc,
CotSXep, K RowState);
Trong 坦
KLoc: i畛u ki畛n l畛c d畛 li畛u
CotSXep: T棚n c畛t d湛ng 畛 s畉p x畉p
K RowState:
DataViewRowState.Unchanged,
22. 22
L畛p DataView
S畉p x畉p, t狸m ki畉m, l畛c trong DataView
Thu畛c t鱈nh c畛a DataView
RowFilter: Ch畛 ra i畛u ki畛n l畛c gi畛ng c但u l畛nh Select.
V鱈 d畛: Ho like Tr%
Sort: ch畛 ra th畛 t畛 sort. V鱈 d畛: MaSV asc
Ph動董ng th畛c c畛a DataView
int Find(object): Tr畉 v畛 ch畛 s畛 d嘆ng th畛a i畛u ki畛n t狸m
ki畉m
DataRowView[] FindRows(object hay object[]): Tr畉 v畛 1
s畛 DataRowView th畛a i畛u ki畛n t狸m ki畉m
Ch炭 箪: 2 Hm ny ch畛 t狸m ki畉m tr棚n c叩c c畛t 動畛c ch畛 ra
trong thu畛c t鱈nh Sort c畛a DataView
23. 23
L畛p DataView
Chuy畛n DataView sang DataTable
C炭 ph叩p
DataTable vw.ToTable()
DataTable vw.ToTable("ten bang")
DataTable vw.ToTable(boolean, m畉ng chu畛i c畛t)
DataTable vw.ToTable("ten bang", boolean, m畉ng
chu畛i c畛t)
Trong 坦
boolean: true lo畉i b畛 nh畛ng d嘆ng tr湛ng l畉p
(distinct)
Editor's Notes #5: In the case of a database, youd simply execute a SQL
query, but a DataTable is not a table inside a database and cannot use a SQL query and a database
engine to help itself do this job.
#6: In the case of a database, youd simply execute a SQL
query, but a DataTable is not a table inside a database and cannot use a SQL query and a database
engine to help itself do this job.
#14: Tr動畛c khi fill d畛 li畛u