際際滷

際際滷Share a Scribd company logo
L畉Y D畛 LI畛U
THEO CCH CONNECTED
Ch動董ng 2
2
N畛i dung
 C叩c b動畛c l畉y d畛 li畛u
 L畉y 1 gi叩 tr畛 董n
 L畉y t畉p gi叩 tr畛
 Truy v畉n t畉p gi叩 tr畛 v l動u l畉i
 Truy v畉n c坦 tham s畛
 G畛i Store Procedure
3
4
5
6
C叩c b動畛c l畉y d畛 li畛u
 3 b動畛c c董 b畉n
 B動畛c 1: Ch畛 r探 d畛 li畛u m 畛ng d畛ng c畉n
 Vi畉t y棚u c畉u b畉ng c但u l畛nh SQL
 SQL:
 Select
 DML, DDL, DCL
 B動畛c 2: G畛i y棚u c畉u 畉n CSDL
 T畉o 畛i t動畛ng Command
 B動畛c 3: Nh畉n k畉t qu畉
7
L畉y 1 gi叩 tr畛 董n
 M畛t s畛 c但u l畛nh SQL tr畉 v畛 gi叩 tr畛 董n
 T畉o 畛i t動畛ng Command
 Cung c畉p vn b畉n c但u l畛nh SQL cho Command
 Th畛c thi c但u l畛nh
8
L畉y 1 gi叩 tr畛 董n
M畛t s畛 c但u l畛nh SQL tr畉 v畛 gi叩 tr畛 董n
 Select min() From 
 Select max() From 
 Select avg() From 
 Select count() From 
 Select sum() From 
string strSQL =
"Select count(*)
From TenBang";
9
L畉y 1 gi叩 tr畛 董n
T畉o 畛i t動畛ng Command
 M畛t 畛i t動畛ng Command c畉n 3 th担ng tin
 畛i t動畛ng Connection
 Vn b畉n c但u l畛nh SQL
 Lo畉i c但u l畛nh: CommadType
 L畉y d畛 li畛u nguy棚n 1 B畉ng: CommadType.TableDirect
 C但u l畛nh SQL: CommadType.Text
 Stored Procedure: CommadType.StoredProcedure
10
L畉y 1 gi叩 tr畛 董n
T畉o 畛i t動畛ng Command
 C叩ch 1:
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = strSQL;
cmd.CommandType = CommandType.Text;
 C叩ch 2:
SqlCommand cmd = new SqlCommand(strSQL);
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
11
L畉y 1 gi叩 tr畛 董n
T畉o 畛i t動畛ng Command
 C叩ch 3:
SqlCommand cmd = new SqlCommand(strSQL, conn);
cmd.CommandType = CommandType.Text;
 C叩ch 4:
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = strSQL;
cmd.CommandType = CommandType.Text;
12
L畉y 1 gi叩 tr畛 董n
Th畛c thi c但u l畛nh
 Ph動董ng th畛c ExecuteScalar()
 ExecuteScalar tr畉 v畛 1 gi叩 tr畛 ki畛u object
 D湛ng 辿p ki畛u 畛 chuy畛n sang ki畛u d畛 li畛u
t動董ng 畛ng
 Ch炭 箪: Ph畉i m畛 k畉t n畛i tr動畛c khi g畛i l畛nh
ExecuteScalar
conn.Open();
kq = (Kieu)cmd.ExecuteScalar();
conn.Close();
13
L畉y t畉p gi叩 tr畛
 C但u l畛nh SQL tr畉 v畛 t畉p gi叩 tr畛
 Th畛c thi c但u l畛nh
 畛i t動畛ng IDataReader
 Thu畛c t鱈nh v ph動董ng th畛c c畛a IDataReader
14
L畉y t畉p gi叩 tr畛
C但u l畛nh SQL tr畉 v畛 t畉p gi叩 tr畛
 T畉p gi叩 tr畛: G畛m nhi畛u d嘆ng ho畉c nhi畛u c畛t
 Select c畛t1, c畛t2,  From 
string strSQL =
"Select cot1, 
From TenBang";
15
L畉y t畉p gi叩 tr畛
Th畛c thi c但u l畛nh
 Ph動董ng th畛c ExecuteReader()
 ExecuteScalar tr畉 v畛 1 畛i t動畛ng IDataReader
(SqlDataReader hay OleDBDataReader)
 IDataReader cho ph辿p ch炭ng ta duy畛t qua c叩c
d嘆ng trong t畉p d畛 li畛u
 Ch炭 箪:
 Ph畉i m畛 k畉t n畛i tr動畛c khi g畛i l畛nh ExecuteReader
 Kh担ng 動畛c 坦ng k畉t n畛i khi c嘆n ang s畛 d畛ng 畛i
t動畛ng IDataReader
conn.Open();
SqlDataReader reader=cmd.ExecuteReader();

conn.Close();
16
L畉y t畉p gi叩 tr畛
畛i t動畛ng IDataReader
 畉c i畛m c畛a 畛i t動畛ng IDataReader
 Ch畛a m畛t lu畛ng d畛 li畛u, t畉i m畛i th畛i i畛m ch畛
truy c畉p 1 d嘆ng
 Read-only, Forward-only
17
L畉y t畉p gi叩 tr畛
Thu畛c t鱈nh v ph動董ng th畛c c畛a IDataReader
 Get[DataType](int i): GetInt32(), GetString(), 
 T棚n 畉y 畛: ph畛 thu畛c vo ki畛u d畛 li畛u c畉n l畉y
 Tham s畛 c畛a hm: l s畛 th畛 t畛 c畛a c畛t (Ordinal) mu畛n
l畉y d畛 li畛u
 string GetName(int i)
 L畉y t棚n c畛t c坦 s畛 th畛 t畛 i
 int GetOrdinal(string name)
 L畉y s畛 th畛 t畛 c畛a c畛t c坦 t棚n name
 bool IsDbNull(int i)
 Ki畛m tra gi叩 tr畛 c畛t I c坦 l null/nothing hay kh担ng
18
L畉y t畉p gi叩 tr畛
Thu畛c t鱈nh v ph動董ng th畛c c畛a IDataReader
 object GetValue(int i)
 L畉y gi叩 tr畛 c畛t I m kh担ng quan t但m ki畛u d畛 li畛u
 Hai indexer c坦 ch畛c nng t動董ng t畛 ph動董ng
th畛c GetValue
 object dataReader[int i]
 object dataReader[string name]
 Ch炭 箪: N棚n d湛ng s畛 th畛 t畛 thay v狸 t棚n c畛t
19
L畉y t畉p gi叩 tr畛
Thu畛c t鱈nh v ph動董ng th畛c c畛a IDataReader
 bool HasRow
 true: cho bi畉t IDataReader ch畛a 1 hay nhi畛u
d嘆ng d畛 li畛u
 bool Read()
 畛c d嘆ng k畉 ti畉p
 Tr畉 v畛 true n畉u c嘆n 1 hay nhi畛u d嘆ng d畛 li畛u
20
L畉y t畉p gi叩 tr畛
Thu畛c t鱈nh v ph動董ng th畛c c畛a IDataReader
conn.Open();
SqlDataReader reader=cmd.ExecuteReader();
if (!reader.HasRows)

while (reader.Read())
{
value1 = reader.GetString(0);
value2 = reader.GetInt32(1);
}
conn.Close();
21
Truy v畉n t畉p gi叩 tr畛 v l動u l畉i
 i畛u ki畛n l動u d畛 li畛u vo b畛 nh畛
 Trong m畛t s畛 tr動畛ng h畛p c畉n l動u d畛 li畛u
 Data-binding
 C畉n nhi畛u th畛i gian 畛 x畛 l箪 d畛 li畛u
C畉n l動u d畛 li畛u l畉i 畛 x畛 l箪 sau
22
Truy v畉n t畉p gi叩 tr畛 v l動u l畉i
 D湛ng v嘆ng l畉p foreach
 畛 l畉p qua c叩c d嘆ng d畛 li畛u
 M畛i d嘆ng d畛 li畛u 動畛c g畛i l DbDataRecord
List<NhanVien> nhanVienList = new List<NhanVien>();
conn.Open();
SqlDataReader reader=cmd.ExecuteReader();
foreach (DbDataRecord rec in reader)
{
nhanVienList.Add(new NhanVien(rec));
}
conn.Close();
//S畛 d畛ng nhanVienList
23
TRUY V畉N C THAM S畛
24
Nhu c畉u c畉n c坦 truy v畉n c坦 tham s畛
 Ch炭ng ta th動畛ng vi畉t c叩c c但u l畛nh SQL 畛 t狸m
ki畉m c叩c d嘆ng d畛 li畛u trong CSDL theo m畛t i畛u
ki畛n no 坦. Nh畛ng gi叩 tr畛 trong i畛u ki畛n do
ng動畛i d湛ng nh畉p vo
 V鱈 d畛: T狸m ki畉m nh但n vi棚n c坦 trong CSDL kh担ng
畛 cho ph辿p ng nh畉p vo ch動董ng tr狸nh
string strUser, strPass;
strUser = txtUser.Text;
strPass = txtPass.Text;
string strSQL;
strSQL = "select * from NhanVien where " +
"username='" + strUser + "' and " +
"password='" + strPass + "'";
25
Nhu c畉u c畉n c坦 truy v畉n c坦 tham s畛
 Nh畉n x辿t
 B畉t k畛 nh畛ng g狸 nh畉p vo txtPass, txtUser 畛u
動畛c 畉t vo trong c但u SQL
 H達y nh畉p vo username: ' or ('1'='1') or '1'='1 hay
 H達y nh畉p vo password: ' or '1'='1
26
Nhu c畉u c畉n c坦 truy v畉n c坦 tham s畛
 Truy v畉n c坦 tham s畛
 Nh畛ng n董i trong c但u SQL c畉n d畛 li畛u ng動畛i
d湛ng, ch炭ng ta s畉 t畉o nh畛ng tham s畛 t畉i
nh畛ng n董i 坦 (g畛i l parameter placeholders)
 B畉t k畛 d畛 li畛u g狸 動畛c truy畛n vo tham s畛 s畉
動畛c 畛i x畛 nh動 l d畛 li畛u thu畉n t炭y (kh担ng
ph畉i l 1 c但u l畛nh SQL)
27
C叩c b動畛c t畉o truy v畉n c坦 tham s畛
 3 b動畛c t畉o truy v畉n c坦 tham s畛
 B動畛c 1: X但y d畛ng vn b畉n c但u SQL c坦 tham s畛
 B動畛c 2: Khai b叩o 畛i t動畛ng SqlParameter, g叩n
gi叩 tr畛 t動董ng 畛ng
 B動畛c 3: G叩n 畛i t動畛ng SqlParameter cho thu畛c
t鱈nh Parameters c畛a 畛i t動畛ng SqlCommand
28
C叩c b動畛c t畉o truy v畉n c坦 tham s畛
 B動畛c 1:X但y d畛ng vn b畉n c但u SQL c坦 tham s畛
 Nh畛ng n董i c畉n d畛 li畛u ng動畛i d湛ng ch炭ng ta t畉o
m畛t parameter placeholder
 T棚n parameter placeholder: @ten
strSQL = "select  where cot1 = @ten1 "
 V鱈 d畛:
 C炭 ph叩p:
strSQL = "select * from NhanVien where " +
manv=@user and matkhau=@pass";
Parameter
placeholder
29
C叩c b動畛c t畉o truy v畉n c坦 tham s畛
 B動畛c 2: Khai b叩o 畛i t動畛ng SqlParameter,
g叩n gi叩 tr畛 t動董ng 畛ng
 M畛i parameter placeholder t動董ng 畛ng v畛i m畛t
畛i t動畛ng SqlParameter
SqlParameter param;
param = new SqlParameter();
param = new SqlParameter(string paramName, object value);
param = new SqlParameter(string paramName, SqlDbType dbType);
param = new SqlParameter(string paramName, SqlDbType dbType, int size);
 C炭 ph叩p:
30
C叩c b動畛c t畉o truy v畉n c坦 tham s畛
 M畛t s畛 lo畉i ki畛u d畛 li畛u c畛a tham s畛: SqlDbType
 S畛
 TinyInt = Byte
 SmallInt = Int16
 Int = Int32
 BigInt = Int64
 Real = Single
 Float = Double
 Decimal = Decimal
 Money = Decimal
 Chu畛i
 Char = String
 NChar = String
 Text = String
 NText = String
 NVarChar = String
 Ki畛u kh叩c
 Bit = Boolean
 DateTime = DateTime
31
C叩c b動畛c t畉o truy v畉n c坦 tham s畛
 M畛t s畛 property c畛a SqlParameter
 string param.ParameterName
 SqlDbType param.SqlDbType
 object param.Value
 int param.Size
32
C叩c b動畛c t畉o truy v畉n c坦 tham s畛
 V鱈 d畛
SqlParameter paramMaNV, paramMatKhau;
paramMaNV = new SqlParameter();
paramMaNV.SqlDbType = SqlDbType.VarChar;
paramMaNV.ParameterName = "@user";
paramMaNV.Value = strMaNV;
paramMatKhau = new SqlParameter();
paramMatKhau.SqlDbType = SqlDbType.VarChar;
paramMatKhau.ParameterName = "@pass";
paramMatKhau.Value = strMatKhau;
33
C叩c b動畛c t畉o truy v畉n c坦 tham s畛
 B動畛c 3: G叩n 畛i t動畛ng SqlParameter cho
thu畛c t鱈nh Parameters c畛a 畛i t動畛ng
SqlCommand
SqlCommand cmd;
SqlParameter param;

cmd.Parameters.Add(param);
cmd.Parameters.Add(string paramName, SqlDbType dbType);
cmd.Parameters.Add(string paramName, SqlDbType dbType, int size);
34
G畛I STORED PROCEDURE
35
G畛i Stored Procedure
Kh叩i ni畛m Stored Procedure
 畛nh ngh挑a Stored Procedure  SP:
 SP l 1 th畛 t畛c 動畛c 畛nh ngh挑a tr動畛c v
動畛c l動u tr畛 trong CSDL 畛 ng動畛i d湛ng s畉n
sng d湛ng
 畉c i畛m c畛a SP
 SQL Server bi棚n d畛ch Stored Procedure
 G畛i SP ch畉y s畉 nhanh h董n vi畛c x但y d畛ng c但u
truy v畉n trong code
 SP b畉o m畉t h董n
36
G畛i Stored Procedure
T畉o Stored Procedure  SP
 C炭 ph叩p t畉o SP
CREATE PROCEDURE usp_TenSP
/*
(
@parameter1 datatype = defaultvalue,
@parameter2 datatype OUTPUT
)
*/
AS
/* SET NOCOUNT ON */
RETURN
37
G畛i Stored Procedure
G畛i SP kh担ng c坦 tham s畛
 2 b動畛c g畛i SP kh担ng tham s畛:
 B動畛c 1: Ch畛 r探 t棚n SP trong thu畛c t鱈nh
CommandText c畛a 畛i t動畛ng Command
SqlCommand cmd;

cmd.CommandText = usp_TenStoredProcedure
 B動畛c 2: Ch畛 cho 畛i t動畛ng Command bi畉t
ch炭ng ta ang mu畛n th畛c thi 1 Stored
Procedure
cmd.CommandType = CommandType.StoredProcedure
38
G畛i Stored Procedure
G畛i SP kh担ng c坦 tham s畛
 V鱈 d畛: create procedure usp_LayDSNhanVien
as
select * from NhanVien
return
string strSP = usp_LayDSNhanVien;
SqlCommand cmd = new SqlCommand(strSP, conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter daNhanVien = new SqlDataAdapter(cmd);
DataTable dtNhanVien = new DataTable();
daNhanVien.Fill(dtNhanVien);
// C叩ch 2
//conn.Open();
//SqlDataReader dr = cmd.ExecuteReader();
39
G畛i Stored Procedure
G畛i SP c坦 tham s畛 input (vo)
 3 b動畛c g畛i SP c坦 tham s畛 input:
 B動畛c 1, 2: gi畛ng nh動 tr動畛c
 B動畛c 3: Th棚m 畛i t動畛ng tham s畛
SqlParameter vo 畛i t動畛ng command (T棚n
tham s畛 gi畛ng t棚n tham s畛 trong SP)
cmd.Parameters.Add(@TenThamSo, kieu, kichthuoc);
cmd.Parameters[@TenThamSo].value = giatri;
40
G畛i Stored Procedure
G畛i SP c坦 tham s畛 input (vo)
 V鱈 d畛:
string sqlSP = "usp_TimNhanVienTheoTen";
conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand(sqlSP, conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Ten", SqlDbType.NVarChar, 20);
cmd.Parameters["@Ten"].Value=giatri;
create procedure usp_TimNhanVienTheoTen
(@ten nvarchar(20))
as
select * from NhanVien
where ten = @ten
return
41
G畛i Stored Procedure
G畛i SP c坦 tham s畛 output (ra)
 T動董ng t畛 SP c坦 tham s畛 input
 Nh動ng b動畛c 3 ph畉i ch畛 r探 tham s畛 no l
output b畉ng thu畛c t鱈nh Direction c畛a 畛i t動畛ng
SqlParameter
SqlParameter param;

param.Direction = ParameterDirection.Output
42
G畛i Stored Procedure
G畛i SP c坦 tham s畛 output (ra)
 V鱈 d畛:
create procedure usp_TimTenNhanVienTheoMa
(@manv nvarchar(20), @ten nvarchar(20) output)
as
select @ten=ten
from NhanVien
where manv=@manv
return
43
G畛i Stored Procedure
G畛i SP c坦 tham s畛 output (ra)
string sqlSP = "usp_TimTenNhanVienTheoMa";
conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand(sqlSP, conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter paramTen = new SqlParameter();
paramTen.Direction = ParameterDirection.Output;
paramTen.ParameterName ="@ten";
paramTen.SqlDbType = SqlDbType.NVarChar;
paramTen.Size = 20;
cmd.Parameters.Add(paramTen);
cmd.Parameters.Add("@MaNV", SqlDbType.NVarChar, 20);
cmd.Parameters["@MaNV"].Value = "NV001";
conn.Open();
cmd.ExecuteReader();
conn.Close();
string ten = cmd.Parameters["@ten"].Value.ToString();
44
T坦m t畉t ch動董ng 2

More Related Content

02 chuong 2 - lay du lieu theo cach connected

  • 1. L畉Y D畛 LI畛U THEO CCH CONNECTED Ch動董ng 2
  • 2. 2 N畛i dung C叩c b動畛c l畉y d畛 li畛u L畉y 1 gi叩 tr畛 董n L畉y t畉p gi叩 tr畛 Truy v畉n t畉p gi叩 tr畛 v l動u l畉i Truy v畉n c坦 tham s畛 G畛i Store Procedure
  • 3. 3
  • 4. 4
  • 5. 5
  • 6. 6 C叩c b動畛c l畉y d畛 li畛u 3 b動畛c c董 b畉n B動畛c 1: Ch畛 r探 d畛 li畛u m 畛ng d畛ng c畉n Vi畉t y棚u c畉u b畉ng c但u l畛nh SQL SQL: Select DML, DDL, DCL B動畛c 2: G畛i y棚u c畉u 畉n CSDL T畉o 畛i t動畛ng Command B動畛c 3: Nh畉n k畉t qu畉
  • 7. 7 L畉y 1 gi叩 tr畛 董n M畛t s畛 c但u l畛nh SQL tr畉 v畛 gi叩 tr畛 董n T畉o 畛i t動畛ng Command Cung c畉p vn b畉n c但u l畛nh SQL cho Command Th畛c thi c但u l畛nh
  • 8. 8 L畉y 1 gi叩 tr畛 董n M畛t s畛 c但u l畛nh SQL tr畉 v畛 gi叩 tr畛 董n Select min() From Select max() From Select avg() From Select count() From Select sum() From string strSQL = "Select count(*) From TenBang";
  • 9. 9 L畉y 1 gi叩 tr畛 董n T畉o 畛i t動畛ng Command M畛t 畛i t動畛ng Command c畉n 3 th担ng tin 畛i t動畛ng Connection Vn b畉n c但u l畛nh SQL Lo畉i c但u l畛nh: CommadType L畉y d畛 li畛u nguy棚n 1 B畉ng: CommadType.TableDirect C但u l畛nh SQL: CommadType.Text Stored Procedure: CommadType.StoredProcedure
  • 10. 10 L畉y 1 gi叩 tr畛 董n T畉o 畛i t動畛ng Command C叩ch 1: SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = strSQL; cmd.CommandType = CommandType.Text; C叩ch 2: SqlCommand cmd = new SqlCommand(strSQL); cmd.Connection = conn; cmd.CommandType = CommandType.Text;
  • 11. 11 L畉y 1 gi叩 tr畛 董n T畉o 畛i t動畛ng Command C叩ch 3: SqlCommand cmd = new SqlCommand(strSQL, conn); cmd.CommandType = CommandType.Text; C叩ch 4: SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = strSQL; cmd.CommandType = CommandType.Text;
  • 12. 12 L畉y 1 gi叩 tr畛 董n Th畛c thi c但u l畛nh Ph動董ng th畛c ExecuteScalar() ExecuteScalar tr畉 v畛 1 gi叩 tr畛 ki畛u object D湛ng 辿p ki畛u 畛 chuy畛n sang ki畛u d畛 li畛u t動董ng 畛ng Ch炭 箪: Ph畉i m畛 k畉t n畛i tr動畛c khi g畛i l畛nh ExecuteScalar conn.Open(); kq = (Kieu)cmd.ExecuteScalar(); conn.Close();
  • 13. 13 L畉y t畉p gi叩 tr畛 C但u l畛nh SQL tr畉 v畛 t畉p gi叩 tr畛 Th畛c thi c但u l畛nh 畛i t動畛ng IDataReader Thu畛c t鱈nh v ph動董ng th畛c c畛a IDataReader
  • 14. 14 L畉y t畉p gi叩 tr畛 C但u l畛nh SQL tr畉 v畛 t畉p gi叩 tr畛 T畉p gi叩 tr畛: G畛m nhi畛u d嘆ng ho畉c nhi畛u c畛t Select c畛t1, c畛t2, From string strSQL = "Select cot1, From TenBang";
  • 15. 15 L畉y t畉p gi叩 tr畛 Th畛c thi c但u l畛nh Ph動董ng th畛c ExecuteReader() ExecuteScalar tr畉 v畛 1 畛i t動畛ng IDataReader (SqlDataReader hay OleDBDataReader) IDataReader cho ph辿p ch炭ng ta duy畛t qua c叩c d嘆ng trong t畉p d畛 li畛u Ch炭 箪: Ph畉i m畛 k畉t n畛i tr動畛c khi g畛i l畛nh ExecuteReader Kh担ng 動畛c 坦ng k畉t n畛i khi c嘆n ang s畛 d畛ng 畛i t動畛ng IDataReader conn.Open(); SqlDataReader reader=cmd.ExecuteReader(); conn.Close();
  • 16. 16 L畉y t畉p gi叩 tr畛 畛i t動畛ng IDataReader 畉c i畛m c畛a 畛i t動畛ng IDataReader Ch畛a m畛t lu畛ng d畛 li畛u, t畉i m畛i th畛i i畛m ch畛 truy c畉p 1 d嘆ng Read-only, Forward-only
  • 17. 17 L畉y t畉p gi叩 tr畛 Thu畛c t鱈nh v ph動董ng th畛c c畛a IDataReader Get[DataType](int i): GetInt32(), GetString(), T棚n 畉y 畛: ph畛 thu畛c vo ki畛u d畛 li畛u c畉n l畉y Tham s畛 c畛a hm: l s畛 th畛 t畛 c畛a c畛t (Ordinal) mu畛n l畉y d畛 li畛u string GetName(int i) L畉y t棚n c畛t c坦 s畛 th畛 t畛 i int GetOrdinal(string name) L畉y s畛 th畛 t畛 c畛a c畛t c坦 t棚n name bool IsDbNull(int i) Ki畛m tra gi叩 tr畛 c畛t I c坦 l null/nothing hay kh担ng
  • 18. 18 L畉y t畉p gi叩 tr畛 Thu畛c t鱈nh v ph動董ng th畛c c畛a IDataReader object GetValue(int i) L畉y gi叩 tr畛 c畛t I m kh担ng quan t但m ki畛u d畛 li畛u Hai indexer c坦 ch畛c nng t動董ng t畛 ph動董ng th畛c GetValue object dataReader[int i] object dataReader[string name] Ch炭 箪: N棚n d湛ng s畛 th畛 t畛 thay v狸 t棚n c畛t
  • 19. 19 L畉y t畉p gi叩 tr畛 Thu畛c t鱈nh v ph動董ng th畛c c畛a IDataReader bool HasRow true: cho bi畉t IDataReader ch畛a 1 hay nhi畛u d嘆ng d畛 li畛u bool Read() 畛c d嘆ng k畉 ti畉p Tr畉 v畛 true n畉u c嘆n 1 hay nhi畛u d嘆ng d畛 li畛u
  • 20. 20 L畉y t畉p gi叩 tr畛 Thu畛c t鱈nh v ph動董ng th畛c c畛a IDataReader conn.Open(); SqlDataReader reader=cmd.ExecuteReader(); if (!reader.HasRows) while (reader.Read()) { value1 = reader.GetString(0); value2 = reader.GetInt32(1); } conn.Close();
  • 21. 21 Truy v畉n t畉p gi叩 tr畛 v l動u l畉i i畛u ki畛n l動u d畛 li畛u vo b畛 nh畛 Trong m畛t s畛 tr動畛ng h畛p c畉n l動u d畛 li畛u Data-binding C畉n nhi畛u th畛i gian 畛 x畛 l箪 d畛 li畛u C畉n l動u d畛 li畛u l畉i 畛 x畛 l箪 sau
  • 22. 22 Truy v畉n t畉p gi叩 tr畛 v l動u l畉i D湛ng v嘆ng l畉p foreach 畛 l畉p qua c叩c d嘆ng d畛 li畛u M畛i d嘆ng d畛 li畛u 動畛c g畛i l DbDataRecord List<NhanVien> nhanVienList = new List<NhanVien>(); conn.Open(); SqlDataReader reader=cmd.ExecuteReader(); foreach (DbDataRecord rec in reader) { nhanVienList.Add(new NhanVien(rec)); } conn.Close(); //S畛 d畛ng nhanVienList
  • 23. 23 TRUY V畉N C THAM S畛
  • 24. 24 Nhu c畉u c畉n c坦 truy v畉n c坦 tham s畛 Ch炭ng ta th動畛ng vi畉t c叩c c但u l畛nh SQL 畛 t狸m ki畉m c叩c d嘆ng d畛 li畛u trong CSDL theo m畛t i畛u ki畛n no 坦. Nh畛ng gi叩 tr畛 trong i畛u ki畛n do ng動畛i d湛ng nh畉p vo V鱈 d畛: T狸m ki畉m nh但n vi棚n c坦 trong CSDL kh担ng 畛 cho ph辿p ng nh畉p vo ch動董ng tr狸nh string strUser, strPass; strUser = txtUser.Text; strPass = txtPass.Text; string strSQL; strSQL = "select * from NhanVien where " + "username='" + strUser + "' and " + "password='" + strPass + "'";
  • 25. 25 Nhu c畉u c畉n c坦 truy v畉n c坦 tham s畛 Nh畉n x辿t B畉t k畛 nh畛ng g狸 nh畉p vo txtPass, txtUser 畛u 動畛c 畉t vo trong c但u SQL H達y nh畉p vo username: ' or ('1'='1') or '1'='1 hay H達y nh畉p vo password: ' or '1'='1
  • 26. 26 Nhu c畉u c畉n c坦 truy v畉n c坦 tham s畛 Truy v畉n c坦 tham s畛 Nh畛ng n董i trong c但u SQL c畉n d畛 li畛u ng動畛i d湛ng, ch炭ng ta s畉 t畉o nh畛ng tham s畛 t畉i nh畛ng n董i 坦 (g畛i l parameter placeholders) B畉t k畛 d畛 li畛u g狸 動畛c truy畛n vo tham s畛 s畉 動畛c 畛i x畛 nh動 l d畛 li畛u thu畉n t炭y (kh担ng ph畉i l 1 c但u l畛nh SQL)
  • 27. 27 C叩c b動畛c t畉o truy v畉n c坦 tham s畛 3 b動畛c t畉o truy v畉n c坦 tham s畛 B動畛c 1: X但y d畛ng vn b畉n c但u SQL c坦 tham s畛 B動畛c 2: Khai b叩o 畛i t動畛ng SqlParameter, g叩n gi叩 tr畛 t動董ng 畛ng B動畛c 3: G叩n 畛i t動畛ng SqlParameter cho thu畛c t鱈nh Parameters c畛a 畛i t動畛ng SqlCommand
  • 28. 28 C叩c b動畛c t畉o truy v畉n c坦 tham s畛 B動畛c 1:X但y d畛ng vn b畉n c但u SQL c坦 tham s畛 Nh畛ng n董i c畉n d畛 li畛u ng動畛i d湛ng ch炭ng ta t畉o m畛t parameter placeholder T棚n parameter placeholder: @ten strSQL = "select where cot1 = @ten1 " V鱈 d畛: C炭 ph叩p: strSQL = "select * from NhanVien where " + manv=@user and matkhau=@pass"; Parameter placeholder
  • 29. 29 C叩c b動畛c t畉o truy v畉n c坦 tham s畛 B動畛c 2: Khai b叩o 畛i t動畛ng SqlParameter, g叩n gi叩 tr畛 t動董ng 畛ng M畛i parameter placeholder t動董ng 畛ng v畛i m畛t 畛i t動畛ng SqlParameter SqlParameter param; param = new SqlParameter(); param = new SqlParameter(string paramName, object value); param = new SqlParameter(string paramName, SqlDbType dbType); param = new SqlParameter(string paramName, SqlDbType dbType, int size); C炭 ph叩p:
  • 30. 30 C叩c b動畛c t畉o truy v畉n c坦 tham s畛 M畛t s畛 lo畉i ki畛u d畛 li畛u c畛a tham s畛: SqlDbType S畛 TinyInt = Byte SmallInt = Int16 Int = Int32 BigInt = Int64 Real = Single Float = Double Decimal = Decimal Money = Decimal Chu畛i Char = String NChar = String Text = String NText = String NVarChar = String Ki畛u kh叩c Bit = Boolean DateTime = DateTime
  • 31. 31 C叩c b動畛c t畉o truy v畉n c坦 tham s畛 M畛t s畛 property c畛a SqlParameter string param.ParameterName SqlDbType param.SqlDbType object param.Value int param.Size
  • 32. 32 C叩c b動畛c t畉o truy v畉n c坦 tham s畛 V鱈 d畛 SqlParameter paramMaNV, paramMatKhau; paramMaNV = new SqlParameter(); paramMaNV.SqlDbType = SqlDbType.VarChar; paramMaNV.ParameterName = "@user"; paramMaNV.Value = strMaNV; paramMatKhau = new SqlParameter(); paramMatKhau.SqlDbType = SqlDbType.VarChar; paramMatKhau.ParameterName = "@pass"; paramMatKhau.Value = strMatKhau;
  • 33. 33 C叩c b動畛c t畉o truy v畉n c坦 tham s畛 B動畛c 3: G叩n 畛i t動畛ng SqlParameter cho thu畛c t鱈nh Parameters c畛a 畛i t動畛ng SqlCommand SqlCommand cmd; SqlParameter param; cmd.Parameters.Add(param); cmd.Parameters.Add(string paramName, SqlDbType dbType); cmd.Parameters.Add(string paramName, SqlDbType dbType, int size);
  • 35. 35 G畛i Stored Procedure Kh叩i ni畛m Stored Procedure 畛nh ngh挑a Stored Procedure SP: SP l 1 th畛 t畛c 動畛c 畛nh ngh挑a tr動畛c v 動畛c l動u tr畛 trong CSDL 畛 ng動畛i d湛ng s畉n sng d湛ng 畉c i畛m c畛a SP SQL Server bi棚n d畛ch Stored Procedure G畛i SP ch畉y s畉 nhanh h董n vi畛c x但y d畛ng c但u truy v畉n trong code SP b畉o m畉t h董n
  • 36. 36 G畛i Stored Procedure T畉o Stored Procedure SP C炭 ph叩p t畉o SP CREATE PROCEDURE usp_TenSP /* ( @parameter1 datatype = defaultvalue, @parameter2 datatype OUTPUT ) */ AS /* SET NOCOUNT ON */ RETURN
  • 37. 37 G畛i Stored Procedure G畛i SP kh担ng c坦 tham s畛 2 b動畛c g畛i SP kh担ng tham s畛: B動畛c 1: Ch畛 r探 t棚n SP trong thu畛c t鱈nh CommandText c畛a 畛i t動畛ng Command SqlCommand cmd; cmd.CommandText = usp_TenStoredProcedure B動畛c 2: Ch畛 cho 畛i t動畛ng Command bi畉t ch炭ng ta ang mu畛n th畛c thi 1 Stored Procedure cmd.CommandType = CommandType.StoredProcedure
  • 38. 38 G畛i Stored Procedure G畛i SP kh担ng c坦 tham s畛 V鱈 d畛: create procedure usp_LayDSNhanVien as select * from NhanVien return string strSP = usp_LayDSNhanVien; SqlCommand cmd = new SqlCommand(strSP, conn); cmd.CommandType = CommandType.StoredProcedure; SqlDataAdapter daNhanVien = new SqlDataAdapter(cmd); DataTable dtNhanVien = new DataTable(); daNhanVien.Fill(dtNhanVien); // C叩ch 2 //conn.Open(); //SqlDataReader dr = cmd.ExecuteReader();
  • 39. 39 G畛i Stored Procedure G畛i SP c坦 tham s畛 input (vo) 3 b動畛c g畛i SP c坦 tham s畛 input: B動畛c 1, 2: gi畛ng nh動 tr動畛c B動畛c 3: Th棚m 畛i t動畛ng tham s畛 SqlParameter vo 畛i t動畛ng command (T棚n tham s畛 gi畛ng t棚n tham s畛 trong SP) cmd.Parameters.Add(@TenThamSo, kieu, kichthuoc); cmd.Parameters[@TenThamSo].value = giatri;
  • 40. 40 G畛i Stored Procedure G畛i SP c坦 tham s畛 input (vo) V鱈 d畛: string sqlSP = "usp_TimNhanVienTheoTen"; conn = new SqlConnection(strConn); SqlCommand cmd = new SqlCommand(sqlSP, conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@Ten", SqlDbType.NVarChar, 20); cmd.Parameters["@Ten"].Value=giatri; create procedure usp_TimNhanVienTheoTen (@ten nvarchar(20)) as select * from NhanVien where ten = @ten return
  • 41. 41 G畛i Stored Procedure G畛i SP c坦 tham s畛 output (ra) T動董ng t畛 SP c坦 tham s畛 input Nh動ng b動畛c 3 ph畉i ch畛 r探 tham s畛 no l output b畉ng thu畛c t鱈nh Direction c畛a 畛i t動畛ng SqlParameter SqlParameter param; param.Direction = ParameterDirection.Output
  • 42. 42 G畛i Stored Procedure G畛i SP c坦 tham s畛 output (ra) V鱈 d畛: create procedure usp_TimTenNhanVienTheoMa (@manv nvarchar(20), @ten nvarchar(20) output) as select @ten=ten from NhanVien where manv=@manv return
  • 43. 43 G畛i Stored Procedure G畛i SP c坦 tham s畛 output (ra) string sqlSP = "usp_TimTenNhanVienTheoMa"; conn = new SqlConnection(strConn); SqlCommand cmd = new SqlCommand(sqlSP, conn); cmd.CommandType = CommandType.StoredProcedure; SqlParameter paramTen = new SqlParameter(); paramTen.Direction = ParameterDirection.Output; paramTen.ParameterName ="@ten"; paramTen.SqlDbType = SqlDbType.NVarChar; paramTen.Size = 20; cmd.Parameters.Add(paramTen); cmd.Parameters.Add("@MaNV", SqlDbType.NVarChar, 20); cmd.Parameters["@MaNV"].Value = "NV001"; conn.Open(); cmd.ExecuteReader(); conn.Close(); string ten = cmd.Parameters["@ten"].Value.ToString();

Editor's Notes

  • #7: Data Manipulation Language Data Definition Language
  • #9: Kiem tra xem select kh坦a chinh c坦 tra ve 1 gi叩 tr畛 hay kh担ng Select 1cot From bang Where cot_khoa_chinh = giaitr Select 1cot From bang Where cot_duynhat = giaitr
  • #10: OleDbCommand1.CommandText = &amp;quot;authors&amp;quot;; OleDbCommand1.CommandType = CommandType.TableDirect; OleDbConnection1.Open(); dreader = OleDbCommand1.ExecuteReader();
  • #18: Truy c畉p theo so thu tu se nhanh hon ten cot
  • #19: Truy c畉p theo so thu tu se nhanh hon ten cot Dinh dang d畛 li畛u String.Format({0} {1}, );
  • #23: using System.Data.Common;
  • #31: BigIntInt64. A 64-bit signed integer. BinaryArray of type Byte. A fixed-length stream of binary data ranging between 1 and 8,000 bytes. BitBoolean. An unsigned numeric value that can be 0, 1, or nullNothingnullptra null reference (Nothing in Visual Basic). CharString. A fixed-length stream of non-Unicode characters ranging between 1 and 8,000 characters. DateTimeDateTime. Date and time data ranging in value from January 1, 1753 to December 31, 9999 to an accuracy of 3.33 milliseconds. DecimalDecimal. A fixed precision and scale numeric value between -10 38 -1 and 10 38 -1. FloatDouble. A floating point number within the range of -1.79E +308 through 1.79E +308. ImageArray of type Byte. A variable-length stream of binary data ranging from 0 to 2 31 -1 (or 2,147,483,647) bytes. IntInt32. A 32-bit signed integer. MoneyDecimal. A currency value ranging from -2 63 (or -9,223,372,036,854,775,808) to 2 63 -1 (or +9,223,372,036,854,775,807) with an accuracy to a ten-thousandth of a currency unit. NCharString. A fixed-length stream of Unicode characters ranging between 1 and 4,000 characters. NTextString. A variable-length stream of Unicode data with a maximum length of 2 30 - 1 (or 1,073,741,823) characters. NVarCharString. A variable-length stream of Unicode characters ranging between 1 and 4,000 characters. Implicit conversion fails if the string is greater than 4,000 characters. Explicitly set the object when working with strings longer than 4,000 characters. RealSingle. A floating point number within the range of -3.40E +38 through 3.40E +38. UniqueIdentifierGuid. A globally unique identifier (or GUID). SmallDateTimeDateTime. Date and time data ranging in value from January 1, 1900 to June 6, 2079 to an accuracy of one minute. SmallIntInt16. A 16-bit signed integer. SmallMoneyDecimal. A currency value ranging from -214,748.3648 to +214,748.3647 with an accuracy to a ten-thousandth of a currency unit. TextString. A variable-length stream of non-Unicode data with a maximum length of 2 31 -1 (or 2,147,483,647) characters. TimestampArray of type Byte. Automatically generated binary numbers, which are guaranteed to be unique within a database. timestamp is used typically as a mechanism for version-stamping table rows. The storage size is 8 bytes. TinyIntByte. An 8-bit unsigned integer. VarBinaryArray of type Byte. A variable-length stream of binary data ranging between 1 and 8,000 bytes. Implicit conversion fails if the byte array is greater than 8,000 bytes. Explicitly set the object when working with byte arrays larger than 8,000 bytes. VarCharString. A variable-length stream of non-Unicode characters ranging between 1 and 8,000 characters. VariantObject. A special data type that can contain numeric, string, binary, or date data as well as the SQL Server values Empty and Null, which is assumed if no other type is declared. XmlAn XML value. Obtain the XML as a string using the GetValue method or Value property, or as an XmlReader by calling the CreateReader method. UdtA SQL Server 2005 user-defined type (UDT). StructuredA special data type for specifying structured data contained in table-valued parameters. DateDate data ranging in value from January 1,1 AD through December 31, 9999 AD. TimeTime data based on a 24-hour clock. Time value range is 00:00:00 through 23:59:59.9999999 with an accuracy of 100 nanoseconds. DateTime2Date and time data. Date value range is from January 1,1 AD through December 31, 9999 AD. Time value range is 00:00:00 through 23:59:59.9999999 with an accuracy of 100 nanoseconds. DateTimeOffsetDate and time data with time zone awareness. Date value range is from January 1,1 AD through December 31, 9999 AD. Time value range is 00:00:00 through 23:59:59.9999999 with an accuracy of 100 nanoseconds. Time zone value range is -14:00 through +14:00.
  • #38: Ten sp ko phan biet chu hoa hay chu thuong