
ADO.NET
Đi t ng _Commandố ượ

_Command
2

_Command: select
// Kh i tạo với 2 tham s : câu ở ố
truy v n và k t n iấ ế ố
SqlCommand cmd = new
SqlCommand("select CategoryName
from Categories", conn);
// Dùng phương thức
ExecuteReader đ l y k t quể ấ ế ả
SqlDataReader rdr =
cmd.ExecuteReader();

_Command: select
qKhi cần một giá trị đơn như đếm, tổng,
trung bình, ...
§Thực thi ExecuteReader() rồi tính toán kết
quả
§Thực thi ExecuteScalar() để database trả về
kết quả
// Kh i tạoở
SqlCommand cmd = new
SqlCommand("select count(*) from
Categories", conn);

_Command: insert
string insertString = @"insert
into DANHMUC(tendm) values (‘Mỹ
ph m')"ẩ;
// Kh i tạoở
SqlCommand cmd = new
SqlCommand(insertString, conn);
// Thực thi phương thức
ExecuteNonQuery
cmd.ExecuteNonQuery();