Vous êtes sur la page 1sur 20

Lp trnh Windows Form vi C#

Bi ging mn hc

Ni dung
ADO.NET

Bi 7: ADO.NET

S lc lch s pht trin Kin trc ADO.NET .NET Data Provider

Lng Trn Hy Hin FIT, HCMUP

DataSet Hi & p

Lp trnh Windows Form vi C#


2
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

Gii thiu ADO.NET

Gii thiu ADO.NET


ActiveX Data Object .NET (ADO.NET)
Cng ngh ca MS Pht trin t nn tng ADO Cung cp cc lp i tng v hm th vin phc v cho vic kt ni v x l d liu
ADO.NET

M hnh .NET Framework


Microsoft .NET Framework

Web Services

User Interface

Data and XML


ADO.NET

XML Base Classes

...

...

.NET Application

Common Language Runtime


3
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

Ni dung
ADO.NET S lc lch s pht trin Kin trc ADO.NET .NET Data Provider DataSet Hi & p

S lc lch s pht trin


Native API
DB API DB API DB API

Application Database

Open DataBase Connectivity


DB API

Driver
ODBC API

DB API DB API

ODBC

Driver Driver

Application Database 5
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

S lc lch s pht trin (cont)


OLEDB v ADO
Provider

S lc lch s pht trin (cont)


ADO.NET

ADO OLE DB Application

Provider

Non-Relational Data
Provider Provider

Driver ODBC Driver Driver Relational Data

OLE: Object Linking and Embedding 7 8

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

Introduction

ADO.NET architecture

Stored into

XML
Database (Oracle, SQL Server)

XML
Database

ADO.NET
Client
9

Data access technology


Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

10

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

ADO.NET (tt)
H tr bi .Net Platform S dng cng ngh XML chuyn i d liu. Tng tc vi tt c cc loi c s d liu. Kh nng thc thi nhanh. S dng cho cc loi ng dng clientserver.

Ni dung
ADO.NET S lc lch s pht trin Kin trc ADO.NET .NET Data Provider DataSet Hi & p

11

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

12

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

Kin trc
Connected Model

ADO.NET
ADO.NET l mt phn ca .NET Framework
Th vin lp c chc nng thao tc d liu trong ngn ng MS.NET

Chng trnh

ADO.NET l dng Disconnected


Cho php ly c mt cu trc phc tp ca DL t CSDL, sau ngt kt ni ri mi thc hin thao tc x l! Trc y ADO lun phi duy tr kt ni trong qu trnh lm vic.

Disconnected Model

Chng trnh 13
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

14

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

Mi trng connected
Mi user c mt kt ni c nh ti data source u im
Mi trng c bo v tt Kim sot c s ng b D liu lun c mi

Mi trng disconnected
Mt tp con ca d liu trung tm c sao chp v b sung c lp, sau s c merge li vo d liu trung tm. u im
C th lm vic bt c lc no, cng nh c th kt ni bt k vo Data Source Cho php user khc c th kt ni Nng cao hiu sut thc hin ca ng dng

Nhc
Phi c mt kt ni mng c nh Scalability
15
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Khuyt
D liu khng c cp nht mt cch nhanh nht S tranh chp c th xut hin v phi gii quyt
16
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

ADO.NET
ADO.NET mnh m
K tha cc u im ca ADO Kt hp vi tng thit k hon ton mi

ADO.NET vs. ADO


c im
DL x l c a vo b nh di dng Duyt d liu

ADO
Recordset : tng ng 1 bng d liu trong database Recordset ch cho php duyt tun t, tng dng mt. Recordset thin v hng kt ni, nn vic h tr ngt kt ni khng mnh Kh nng trao i d liu ADO qua Internet thng c nhiu hn ch. Do dng chun COM

ADO.NET
Dataset : tng ng 1 database

Dataset : duyt t do, ngu nhin, truy cp thng ti bng, dng ,ct mong mun. Dataset h tr hon ton ngt kt ni

c im ni bt
Thit k hon ton da vo XML Chun giao tip d liu tt nht trn mi trng Internet hin nay Thit k hon ton hng i tng c trng ca th vin .NET Framework
17
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

D liu ngt kt ni

Trao i d liu qua Internet

ADO.NET trao i d liu qua Internet rt d dng v ADO.NET c thit k theo chun XML, l chun d liu chnh c s dng trao i trn Internet.

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

ADO.NET
Kin trc ca ADO.NET gm 2 phn chnh Phn kt ni: s dng khi kt ni CSDL v thao tc d liu, phi thc hin kt ni khi thao tc
Connection: qun l vic ng m DB ???Connection: SqlConnection, OleDbConnection Command: lnh truy vn, tng tc d liu khi ang lp kt ni ???Command: SqlCommand, OleDbCommand DataReader: c d liu, ch x l 1 dng d liu ti mt thi im ???DataReader: SqlDataReader, OleDbDataReader DataAdapter: cu ni gia DataBase v DataSet
19
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

ADO.NET
Phn ngt kt ni: l DataSet
DataSet khng quan tm n Database thuc kiu g, m ly d liu t DataAdapter x l DataSet xem nh mt Database trong b nh: bng, quan h DataSet c cc thnh phn con nh DataTable DataRow DataColumn DataRelation Cc i tng nhm: DataTableCollection, DataRowCollection, DataColumnCollection
20
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

M hnh i tng ADO.NET

Namespace
System.Data All generic data access classes System.Data.Common Classes shared (or overridden) by individual data providers System.Data.Odbc ODBC provider classes System.Data.OleDb OLE DB provider classes System.Data.ProviderBase New base classes and connection factory classes System.Data.Oracle Oracle provider classes System.Data.Sql New generic interfaces and classes for SQL Server data access System.Data.SqlClient SQL Server provider classes System.Data.SqlTypes SQL Server data types

21

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

22

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

Cc lp th vin ADO.NET
System.Data.OleDb: Access, SQL Server, Oracle System.Data.SqlClient: SQL Server System.Data.OracleClient: Oracle c im:
C ba th vin trn v giao tip lp trnh l ging nhau Dng th vin SqlClient truy xut SQL Server nhanh hn OleDb Tng t cho OracleClient

.NET Data Provider


Cc thnh phn .NET Data Provider

23

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

24

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

Connected Model
Kt ni vo CSDL Thc hin lnh
Thm/Xa/Sa d liu c d liu t CSDL

Connected Model
Kt ni vo CSDL
Cc lp ph trch kt ni ODBCConnection OleDBConnection SqlConnection OracleConnection Thuc tnh quan trng ConnectionString: xc nh ngun d liu cn kt ni Phng thc quan trng Open: m kt ni Close: ng kt ni

25

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

26

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

.NET Data Provider - Connection

.NET Data Provider Connection (cont)


Kt ni vo CSDL Access

Connection Application Data Source

OleDbConnection cnn = new OleDbConnection(); cnn.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=QLHS.mdb"; cnn.Open(); //Cc lnh cn thc hin cnn.Close();

Cc i tng Connection tun th IDbConnection interface.


ConnectionString: loi Data Source cn kt ni. Open(): thit lp kt ni n Data Source. Close(): ngt kt ni n Data Source.
27
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

28

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

.NET Data Provider Connection (cont)


Kt ni vo CSDL SQL Server:
SqlConnection cnn = new SqlConnection(server= HIENLTH-PC\\SQLEXPRESS; database = QLHS; user id=sa; password=sa); cnn.Open(); // Thc hin truy vn d liu cnn.Close();
29
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

SQL2005 ConnectionString
Theo c quyn ca SQL Server (c user, pass):
server= HIENLTH-PC\SQLEXPRESS; database = QLHS; user id=sa; password=sa

Theo c quyn ca h iu hnh:


server= HIENLTH-PC\SQLEXPRESS; database = QLHS; Trusted Connection = True; server= HIENLTH-PC\SQLEXPRESS; database = QLHS; Integrated Security = True;
30
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

Tp tin lu tr kt ni
Cho php ngi dng c th cu hnh cc gi tr thuc tnh cho chui kt ni CSDL. Thng c dng *.ini, *.txt. T .NET 1.0 tr ln c th s dng tp tin App.config (nh dng XML) cha khai bo cc tham s cng gi tr v cc ch th khc. V d: Tp tin DBConfig.ini

c thng tin tp tin lu tr kt ni


Loi *.ini, *.txt s dng StreamReader (using System.IO)
StreamReader docfile = new StreamReader(@"DBConfig.ini"); servername = Tach(docfile.ReadLine()); databasename = Tach(docfile.ReadLine()); username = Tach(docfile.ReadLine()); password = Tach(docfile.ReadLine());

31

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

32

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

c thng tin tp tin lu tr kt ni


Loi App.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="SqlServer" connectionString="server=HIENLTHPC\sqlexpress;database=SEQLHS;integrated security=true" providerName="System.Data.SqlClient"/> </connectionStrings> </configuration>

c thng tin tp tin lu tr kt ni


Loi App.config:
connectionstring = ConfigurationManager.ConnectionStrings[tn]. ConnectionString;
V d:

con.ConnectionString =
ConfigurationManager.ConnectionStrings[SqlServer "]. ConnectionString;

33

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

34

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

.NET Data Provider - Command

.NET Data Provider - Command


Cc i tng Command tun th IDbCommand interface.

011010011 Connection Application

011010011 Data Source

Command

Connection: kt ni dng thc hin cu lnh. CommandText: cu lnh SQL cn thc hin trn Data Source. CommandType: loi cu lnh trong CommandText (Text, TableDirect, StoredProc). ExecuteScalar(): thc hin cu lnh trong CommandText, kt qu tr v l mt gi tr n. ExecuteNonQuery(): thc hin cu lnh trong CommandText v khng c kt qu tr v. ExecuteReader(): thc hin cu lnh trong CommandText, kt qu tr v l mt DataReader.

35

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

36

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

Command (cont) SQL Server


SqlConnection cnn = new SqlConnection(server=HIENLTHPC\\SQLEXPRESS; database=QLHS; user id=sa; password=sa); SqlCommand cmd = new SqlCommand();

Command (cont) SQL Server


SqlConnection cnn = new SqlConnection(server=HIENLTHPC\SQLEXPRESS; database=QLHS; user id=sa; password=sa); SqlCommand cmd = new SqlCommand(); cmd.Connection = cnn;

cmd.Connection = cnn; cmd.CommandText = SELECT COUNT(*) FROM HocSinh; cmd.CommandType = CommandType.Text; cnn.Open(); int count = (int)cmd.ExecuteScalar(); cnn.Close();
37 38

cmd.CommandText = INSERT INTO HocSinh(Ho, Ten, DienThoai) VALUES(NNguyn Vn, NTrng, 0989366990); cmd.CommandType = CommandType.Text; cnn.Open(); cmd.ExecuteNonQuery(); cnn.Close();

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

Command (cont) MS Access


Thc hin lnh
Thm/Xa/Sa d liu

.NET Data Provider - Parameter


Mc ch s dng:
Mt vi gi tr trong cu lnh ch bit khi thc hin cu lnh. Cn thc hin cu lnh nhiu ln vi cc gi tr khc nhau.

V d:
OleDbCommand cmd = new OleDbCommand(); cmd.Connection = cnn; cmd.CommandText = INSERT INTO Hocsinh VALUES(5, 'L Vn Xim','11/12/2006', 'Khnh Ha', '221 Nguyn Vn C')"; cmd.ExecuteNonQuery();
39
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Cc bc thc hin:
Tham s ha cu lnh: ? hoc @[tn tham s]. To cc parameters tng ng cho command. t gi tr cho cc parameter mi khi dng command thc hin cu lnh.
40
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

.NET Data Provider Parameter(cont)


Tham s ha
SQL Data Provider:
cmd.CommandText = SELECT * FROM HocSinh WHERE tenhocsinh = @ten; cmd.CommandText = INSERT INTO HocSinh(id_hocsinh, tenhocsinh, dtb) + VALUES(@id, @ten, @dtb);

.NET Data Provider Parameter(cont)


To cc parameter
cmd.Parameters.Add(@id, 5); cmd.Parameters.Add(@ten, Nguyn Vn A); cmd.Parameters.Add(@dtb, 8.5);

t gi tr cho cc parameter
foreach (Student s in studentList) { cmd.Parameters[@id] = i; cmd.Parameters[@ten] = s.studentName; cmd.Parameters[@dtb] = s.studentMarks; cmd.ExecuteNonQuery(); }
42
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Cc provider khc:
cmd.CommandText = SELECT * FROM HocSinh WHERE tenhocsinh = ?; cmd.CommandText = INSERT INTO HocSinh(id_hocsinh, tenhocsinh, dtb) + VALUES(?, ?, ?);
41
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

Parameter(cont) MS Access
V d
OleDbCommand cmd = new OleDbCommand(); cmd.Connection = cnn; cmd.CommandText = "INSERT INTO Hocsinh VALUES (@Ma,@Ten,@Diachi,@NgaySinh,@Toan,@Van); OleDbParameter para=cmd.Parameters.Add("@Ma",OleDbType.Integer); para.Value=1; para=cmd.Parameters.Add("@Ten",OleDbType.VarWChar); para.Value= N"L Vn A"; para=cmd.Parameters.Add("@Diachi",OleDbType.VarWChar); para.Value= N"Nguyn Vn C"; para = cmd.Parameters.Add("@Ngaysinh", OleDbType.Date); para.Value = new DateTime(1982,10,1); para=cmd.Parameters.Add("@Toan",OleDbType.Integer); para.Value=5; para=cmd.Parameters.Add("@Van",OleDbType.Integer); para.Value=7; cmd.ExecuteNonQuery();

.NET Data Provider DataReader

ExecuteReader() Read() DataReader Application Connection 011010011 011010011 Data Source

Command MaHS i HoTen HocSinh I DiaChi DiaChi i

43

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

.NET Data Provider DataReader


DataReader l g?
Ch di chuyn ti pha trc, ch c truy cp d liu nhanh, kt ni n ngun d liu (data source) Qun l d liu, hoc rng buc vo iu khin listbound S dng ti nguyn

.NET Data Provider - DataReader


Cc i tng DataReader tun th interface IDataReader.
HasRow: cho bit cn d liu c na khng. Read(): c mt mu tin vo DataReader. Ton t [ i ]: truy xut n ct i trong mu tin c c. Close(): ng DataReader.

Lu :
Truy xut tun t v khng quay lui. Khng cp nht d liu. C ch kt ni.

45

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

46

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

.NET Data Provider DataReader (cont)


SqlConnection cnn = new SqlConnection(server=localhost; database=Northwind; user id=sa; password=sa); SqlCommand cmd = new SqlCommand(); cmd.Connection = cnn; cmd.CommandText = select * from Orders; cmd.CommandType = CommandType.Text; cnn.Open(); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { MessageBox.Show(dr[CustomerID].ToString()); } dr.Close(); cnn.Close();
47
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

DataReader (cont) MS Access


Thc hin lnh
c d liu t CSDL V d

OleDbDataReader rd = cmd.ExecuteReader(); while (rd.Read()) { if (!rd.IsDBNull(3)) MessageBox.Show(dr[CustomerID].ToString()); }


48
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

.NET Data Provider - SqlDataAdapter


B c d liu t CSDL ngun (SQLServer) v vo i tng DataSet hay DataTable. Phn trang d liu Phng thc:
Fill(datasource) Fill(datasource, start, number, tablename)

.NET Data Provider - DataAdapter


DataAdapter
SelectCommand

DataSet

InsertCommand UpdateCommand

Data Source
DeleteCommand

Cc i tng DataAdapter tun th interface IDbDataAdapter.


Fill(DataSet): dng SelectCommand ly d liu t Data Source vo DataSet. Update(DataSet): dng InsertCommand, UpdateCommand v DeleteCommand cp nht d liu trong DataSet vo Data Source.
50
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

49

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

.NET Data Provider DataAdapter (cont)


SqlConnection cnn = new SqlConnection(server=localhost; database=Northwind; user id=sa; password=sa); SqlDataAdapter da = new SqlDataAdapter(select * from Orders, cnn) DataSet ds = new DataSet(); da.Fill(ds); // Does something on the DataSet. da.Update(ds);
51
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Ni dung
S lc lch s pht trin Kin trc ADO.NET .NET Data Provider DataSet Hi & p

52

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

System.Data namespace
DataSet This object is designed for disconnected use and can contain a set of DataTables and include relationships between these tables. DataTable A container of data that consists of one or more DataColumns and, when populated, will have one or more DataRows containing data. DataRow A number of values, akin to a row from a database table, or a row from a spreadsheet. DataColumn This object contains the definition of a column, such as the name and data type. DataRelation A link between two DataTable classes within a DataSet class. Used for foreign key and master/detail relationships. Constraint This class defines a rule for a DataColumn class (or set of data columns), such as unique values.

DataSet
DataSet l g?
nh x DataSet Data Source

DataSet l c s d liu c lu tr trong b nh chnh (inmemory database). C ch khng kt ni. Gm cc i tng


DataTable DataRelation

53

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

54

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

DataSet (cont)
DataSet
Tables
Columns

Cc phng thc ca DataSet


DataTable
DataColumn

Thm 1 i tng DataTable: ds.Tables.Add(datatable_name); Xa 1 i tng:


ds.Tables.Remove(datatable_name); ds.Table.RemoveAt(datatable_index);

Rows

DataRow

Relations

DataRelation

Loi b tt c DataTable: ds.Tables.Clear(); Kim tra tn ti: ds.Tables.Contains(datatable_name); Kim tra tn ti v c th xa c: ds.Tables.CanRemove(datatable_name); m s lng DataTable: ds.Tables.Count (thuc tnh)
56
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

55

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

Cc phng thc ca DataSet


Ghi ra file XML: ds.WriteXml(ten_file_xml); c t file XML: ds.ReadXml(ten_file_xml);

DataSet - DataTable
DataTable th hin mt bng trong c s d liu. Gm cc i tng:
DataColumn DataRow

Cc thuc tnh v phng thc:


TableName: tn bng. Columns: danh sch cc ct (DataColumn). Rows: danh sch cc mu tin (DataRow). PrimaryKey: danh sch cc ct lm kha chnh (DataColumn). NewRow(): to mt mu tin mi.
57
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

58

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

DataSet - DataColumn
DataColumn th hin mt ct trong bng. Cc thuc tnh v phng thc:
ColumnName: tn ct. DataType: kiu d liu ct.

DataSet - DataRow
DataRow th hin mt mu tin trong bng. Cc thuc tnh v phng thc:
RowState: trng thi ca mu tin (Added, Modified, Deleted, Unchanged, Detach). Ton t [i]: truy xut n ct i ca mu tin. Delete(): nh du xa mu tin.

59

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

60

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

DataSet (cont)
DataTable table = new DataTable(SinhVien); table.Columns.Add(new DataColumn(MSSV, Type.GetType(Int32)); table.Columns.Add(new DataColumn(HoTen, Type.GetType(string)); table.PrimaryKey = new DataColumn[] { table.Columns[MSSV] }; DataRow row = table.NewRow(); row[MSSV] = 123; row[HoTen] = Nguyn Vn A; table.Rows.Add(row);

DataSet (cont)
SqlConnection cnn = new SqlConnection(server=HIENLTH-PC\SQLEXPRESS; database=Northwind; user id=sa; password=sa); SqlDataAdapter da = new SqlDataAdapter(select * from Orders, cnn) DataSet ds = new DataSet(); da.Fill(ds); foreach (DataRow row in ds.Tables[0].Rows) row[OrderDate] = DateTime.Now; da.Update(ds);

61

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

62

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

DataSet (cont)
SqlConnection cnn = new SqlConnection(server=HIENLTHPC\SQLEXPRESS; database=Northwind; user id=sa; password=sa); SqlDataAdapter da = new SqlDataAdapter(select * from Orders, cnn) DataSet ds = new DataSet();

DataSet (cont)
So snh DataSet v DataReader:
Tc truy xut. B nh lu tr. Thun tin trong thao tc. C ch kt ni v khng kt ni.

da.Fill(ds); foreach (DataRow row in ds.Tables[0].Rows) if (row[CustomerID].ToString() == Nguyn Vn A) row.Delete(); da.Update(ds);


63
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

64

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

DataSet (cont) MS Access


c d liu t CSDL OleDbDataAdapter da = new OleDbDataAdapter( "select * from HocSinh", cnn); DataSet dsHocSinh=new DataSet(); da.Fill(dsHocSinh, "HocSinh"); for (int i = 0; i < dsHocSinh.Tables["HocSinh"].Rows.Count; i++) { listBox1.Items.Add( dsHocSinh.Tables["HocSinh"].Rows[i]["HoTen"].ToString()); }
65
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

DataSet (cont) MS Access


X l d liu
Thm d liu DataRow newRow; newRow=dsHocSinh.Tables["HocSinh"].NewRow(); newRow[0] = 10; newRow[1] = "L Th B"; dsHocSinh.Tables["HocSinh"].Rows.Add(newRow); Xo d liu dsHocSinh.Tables["HocSinh"].Rows[0].Delete(); Thay i d liu DataRow row = dsHocSinh.Tables["HocSinh"].Rows[1]; row.BeginEdit(); row[0] = 11; row[1] = "L Th C"; row.EndEdit();
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

66

Lp trnh Windows Form vi C#

DataSet (cont) MS Access


Cp nht d liu xung CSDL
OleDbCommandBuilder buider = new OleDbCommandBuilder(da); da.Update(dsHocSinh, "HocSinh");

Bi ging mn hc

Trnh by d liu

Lp trnh Windows Form vi C#


67
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

Nhc li SqlDataAdapter
SqlDataAdapter c dng in d liu vo i tng DataSet, DataTable v cp nht d liu t hai i tng ny tr li d liu ngun. Cc trng hp to i tng SqlDataAdapter:
SqlDataAdapter(string SQL, SqlConnection cnn); SqlDataAdapter(SqlCommand cmd);

Cp nht d liu t DataAdapter


Trong Form khai bo cc thuc tnh l i tng DataSet (hay DataTable) Ly d liu: d liu vo DataSet dataAdapter.Fill(dataset);
Phn trang

in d liu:
sqlDataAdapter.Fill(datasetname); sqlDataAdapter.Fill(datatable); sqlDataAdapter(DataSet dataset, int start, int end); sqlDataAdapter(int start, int end, DataTable datatable);

Cp nht d liu: Cp nht d liu t DataSet vo CSDL dataAdapter.Update(dataset);

69

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

70

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

DataView
Th hin ca 1 DataTable. ng vai tr quan trng trong DataBinding 1 DataTable c th c nhiu View khc nhau Dng trnh by d liu di hnh thc lc, sp xp, tm kim, hiu chnh v iu hng d liu trong DataTable
DataView DataView DataView

Khai bo DataView
C th s dng cc c php sau:
dataView = new DataView(); dataView.Table = dataTable; dataView = new DataView(dataTable); dataView = new DataView(dataTable, sortFilter, sortString, DataViewRowState);

DataTable 71
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

72

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

DataView
Danh sch cc thuc tnh
Tn AllowDelete AllowEdit AllowNew Item (index) RowFilter Sort Table Count
73

DataView
Danh sch cc phng thc
ngha Cho php xa trn th hin Cho php chnh sa Cho php thm mi Ly gi tr value ti column c ch s index Thit lp Expression dng lc row Sp xp tng hoc gim theo column Cho bit view ny c to bi table no Ly s lng mu tin c trong DataView
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Tn AddNew Delete(index)

ngha Thm mi 1 mu tin Xa mu tin th index

74

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

DataBinding
Hin th d liu trong DataTable, vo cc control (TextBox, ComboBox, DataGrid, ) Gm 2 loi: V d: in ni dung bng TonGiao vo ComboBox Tn Gio
DataTable bang = ketnoi.GetDataTable(SELECT * FROM TonGiao); cboTonGiao.DataSource = bang; cboTonGiao.DisplayMember = bang.Columns[1].ToString();//TenTG cboTonGiao.SelectedValue = bang.Columns[0].ToString();//MaTG
75
Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Simple Binding Complex Binding

76

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

a d liu vo li DataGridView

in d liu vo DataGridView
datagridview.DataSource = datatable; datagridview.DataSource = dataset.Tables[index];

77

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

78

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lp trnh Windows Form vi C#

Lp trnh Windows Form vi C#

DataGridView (tt)
Cc thuc tnh ca DataGridView:
DataSource: in d liu vo DataGridView CurrentRow: ly mu tin ang chn CurrentCell: ly ang chn

Cc bin c (Event) ca DataGridView:


SelectionChanged: xy ra khi ngi dng di chuyn vo vng d liu DoubleClick
79 82

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM

Lng Trn Hy Hin 2010 Khoa CNTT H S Phm TpHCM