Ngày nay, website tin tức được xem là mô hình có tính phổ biến và đa dạng nhất bởi khả năng cập nhật đa dạng và xuất bản thông tin nhanh. Vì vậy, Thiết Kế Website Tin Tức rất phù hợp với các cơ quan, tổ chức muốn xây dựng cho mình một trang web để đưa tin tức tiếp cận người dùng internet,… Website tin tức cung cấp đầy đủ các tính năng cơ bản của một website như: quản lý quảng cảo, hỗ trợ tìm kiếm, thống kê, hệ thống bình chọn,…không giới hạn về kí tự, số lượng bài viết hay thời điểm đăng tải. Bên cạnh đó, trang web tin tức có tốc độ truy cập nhanh và rất thuận tiện cho người tìm kiếm.
Trang 1Bài 9:
Kết nối cơ sở dữ liệu với ADO.NET
1 Mục đích
• Thực tập kết nối, thao tác dữ liệu bằng nhiều cách thông qua thư viện ADO.NET
2 Yêu cầu
• Đã hoàn thành bài 8
3 Thời gian để hoàn tất bài thực hành:
• 90 phút
Trang 21 Thêm vào trang ADODOTNET.aspx, thiết kế giao diện theo mẫu:
2 Kết nối cơ sở dữ liệu
protected void Button1_Click(object sender, EventArgs e)
{
//Khai báo và khởi tạo biến Connection
SqlConnection cnn = new SqlConnection("server=serverName;
database=WebNews; user id=sa; password=1234567");
//Mở kết nối
cnn.Open();
// Hiển thị thông tin trạng thái kết nối
TextBox1.Text = "State = " + cnn.State;
//Đóng kết nối
cnn.Close();
}
3 Kết nối, thao tác dữ liệu, sử dụng đối tượng Command với câu lệnh SELECT
protected void Button2_Click(object sender, EventArgs e)
{
try
{
//Khai báo và khởi tạo biến Connection
SqlConnection cnn = new SqlConnection("server=serverName;
database=WebNews; user id=sa; password=1234567");
Trang 3//Khai báo và khởi tạo biến Command
SqlCommand cmd = new SqlCommand();
//Khai báo biến Command sử dụng Connection nào để đến database? cmd.Connection = cnn;
//Biến Command thao tác với database bằng (1.Câu lệnh, 2.Tên bảng, 3.Tên Store) nào?
cmd.CommandText = "SELECT COUNT(*) FROM LinhVuc";
//Cho biết CommandText chính là câu lệnh
cmd.CommandType = CommandType.Text;
//Mở kết nối
cnn.Open();
// Lấy dữ liệu về bằng phương thức ExecuteScalar
int count = (int)cmd.ExecuteScalar();
//Xuất kết quả ra WebForm
TextBox2.Text = count.ToString();
//Đóng kết nối
cnn.Close();
}
catch (Exception)
{
//Xuất kết quả ra WebForm
TextBox2.Text = "Không thành công!";
}
}
4 Kết nối, thao tác dữ liệu, sử dụng đối tượng Command với câu lệnh INSERT
protected void Button3_Click(object sender, EventArgs e)
{
try
{
SqlConnection cnn = new SqlConnection("server=serverName;
database=WebNews; user id=sa; password=1234567");
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
//Biến Commnad thao tác với database bằng câu lệnh (INSERT,
UPDATE, DELETE)
cmd.CommandText = "INSERT INTO LINHVUC VALUES('VH',N'Văn hóa')"; cmd.CommandType = CommandType.Text;
cnn.Open();
//Thao tác dữ liệu (INSERT, UPDATE, DELETE) bằng phương thức ExecuteNonQuery
cmd.ExecuteNonQuery();
//Xuất kết quả ra WebForm
TextBox3.Text = "Thành công!";
cnn.Close();
}
catch (Exception)
{
//Xuất kết quả ra WebForm
TextBox3.Text = "Không thành công!";
Trang 4}
}
5 Kết nối, thao tác dữ liệu, sử dụng đối tượng Command với câu lệnh INSERT + Truyền tham số
protected void Button4_Click( object sender, EventArgs e)
{
try
{
SqlConnection cnn = new
SqlConnection ( "server=serverName; database=WebNews; user id=sa; password=1234567" );
SqlCommand cmd = new SqlCommand ();
cmd.Connection = cnn;
//Khai báo và khởi tạo tham số
SqlParameter parMaLinhVuc = new
SqlParameter ( "@MALINHVUC" , SqlDbType NChar, 10);
SqlParameter parTenLinhVuc = new
SqlParameter ( "@TENLINHVUC" , SqlDbType NVarChar, 255);
cmd.CommandText = "INSERT INTO LINHVUC
VALUES(@MALINHVUC,@TENLINHVUC)" ;
cmd.CommandType = CommandType Text;
//Thêm biến tham số cho Command
cmd.Parameters.Add(parMaLinhVuc);
cmd.Parameters.Add(parTenLinhVuc);
//Gán giá trị cho biến tham số
parMaLinhVuc.Value = TextBox4.Text;
parTenLinhVuc.Value = TextBox5.Text;
cnn.Open();
cmd.ExecuteNonQuery();
cnn.Close();
TextBox4.Text = "Thành công!" ;
}
catch ( Exception )
{
TextBox4.Text = "Không thành công!" ;
}
}
Trang 56 Kết nối, thao tác dữ liệu, sử dụng đối tượng Command với câu lệnh SELECT + DataReader
protected void Button5_Click(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection("server=serverName;
database=WebNews; user id=sa; password=1234567");
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
cmd.CommandText = "SELECT * FROM LINHVUC";
cmd.CommandType = CommandType.Text;
cnn.Open();
//Lấy danh sách lĩnh vực bỏ vào biến DataReader
SqlDataReader dr = cmd.ExecuteReader();
String list = "";
//Duyệt qua DataReader
while (dr.Read())
{
list = list + dr["MaLinhVuc"].ToString().Trim() + " ";
}
dr.Close();
TextBox6.Text = list.ToString();
cnn.Close();
}
7 Kết nối cơ sở dữ liệu + gọi store để lấy dữ liệu
CREATE PROCEDURE GetLinhVuc
AS
BEGIN
SELECT FROM LINHVUC
END
GO
protected void Button6_Click(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection("server=serverName;
database=WebNews; user id=sa; password=1234567");
SqlCommand cmd = new SqlCommand("GETLINHVUC", cnn);
cmd.CommandType = CommandType.StoredProcedure;
cnn.Open();
IDataReader dr = cmd.ExecuteReader();
String list = "";
while (dr.Read())
{
list = list + dr["MaLinhVuc"].ToString().Trim()+" ";
}
dr.Close();
Trang 6TextBox7.Text = list.ToString();
cnn.Close();
}
8 Kết nối cơ sở dữ liệu + gọi store để lấy dữ liệu + truyền tham số
CREATE PROCEDURE GetLinhVucByMaLinhVuc
@MaLinhVuc char(15)
AS
BEGIN
SELECT FROM LINHVUC WHERE MaLinhVuc=@MaLinhVuc
END
GO
protected void Button7_Click(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection("server=serverName;
database=WebNews; user id=sa; password=1234567");
SqlCommand cmd = new SqlCommand("GETLINHVUCBYMALINHVUC", cnn);
cmd.CommandType = CommandType.StoredProcedure;
//Khai báo và khởi tạo biến Command
SqlParameter parMALINHVUC = new SqlParameter("@MALINHVUC",
SqlDbType.NChar, 10);
parMALINHVUC.Value = TextBox8.Text;
cmd.Parameters.Add(parMALINHVUC);
cnn.Open();
SqlDataReader dr = cmd.ExecuteReader();
String list = "";
while (dr.Read())
{
list = list + dr["TenLinhVuc"].ToString().Trim()+" ";
}
dr.Close();
TextBox9.Text = list.ToString();
cnn.Close();
}
9 Kết nối cơ sở dữ liệu + sử dụng DataAdapter + update dữ liệu
protected void Button8_Click(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection("server=serverName;
database=WebNews; user id=sa; password=1234567");
SqlDataAdapter da = new SqlDataAdapter("select * from LINHVUC", cnn); SqlCommandBuilder commandBuilder = new SqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds);
foreach (DataRow row in ds.Tables[0].Rows)
if (row["MaLinhVuc"].ToString().Trim() =="VH")
{
Trang 7row["TENLINHVUC"] = "BBB";
}
TextBox10.Text = ds.Tables[0].Rows[2].ItemArray[1].ToString();
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
//Không sử dụng SqlCommandBuilder thì không thể update dữ liệu
da.Update(ds);
}
10 Kết nối cơ sở dữ liệu + sử dụng DataAdapter + gọi store
protected void Button9_Click(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection("server=serverName;
database=WebNews; user id=sa; password=1234567");
//Khai báo và khởi tạo SqlDataAdapter
SqlDataAdapter da = new SqlDataAdapter("GETLINHVUC", cnn);
//Khai báo và khởi tạo DataSet
DataSet ds = new DataSet();
//Lấy dữ liệu trả về đổ vào dataset ds
da.Fill(ds);
TextBox11.Text = ds.Tables[0].Rows[2].ItemArray[1].ToString();
GridView2.DataSource = ds.Tables[0];
GridView2.DataBind();
}
11 Kết nối cơ sở dữ liệu + sử dụng DataAdapter + gọi store + truyền tham số
protected void Button10_Click(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection("server=serverName;
database=WebNews; user id=sa; password=1234567");
SqlCommand cmd = new SqlCommand("GETLINHVUCBYMALINHVUC", cnn);
cmd.CommandType = CommandType.StoredProcedure;
//Khai báo và khởi tạo tham số
SqlParameter parMALINHVUC = new SqlParameter("@MALINHVUC",
SqlDbType.NChar, 10);
parMALINHVUC.Value = "XH";
cmd.Parameters.Add(parMALINHVUC);
cnn.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
GridView3.DataSource = ds.Tables[0];
GridView3.DataBind();
cnn.Close();
}
Trang 812 Kết nối cơ sở dữ liệu + sử dụng DataAdapter + đối số là Command
protected void Button10_Click(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection("server=serverName;
database=WebNews; user id=sa; password=1234567");
SqlCommand cmd = new SqlCommand("GETLINHVUC", cnn);
cmd.CommandType = CommandType.StoredProcedure;
cnn.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
//da.InsertCommand = cmd;
//da.DeleteCommand = cmd;
//da.UpdateCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
GridView4.DataSource = ds.Tables[0];
GridView4.DataBind();
cnn.Close();
}
}
***Hết***