... private void Form1_Load(object sender, EventArgs e) { } } } Chạy chương trình Bấm vào Start data lên Đã thêm vào bảng Thoát chương trình ... SqlDataAdapter(cmd); //tạo đtượng DataTable SqlDataApdater DataTable dt = new DataTable(); //đổ liệu vào DataTable dt da.Fill(dt); cmd.Dispose(); Closeconnect(); return dt; } public static void ex2(string
Trang 1Assignment 04 – Opt 1
1 Write an application to allow the user to add or update his/her profile data to the trainees list of the application, details as described below:
- On entering the application, the user is asked to input his/her account, first-name, last-name, email Those must not be empty.
- When valid data are inputted,
- The user’s profile information would be added or updated into the database The update occurs only in the case his/her account exists already.
- The user would be redirected to the trainees list page which would show the guests who are accessed the application
- Any input data validation or application exception must be handled Warning and error messages must be shown to the user.
2 From the trainees list page, the user can add one or more feedbacks related to his/her training course (selected from the list) The feedback information includes:
- Feedback type, choose from one of following types:
- The course objectives were met (Mục tiêu khóa học đã đạt được)
- The level of the course is appropriate (Khóa học phù hợp với trình độ của học viên)
- This course will be useful for my work (Khóa học bổ ích cho công việc của bạn)
- The training materials were good (Tài liệu giảng dạy chuẩn bị tốt)
- The organization of the course (facility) was good (Cơ sở vật chất và công tác tổ chức tốt)
- Subject coverage is good with respect to courseware (Đảm bảo đủ nội dung c/t học)
- Your questions/doubts were answered satisfactorily and teacher support is good (Giảng viên hỗ trợ và giải đáp thắc mắc tốt cho học viên)
- The quality of trainer instruction and communication was good (Kỹ năng sư phạm của
GV tốt)
- Feedback rate, rate of of below 5 value (this is required data)
- Strongly disagree (1)
- Disagree (2)
- Neutral (3)
- Agree (4)
- Strongly agree (5)
- Feedback notes, for each feedback item (this is optional data)
For each course, when giving feedbacks on all feedback types, the user can store those to the database.
Requirements:
- Use MSSQL server as the database.
- Use ADO.Net for building DAO
- Build BOs use DAO classes.
Trang 2Tạo Class Ketnoi
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace Bai1
{
class ketnoi
{
public static SqlConnection conn = null; //khai báo 2 thành viên cu,a Class
public static string connect = "";
public static void Openconnect() //phương thức mơ, kê4t nố4i
{
try
{
string connect = @"server = MAY-15\MISASME2015;database =
trainees;integrated security = true";
conn = new SqlConnection(connect);
conn.Open(); // mơ, kê4t nố4i
//MessageBox.Show("Ket noi thanh cong", "Connect Database");
}
catch
{ MessageBox.Show("Ket noi loi!?", "Connect Database"); }
}
public static void Closeconnect()// phương thức đóng kê4t nố4i
{
try
{
string connect = @"server = MAY-15\MISASME2015;database = trainees;integrated security = true";
conn = new SqlConnection(connect);
conn.Close();
//MessageBox.Show("Ngat ket noi thanh cong", "DisConnect
Database");
}
catch (Exception ex)
{
MessageBox.Show("Ngat ket noi loi!?" + ex, "DisConnect
Database");
}
}
public static DataTable ex1(string sql) //chạy 1 Select query
{
Openconnect();//conn.Open(); //mơ, kê4t nố4i
SqlCommand cmd = new SqlCommand(sql, conn); //tạo đtượng SqlCommand
Trang 3SqlDataAdapter da = new SqlDataAdapter(cmd); //tạo đtượng DataTable SqlDataApdater
DataTable dt = new DataTable();
//đố, dữ liệu vào DataTable dt
da.Fill(dt);
cmd.Dispose();
Closeconnect();
return dt;
}
public static void ex2(string sql) //chạy 1 Action query: Insert, Update, Delete
{ //tạo đố4i tượng SqlCommand mới
Openconnect();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery(); //thi hành query
cmd.Dispose();
Closeconnect();
}
}
}
Tạo Class Thaotac
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace Bai1
{
class thaotac
{
ketnoi x = new ketnoi();
public DataTable ShowAccount() //tra, vêQ các Acconut
{
string sql = "select * from hocvien";
DataTable dt = new DataTable();
dt = ketnoi.ex1(sql);
return dt;
}
public void Insertnv(string a, string b, string c, string d) //thêm 1 Nniên
{
string sql = "Insert into hocvien(account,firstname,lastname,email) Values('" + a + "','" + b + "','" + c + "','" + d + "')";
ketnoi.ex2(sql); //gọi thi hành phương thức ex2
}
public void Updatenv(string a, string b, string c, string d) //sư,a 1 Nviên
Trang 4{
string sql = "Update hocvien set firstname='" + b + "', lastname=" +
c + ", email='" + d + "' where account='" + a + "'";
ketnoi.ex2(sql); //gọi thi hành phương thức ex2
}
public void Deletenv(string a) //sư,a 1 Account
{
string sql = "delete hocvien where account='" + a + "'";
ketnoi.ex2(sql); //gọi thi hành phương thức ex2
}
public Boolean Findnv(string a) //tìm 1 Account
{
string sql = "select * from hocvien where account = '" + a + "'"; DataTable dt = new DataTable();
dt = ketnoi.ex1(sql); //gọi thi hành phương thức ex1
if (dt.Rows.Count == 0)
return false;
else
return true;
}
}
}
Chương trình Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Bai1
{
public partial class Form1 : Form
{
thaotac x = new thaotac(); // khơ,i tạo đt thao tác đê, gọi các phương thức cu,a nó trong Form
public void ResetForm() // xoá nội dung các Control trên Form
{
T1.Text = "";
T2.Text = "";
T3.Text = "";
T4.Text = "";
T1.Focus();
}
public Form1()
Trang 5{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ketnoi.Openconnect();
dgv.DataSource = ketnoi.ex1("select * from hocvien");
ketnoi.Closeconnect();
}
private void button6_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Có chắ4c khống?", "Exit Project",
MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
Application.Exit();
}
private void button2_Click(object sender, EventArgs e)
{
ResetForm();
}
private void button4_Click(object sender, EventArgs e)
{
if ((T1.TextLength == 0) && (T2.TextLength == 0) && (T3.TextLength == 0) && (T4.TextLength == 0))
MessageBox.Show("Khống được đê, trố4ng !!!!", "Insert data");
else
{
string a = T1.Text.ToString();
string b = T2.Text.ToString();
//string d =dtp4.Value.ToShortDateString();
//string d =
Convert.ToDateTime(dtp4.Value).ToString("dd/MM/yyyy");
string c = T3.Text.ToString();
string d = T4.Text.ToString();
if (x.Findnv(a) == false)
{
x.Insertnv(a, b, c, d);
MessageBox.Show("Thêm xong", "Insert Data");
ketnoi.Openconnect();
dgv.DataSource = ketnoi.ex1("select * from hocvien");
}
else
{
x.Updatenv(a, b, c, d);
MessageBox.Show("Sư,a xong", "Update Data");
ketnoi.Openconnect();
dgv.DataSource = ketnoi.ex1("select * from hocvien");
}
ResetForm();
}
Trang 6}
private void button3_Click(object sender, EventArgs e)
{
if (T1.TextLength == 0)
MessageBox.Show("Pha,i nhập Mã số4 cầQn xoá!", "Delete 1 Record"); else
{
x.Deletenv(T1.Text);
MessageBox.Show("Xoá xong", "Delete a Record");
ketnoi.Openconnect();
dgv.DataSource = ketnoi.ex1("select * from hocvien");
ResetForm();
}
}
private void button5_Click(object sender, EventArgs e)
{
if (T1.TextLength == 0)
MessageBox.Show("Pha,i nhập Mã số4 cầQn tìm!", "Find Record"); else
{
if (x.Findnv(T1.Text) == false)
MessageBox.Show("Khống tìm thầ4y!", "Find Record");
else
MessageBox.Show("Tìm thầ4y, Nhần viên có Mã số4: " + T1.Text,
"Find Record");
ResetForm();
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Trang 7Chạy chương trình
Bấm vào Start hiện data lên
Trang 8Đã thêm vào bảng
Trang 9Thoát chương trình