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();