Nếu như SqlDataReader đọc từng hàng dữ liệu thì SqlDataAdapter đọc toàn bộ dữ liệu một lần. Dữ liệu nhận được sẽ được lưu trữ trong DataTable hay DataSet. DataTable giống như các bảng, DataSet giống như một sơ sở dữ liệu. Một DataSet có thể chứa nhiều DataTable.
Đoạn mã sau dùng đối tượng SqlDataAdapter để đọc dữ liệu từ bảng Khoa của cơ sở dữ liệu QuanLySinhVien và lưu trữ dữ liệu này trong một đối tượng DataTable bằng phương thức Fill:
try { DataTable khoa = new DataTable(); string connectionString = ConfigurationManager.ConnectionStrings["QLSV"].ConnectionString; using (SqlConnection connection = new SqlConnection(connectionString)) using (SqlCommand command = new SqlCommand("SELECT MaKhoa, TenKhoa FROM Khoa;", connection)) { using (SqlDataAdapter adapter = new SqlDataAdapter(command)) { adapter.Fill(khoa); } } Console.WriteLine("Du lieu tu bang Khoa:"); foreach (DataRow r in khoa.Rows) { string sFormat = String.Format("Ma Khoa:{0} Ten Khoa: {1}", r["MaKhoa"], r["TenKhoa"]); Console.WriteLine(sFormat); } } catch (Exception ex) { Console.WriteLine("Loi khi mo ket noi:" + ex.Message); }
Thêm hàng mới đến DataTable và cập nhật đến cơ sở dữ liệu:
try { DataTable khoa = new DataTable(); string connectionString = ConfigurationManager.ConnectionStrings["QLSV"].ConnectionString; using (SqlConnection connection = new SqlConnection(connectionString)) using (SqlCommand command = new SqlCommand("SELECT*FROM Khoa", connection)) using (SqlDataAdapter adapter = new SqlDataAdapter(command)) using (SqlCommandBuilder builder = new SqlCommandBuilder(adapter)) { adapter.FillSchema(khoa, SchemaType.Source); adapter.Fill(khoa); DataRow khoamoi = khoa.NewRow(); khoamoi["MaKhoa"] = "MH09"; khoamoi["TenKhoa"] = "Cong nghe Sinh Hoc"; khoa.Rows.Add(khoamoi); adapter.Update(khoa); } Console.WriteLine("Them du lieu thanh cong!"); } catch (Exception ex) { Console.WriteLine("Loi khi mo ket noi:" + ex.Message); }
Thay đổi nội dung của một hàng đã tồn tại trong DataTable và cập nhật đến cơ sở dữ liệu:
try { DataTable khoa = new DataTable(); string connectionString = ConfigurationManager.ConnectionStrings["QLSV"].ConnectionString; using (SqlConnection connection = new SqlConnection(connectionString)) using (SqlCommand command = new SqlCommand("SELECT*FROM Khoa", connection)) using (SqlDataAdapter adapter = new SqlDataAdapter(command)) using (SqlCommandBuilder builder = new SqlCommandBuilder(adapter)) { adapter.FillSchema(khoa, SchemaType.Source); adapter.Fill(khoa); DataRow[] dt = khoa.Select("MaKhoa = 'KH07'"); dt[0]["TenKhoa"] = "Cat got kim loai"; adapter.Update(dt); } Console.WriteLine("Cap nhat du lieu thanh cong!"); } catch (Exception ex) { Console.WriteLine("Loi khi mo ket noi:" + ex.Message); }
Xoá một hàng trong DataTable và cập nhật đến cơ sở dữ liệu:
try { DataTable khoa = new DataTable(); string connectionString = ConfigurationManager.ConnectionStrings["QLSV"].ConnectionString; using (SqlConnection connection = new SqlConnection(connectionString)) using (SqlCommand command = new SqlCommand("SELECT*FROM Khoa", connection)) using (SqlDataAdapter adapter = new SqlDataAdapter(command)) using (SqlCommandBuilder builder = new SqlCommandBuilder(adapter)) { adapter.FillSchema(khoa, SchemaType.Source); adapter.Fill(khoa); DataRow[] dt = khoa.Select("MaKhoa = 'MH09'"); dt[0].Delete(); adapter.Update(dt); } Console.WriteLine("Xoa du lieu thanh cong!"); } catch (Exception ex) { Console.WriteLine("Loi khi mo ket noi:" + ex.Message); }