Bài giảng Lập trình C# - Hồ Văn Lâm

.NET Platform là một framework mới, cung cấp các API (Application Programming Interface)

.NET Platform cho phép các công nghệ khác nhau chạy trên nó như:

COM+, ASP, XML

Các giao thức mới như SOAP, WSDL

Tập trung vào Internet.

 

ppt224 trang | Chuyên mục: Visual C# | Chia sẻ: dkS00TYs | Lượt xem: 3851 | Lượt tải: 5download
Tóm tắt nội dung Bài giảng Lập trình C# - Hồ Văn Lâm, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
 danh sách thư mục TP và các tệp Chương 5  Windows Applications - ListView Các thuộc tính thường dùng CheckBoxes: Có/không xuất hiện các checkbox trên các dòng dữ liệu (ngầm định là False) Columns: Các cột hiển thị trong chế độ Details FullRowSelect: Indicates whether all SubItems are hightlighted along with the Item when selected. GridLines: Hiển thị lưới (chỉ hiển thị trong chế độ Details). Chương 5 Windows Applications - ListView Các thuộc tính thường dùng Items: Mảng các dòng (ListViewItems) trong ListView. LargeImageList: Danh sách ảnh (ImageList) hiển thị trên ListView. SmallImageList: Danh sách ảnh (ImageList) hiển thị trên ListView. MultiSelect: Có/Không cho phép chọn nhiều dòng (ngầm định là True). SelectedItems: Mảng các dòng được chọn. Chương 5  Windows Applications - ListView Các phương thức thường dùng Add: Thêm một dòng vào ListView Clear: Xoá tất cả các dòng của ListView Remove: Xoá một dòng trong ListView RemoveAt(index): Xoá một dòng ở vị trí index Sự kiện thường dùng ItemSelectionChanged: Xảy ra khi chọn một dòng. Chương 5  Windows Applications - ListView Các thuộc tính thường dùng View: Kiểu hiện thị của ListView Icons: Hiển thị danh sách theo các biểu tượng List: Hiển thị danh sách theo một cột Details: Hiển thị ListView theo danh sách nhiều cột Chương 5  Windows Applications – TabControl Tạo ra các cửa sổ Tab Mỗi cửa sổ Tab gọi là một TabPage TabPages có thể chứa các điều khiển Chương 5 Windows Applications – TabControl Thêm và xóa các TabPage. Chương 5  Windows Applications – TabControl Ví dụ: tạo TabControl như sau: Chương 6  Truy cập cơ sở dữ liệu với .NET – ADO.Net ADO.NET là công nghệ truy nhập dữ liệu có cấu trúc Cung cấp giao diện hướng đối tượng hợp nhất (Uniform object oriented) cho các dữ liệu khác nhau Cơ sở dữ liệu quan hệ XML Các dữ liệu khác Được thiết kế cho các ứng dụng phân tán và Web Chương 6  Truy cập cơ sở dữ liệu với .NET ADO.NET = ActiveX Data Objects Các đối tượng ADO.NET chứa trong không gian tên System.Data. Các đối tượng ADO.NET chia thành 2 loại: Connected: Các đối tượng truyền thông trực tiếp với cơ sở dữ liệu. Disconnected: Các đối tượng không truyền thông trực tiếp với cơ sở dữ liệu. Chương 6  Truy cập cơ sở dữ liệu với .NET Chương 6  Truy cập cơ sở dữ liệu với .NET Chương 6  Truy cập cơ sở dữ liệu với .NET Chương 6  Truy cập cơ sở dữ liệu với .NET DataTable Có thể ánh xạ một bảng vật lý với DataTable Một DataTable là một mảng 2 chiều gồm các dòng và các cột Một số thuộc tính Columns: Các cột dữ liệu của DataTable Count: Số cột trong DataTable Rows: Các dòng dữ liệu của DataTable Count: Số dòng trong DataTable Chương 6  Truy cập cơ sở dữ liệu với .NET Data Table Tạo một DataTable trong bộ nhớ DataTable myTable = new DataTable(); myTable.Columns.Add(“MaKhoa”, typeof(string)); myTable.Columns.Add(“TenKhoa”, typeof(string)); Chương 6  Truy cập cơ sở dữ liệu với .NET ADO.NET tổ chức thành mô hình đối tượng System.Data: Các lớp của ADO.NET System.Data.OleDb: Các lớp làm việc với dữ liệu OLE DB System.Data.SqlClient: Các lớp làm việc với cở sở dữ liệu SQL Server Chương 6  Truy cập cơ sở dữ liệu với .NET ADO.NET Data Providers Là các lớp truy nhập dữ liệu nguồn Microsoft SQL Server™ 2000, SQL Server 2005	 Oracle Microsoft Access Thiết lập kết nối giữa DataSetsvà dữ liệu nguồn Có 2 thư viện ADO.NET Data Providers System.Data.OleDb: Dùng truy nhập cơ sở dữ liệu OLE System.Data.SqlClient: Truy nhập SQL Server Chương 6  Truy cập cơ sở dữ liệu với .NET ADO.NET Data Providers Chương 6  Truy cập cơ sở dữ liệu với .NET Đối tượng Connection Biểu diển kết nối tới cơ sở dữ liệu //Ket noi toi co so du lieu MS Access 	string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + 	"Data Source="; 	OleDbConnection myConnection = new OleDbConnection(conStr); 	myConnection.Open(); //Ket noi toi co so du lieu SQL Server 	string conStr = “Data Source = ;“ + 	“Persist Security Info = true;“ + 	“Initial Catalog = ;“ + 	“User Id =name; Password=psw;” + 	“Connect Timeout = ”; 	SqlConnection myConnection = new SqlConnection(conStr); 	myConnection.Open(); Chương 6  Truy cập cơ sở dữ liệu với .NET Đối tượng DataAdapter Dùng để lấy dữ liệu từ dữ liệu nguồn vào DataSet và để cập nhật dữ liệu từ DataSet vào dữ liệu nguồn OleDbDataAdapter làm việc với CSDL MS Access SqlDataAdapter làm việc với dữ liệu SQL Server Chương 6  Truy cập cơ sở dữ liệu với .NET Ví dụ phương thức Fill lấy dữ liệu vào DataTable 	string conStr = “Data Source = may01;" + 	“Initial Catalog = QLSV;“ + 	“Persist Security Info = true;“ + 	“User Id =sa; Password=sa; Connect Timeout =50 ”; 	//Ket noi toi co so du lieu 	SqlConnection myConnection = new SqlConnection(conStr); 	myConnection.Open(); 	string sqlStr = “SELECT * FROM tblKhoaDaoTao”; 	SqlDataAdapter myDataAdapter = new 	SqlDataAdapter(sqlStr,myConnection); 	DataSet myDataSet = new DataSet(); 	myDataAdapter.Fill(myDataSet,”tblKhoaDaoTao”); 	DataTable myTable = myDataSet.Tables[“tblKhoaDaoTao”]; Chương 6  Truy cập cơ sở dữ liệu với .NET Đối tượng DataGridView Dùng để hiển thị dữ liệu từ 1 DataTable Cách thực hiện Thêm đối tượng DataGridView vào Form Nháy chuột phải và chọn Add column hoặc Edit columns Lần lượt chọn Add để thêm các cột Mỗi cột cần khai báo các thuộc tính Name: Tên cột dùng trong mã lệnh Header text: Tiêu đề hiển thị của cột DataPropertyName: Tên cột dữ liệu của DataTable. Chương 6  Truy cập cơ sở dữ liệu với .NET Đối tượng DataGridView DataSource: Tên DataTable cần hiển thị lên lưới AutoGenerateColumns: Tự động lấy các cột nếu bằng true, ngược lại lấy đúng số cột đã khai báo. dataGridView1.AutoGenerateColumns = false; dataGridView1.DataSource = myTable; AllowUserToAddRows: Cho/không thêm dòng trên lưới AllowUserToDeleteRows: Cho/không xoá dòng trên lưới Sự kiện thường dùng RowEnter: Xảy ra khi con trỏ đưa vào một dòng e.RowIndex: Dòng hiện thời e.ColumnIndex: Cột hiện thời Chương 6  Truy cập cơ sở dữ liệu với .NET Đối tượng DataGridView Hiển thị dữ liệu trong bảng tblKhoaDaoTao lên lưới, khi chuyển con trỏ trên lưới dữ liệu hiển thị lên TextBox. Chương 6  Truy cập cơ sở dữ liệu với .NET private string conStr = "Data Source = (local);" + 	"Initial Catalog = QLSinhVien;" + 	"persist security info = true;" + 	"User Id=sa; Password=sa; Connect Timeout =50"; 	private SqlDataAdapter myDataAdapter; 	private DataSet myDataSet; 	private DataTable myTable; private void frmDataGridView_Load(object sender, EventArgs e) 	{ 	string SqlStr = "SELECT * FROM tblKhoaDaoTao"; 	myDataAdapter = new SqlDataAdapter(SqlStr, conStr); 	myDataSet = new DataSet(); 	myDataAdapter.Fill(myDataSet,"tblKhoaDaoTao"); 	myTable = myDataSet.Tables["tblKhoaDaoTao"]; //Chuyen len luoi 	dataGridView1.DataSource = myTable; 	dataGridView1.AutoGenerateColumns = false; } Chương 6  Truy cập cơ sở dữ liệu với .NET private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e) 	{ 	try 	 { 	 int row = e.RowIndex; 	 txtMaKhoa.Text = myTable.Rows[row]["MaKhoa"].ToString(); 	 txtTenKhoa.Text = myTable.Rows[row]["TenKhoa"].ToString(); 	 } 	catch (Exception) 	 { 	 } 	} Chương 6  Truy cập cơ sở dữ liệu với .NET Đối tượng SqlCommand Dùng để thực hiện câu lệnh SQL Insert Update Delete Chương 6  Truy cập cơ sở dữ liệu với .NET Khai báo biến 	private string conStr =“Data Source = ...;”; 	private SqlConnection myConnection; 	private SqlCommand myCommand; Mở kết nối 	myConnection = new SqlConnection(conStr); 	myConnection.Open(); Thực hiện câu lệnh SQL 	myCommand = new SqlCommand(sqlStr,myConnection); 	myCommand.ExecuteNonQuery(); Chú ý: sqlStr là câu lệnh SQL Chương 6  Truy cập cơ sở dữ liệu với .NET private void btnSave_Click(object sender, EventArgs e) 	{ 	string sSql; 	sSql = "Insert Into tblKhoaDaoTao (MaKhoa, TenKhoa)"+ 	"Values (N'"+ txtMaKhoa.Text + "',N'" + txtTenKhoa.Text + "')"; 	myCommand = new SqlCommand(sSql, myConnection); 	myCommand.ExecuteNonQuery(); 	Display(); 	SetControls(false); 	} Chương 6  Truy cập cơ sở dữ liệu với .NET Đối tượng SqlCommandBuilder Tự động thực hiện Update, Insert, Delete Khai báo các biến 	private string conStr = “Data Source = ;”; 	private SqlConnection myConnection; 	private SqlDataAdapter myDataAdapter; 	private SqlCommandBuilder myCommandBuilder; 	private DataSet myDataSet; 	private DataTable myTable; 	private string sqlStr; Chương 6  Truy cập cơ sở dữ liệu với .NET Tạo kết nối tới cơ sở dữ liệu 	myConnection = new SqlConnection(conStr); Tạo một SqlDataAdapter 	myDataAdapter = new SqlDataAdapter(sqlStr, myConnection); Tạo một SqlCommandBuilder 	myCommandBuilder = new 	SqlCommandBuilder(myDataAdapter); Tạo một DataTable 	myDataSet = new DataSet(); 	myDataAdapter.Fill(myDataSet,”....”); 	myTable = myDataSet.Tables[“....”]; Chương 6  Truy cập cơ sở dữ liệu với .NET Xoá một dòng 	myTable.Rows[pos].Delete(); 	myDataAdapter.Update(myTable); 	Trong do : pos là dòng cần xoá Thêm một dòng 	DataRow newRow = myTable.NewRow(); 	newRow["MAKHOA"] = txtMakhoa.Text; 	newRow["TENKHOA"] = txtTen.Text; 	myTable.Rows.Add(newRow); 	myDataAdapter.Update(myTable); Chương 6  Truy cập cơ sở dữ liệu với .NET Sửa một dòng 	DataRow editRow =myTable.Rows[pos]; 	editRow["MAKHOA"] = txtMakhoa.Text; 	editRow["TENKHOA"] = txtTenkhoa.Text; 	myDataAdapter.Update(myTable); 	Note: pos là dòng cần sửa Loại bỏ sửa đổi dòng 	myTable.RejectChanges(); Chương 6  Truy cập cơ sở dữ liệu với .NET Đối tượng ListBox và ComboBox ListBoxes: Cho phép người dùng xem và chọn các dòng dữ liệu từ danh sách ComboBox: Sự kết hợp của TextBox và LixtBox Cac thuoc tinh DataSource: Nguồn dữ liệu, là một DataTable DisplayMember: Cột hiển thị trong ListBox ValueMember: Cột giá trị trả về khi chọn ListBox SelectedIndex: Dòng hiện thời được chọn SelectedValue: Giá trị được chọn trên ListBox Chương 6  Truy cập cơ sở dữ liệu với .NET Ví dụ hiển thị dữ liệu trong tblKhoaDaoTao if (myTable.Rows.Count > 0) 	{ 	comboBox1.DataSource = myTable; 	comboBox1.DisplayMember = “TenKhoa"; 	comboBox1.ValueMember = “MaKhoa"; 	comboBox1.SelectedIndex = 0; 	} Giá trị trả về khi chọn là: comboBox1.SelectValue 

File đính kèm:

  • pptBài giảng Lập trình C# - Hồ Văn Lâm.ppt
Tài liệu liên quan