CÁC GIẢI PHÁP LẬP TRÌNH C# - Trang 481

481

Chương 12: Dịch vụ Web XML và Remoting

return names;

}

private DataSet GetCustomerDataSet() {

// Kiểm tra item đã có trong cache chưa.

DataSet ds = HttpContext.Current.Cache["Products"] as DataSet;

if (ds == null) {

string SQL = "SELECT * FROM Products";

// Tạo các đối tượng ADO.NET.

SqlConnection con = new SqlConnection(connectionString);

SqlCommand com = new SqlCommand(SQL, con);

SqlDataAdapter adapter = new SqlDataAdapter(com);

ds = new DataSet();

// Thực thi câu truy vấn.

try {

con.Open();

adapter.Fill(ds, "Products");

// Lưu item vào cache (trong 60 giây).

HttpContext.Current.Cache.Insert("Products", ds, null,

DateTime.Now.AddSeconds(60), TimeSpan.Zero);

} catch (Exception err) {

System.Diagnostics.Debug.WriteLine(err.ToString());

} finally {

con.Close();

}

}

return ds;

}

}