Xây dựng phần mềm hướng đối tượng - Hướng dẫn thực hành tuần 5: ADO.NET

if(rows.Count > 0)

{

DataGridViewRowrow = rows[0];

txtMaHS.Text = row.Cells["MaHS"].Value.ToString();

txtTenHS.Text = row.Cells["TenHS"].Value.ToString();

if(row.Cells["NgaySinh"].Value.ToString().Length>0)

dtNgaySinh.Value =

DateTime.Parse(row.Cells["NgaySinh"].Value.ToString());

txtDiaChi.Text = row.Cells["DiaChi"].Value.ToString();

txtDiemTB.Text = row.Cells["DiemTB"].Value.ToString();

cboLop.Text = row.Cells["TenLop"].Value.ToString();

}

}

pdf8 trang | Chuyên mục: Visual C# | Chia sẻ: dkS00TYs | Lượt xem: 1626 | Lượt tải: 1download
Tóm tắt nội dung Xây dựng phần mềm hướng đối tượng - Hướng dẫn thực hành tuần 5: ADO.NET, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
Xây dựng phần mềm hướng đối tượng GVHD: Trần Anh Dũng 
HƯỚNG DẪN THỰC HÀNH TUẦN 5 
Chủ đề: ADO.NET (tt) 
Phần 2: Các thao tác kết nối ADO.NET sử dụng cấu trúc dòng lệnh với 
namespace System.Data. 
(Kết nối với Database SQL Server) 
1. Thiết kế CSDL SQL Server như sau: 
a. Tạo databse với tên DBHOCSINH, thiết kế các table sau: 
HOCSINH 
STT Tên trường Kiểu dữ liệu Ghi chú 
1 MaHS Nvarchar(20) PrimaryKey 
2 TenHS Nvarchar(100) 
3 NgaySinh Datetime 
4 DiaChi Nvarchar(255) 
5 DTB Real 
6 MaLop Nvarchar(20) ForeignKey (tham chiếu đến 
Lop(MaLop) 
LOP 
STT Tên trường Kiểu dữ liệu Ghi chú 
1 MaLop Nvarchar(20) PrimaryKey 
2 TenLop Nvarchar(100) 
3 SiSo smallint 
2. Tạo project mới: 
Thiết kế lại Form “Kết nối CSDL SQL Server” như màn hình sau: 
Xây dựng phần mềm hướng đối tượng GVHD: Trần Anh Dũng 
Source code lớp frmConnection 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
namespace QLHocSinh 
{ 
 public partial class frmConnection : Form 
 { 
 //Khai báo biến thành phần 
 public string sServerName = ""; 
 public string sDatabaseName = ""; 
 public string sUser = ""; 
 public string sPass = ""; 
 public frmConnection() 
 { 
 InitializeComponent(); 
 } 
 private void frmConnection_Load(object sender, EventArgs e) 
 { 
 txtServerName.Focus(); 
 } 
 private void cmdExit_Click(object sender, EventArgs e) 
 { 
 this.Close(); 
 } 
 private bool KiemTraDLHopLe() 
 { 
 if (txtServerName.Text.Trim().Length == 0) 
Xây dựng phần mềm hướng đối tượng GVHD: Trần Anh Dũng 
 { 
 MessageBox.Show("Chưa nhập tên máy chủ.", "Thong bao loi", 
MessageBoxButtons.OK, MessageBoxIcon.Stop); 
 return false; 
 } 
 if (txtDBName.Text.Trim().Length == 0) 
 { 
 MessageBox.Show("Chưa nhập tên CSDL.", "Thong bao loi", 
MessageBoxButtons.OK, MessageBoxIcon.Stop); 
 return false; 
 } 
 return true; 
 } 
 private void cmdConnect_Click(object sender, EventArgs e) 
 { 
 if (!KiemTraDLHopLe()) 
 return; 
 sServerName = txtServerName.Text.Trim(); 
 sDatabaseName = txtDBName.Text.Trim(); 
 sUser = txtUser.Text.Trim(); 
 sPass = txtPass.Text.Trim(); 
 frmHocSinh frm = new frmHocSinh(); 
 //Kiểm tra kết nối CSDL 
 if (!frm.connect(sServerName, sDatabaseName, sUser, sPass)) 
 { 
 MessageBox.Show("Kết nối đến máy chủ không thành công.", 
"Thong bao loi", MessageBoxButtons.OK, MessageBoxIcon.Stop); 
 return; 
 } 
 MessageBox.Show("Kết nối đến máy chủ thành công.", "Thong bao", 
MessageBoxButtons.OK, MessageBoxIcon.Information); 
 frm.Show(); 
 this.Hide(); 
 } 
 } 
} 
Xây dựng phần mềm hướng đối tượng GVHD: Trần Anh Dũng 
Thiết kế form nhập thông tin học sinh như sau: 
Kết quả màn hình khi chạy: 
 DataGridView 
Xây dựng phần mềm hướng đối tượng GVHD: Trần Anh Dũng 
Để kết nối với CSDL SQL Server dùng namespace: using System.Data.SqlClient; 
Source code lớp Màn hình nhập thông tin học sinh: 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.SqlClient; 
namespace QLHocSinh 
{ 
 public partial class frmHocSinh : Form 
 { 
 private SqlConnection connection; 
 private SqlDataAdapter adapter; 
 private DataSet dataSet; 
 private SqlCommand command; 
 private string maHS, tenHS, diachi, malop; 
 private float dtb; 
 private DateTime ngaysinh; 
 public frmHocSinh() 
 { 
 InitializeComponent(); 
 } 
 public bool connect(string sServerName, string sDBName, 
string sUser, string sPass) 
 { 
 string connnectionString = "server=" + sServerName + 
"; database=" + 
 sDBName + "; user id=" + sUser + "; password=" + sPass; 
 connection = new SqlConnection(connnectionString); 
 try 
 { 
 connection.Open(); 
 connection.Close(); 
 return true; 
 } 
 catch 
 { 
 return false; 
 } 
 } 
 private DataTable getDSLop() 
 { 
 adapter = new SqlDataAdapter("select * from LOP", connection); 
 dataSet = new DataSet(); 
 adapter.Fill(dataSet); 
 return dataSet.Tables[0]; 
 } 
 private DataTable getDSHocSinh() 
 { 
Xây dựng phần mềm hướng đối tượng GVHD: Trần Anh Dũng 
 adapter = new SqlDataAdapter("Select h.MaHS, h.TenHS, 
 h.NgaySinh, h.DiaChi, h.DiemTB, l.TenLop From HOCSINH h, 
LOP l Where h.MaLop=l.MaLop", connection); 
 dataSet = new DataSet(); 
 adapter.Fill(dataSet); 
 return dataSet.Tables[0]; 
 } 
 private void DinhDangLuoi() 
 { 
 dgHocSinh.ReadOnly = true; 
 dgHocSinh.Columns[0].HeaderText = "Mã HS"; 
 dgHocSinh.Columns[0].Width = 70; 
 dgHocSinh.Columns[1].HeaderText = "Tên HS"; 
 dgHocSinh.Columns[1].Width = 150; 
 dgHocSinh.Columns[2].HeaderText = "Ngày sinh"; 
 dgHocSinh.Columns[2].Width = 90; 
 dgHocSinh.Columns[3].HeaderText = "Địa chỉ"; 
 dgHocSinh.Columns[3].Width = 200; 
 dgHocSinh.Columns[4].HeaderText = "Điểm TB"; 
 dgHocSinh.Columns[4].Width = 80; 
 dgHocSinh.Columns[5].HeaderText = "Lớp"; 
 dgHocSinh.Columns[5].Width = 80; 
 } 
 private void Form1_Load(object sender, EventArgs e) 
 { 
 //Load dữ liệu vào comboBox Lớp 
 DataTable dtLop = getDSLop(); 
 cboLop.DataSource = dtLop; 
 //Chọn item đầu tiên trong table 
 cboLop.SelectedIndex = 0; 
 // Column sẽ được hiển thị 
 cboLop.DisplayMember = dtLop.Columns[1].ColumnName; 
 // Column sẽ được giữ giá trị 
 cboLop.ValueMember = dtLop.Columns[0].ColumnName; 
 //Load danh sách học sinh lên lưới 
 dgHocSinh.DataSource = getDSHocSinh(); 
 //Định dạng lưới 
 DinhDangLuoi(); 
 } 
 private void getData() 
 { 
 maHS = txtMaHS.Text; 
 tenHS = txtTenHS.Text; 
 ngaysinh = dtNgaySinh.Value; 
 diachi = txtDiaChi.Text; 
 malop = (string)cboLop.SelectedValue; 
 dtb = float.Parse(txtDiemTB.Text); 
 } 
 private void insert() 
 { 
 connection.Open(); 
 string insertCommand = "INSERT INTO HOCSINH VALUES('" + 
 maHS + "', '" + 
Xây dựng phần mềm hướng đối tượng GVHD: Trần Anh Dũng 
 tenHS + "', '" + 
 ngaysinh.ToShortDateString() + "', '" + 
 diachi + "', " + 
 dtb + ", '" + 
 malop + "')"; 
 command = new SqlCommand(insertCommand, connection); 
 command.ExecuteNonQuery(); 
 connection.Close(); 
 } 
 private void delete() 
 { 
 connection.Open(); 
 string deleteCommand = "DELETE FROM HOCSINH WHERE MaHS = '" + 
maHS + "'"; 
 command = new SqlCommand(deleteCommand, connection); 
 command.ExecuteNonQuery(); 
 connection.Close(); 
 } 
 private void btnXoa_Click(object sender, EventArgs e) 
 { 
 getData(); 
 delete(); 
 MessageBox.Show("Xoa du lieu thanh cong", "Thong bao", 
MessageBoxButtons.OK, MessageBoxIcon.Information); 
 //Load lai danh sach hoc sinh len luoi 
 dgHocSinh.DataSource = getDSHocSinh(); 
 //Xoa du lieu tren cac textbox 
 txtMaHS.Text = ""; 
 btnNew_Click(sender, e); 
 } 
 private void btnThoat_Click(object sender, EventArgs e) 
 { 
 Application.Exit(); 
 } 
 private void btnLuu_Click(object sender, EventArgs e) 
 { 
 getData(); 
 insert(); 
 MessageBox.Show("Cap nhat thanh cong", "Thong bao", 
MessageBoxButtons.OK, MessageBoxIcon.Information); 
 //Load lai danh sach hoc sinh len luoi 
 dgHocSinh.DataSource = getDSHocSinh(); 
 } 
 private void txtMaHS_KeyPress(object sender, KeyPressEventArgs e) 
 { 
 if (e.KeyChar == 13) 
 SendKeys.Send("{TAB}"); 
 } 
 private void dgHocSinh_SelectionChanged(object sender, EventArgs e) 
 { 
 DataGridViewSelectedRowCollection rows = dgHocSinh.SelectedRows; 
Xây dựng phần mềm hướng đối tượng GVHD: Trần Anh Dũng 
 if (rows.Count > 0) 
 { 
 DataGridViewRow row = rows[0]; 
 txtMaHS.Text = row.Cells["MaHS"].Value.ToString(); 
 txtTenHS.Text = row.Cells["TenHS"].Value.ToString(); 
 if (row.Cells["NgaySinh"].Value.ToString().Length>0) 
 dtNgaySinh.Value = 
 DateTime.Parse(row.Cells["NgaySinh"].Value.ToString()); 
 txtDiaChi.Text = row.Cells["DiaChi"].Value.ToString(); 
 txtDiemTB.Text = row.Cells["DiemTB"].Value.ToString(); 
 cboLop.Text = row.Cells["TenLop"].Value.ToString(); 
 } 
 } 
 private void btnNew_Click(object sender, EventArgs e) 
 { 
 txtMaHS.Text = ""; 
 txtTenHS.Text = ""; 
 txtDiaChi.Text = ""; 
 txtDiemTB.Text = ""; 
 } 
 } 
} 

File đính kèm:

  • pdfXây dựng phần mềm hướng đối tượng - Hướng dẫn thực hành tuần 5 ADO.NET.pdf
Tài liệu liên quan