1. Trang chủ
  2. » Công Nghệ Thông Tin

Mô hình 3 lớp ASP.NET

18 238 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 18
Dung lượng 389,82 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 1

Bài 10:

LÀM LẠI BÀI 8 VỚI MÔ HÌNH 3 LỚP

1 Mục đích

Trong bài thực hành này, bạn sẽ xây dựng trang website quản lý tin:

• Cho phép chọn một lĩnh vực từ combo box và hiển thị danh sách tin thuộc lĩnh vực được chọn

• Cho phép sắp xếp danh sách tin theo ngày đăng tin, tiêu đề

• Cho phép thêm, xoá, sửa tin

2 Yêu cầu

• Đã nắm bắt được cách thức kết nối CSDL

• Xây dựng được kiến trúc 3 tầng

• Tạo các class: LinhVucInfo, BanTinInfo, LinhVucController, BanTinController, LinhVucDAL, BanTinDAL, DataService

3 Vấn đề liên quan

• Thao tác với DataGrid: hiển thị dữ liệu, phân trang, sắp xếp,…

4 Thời gian để hoàn tất bài thực hành: 120 phút

Trang 2

1 Tạo trang QuanLyTin3Lop.aspx

• Tạo DropDownList Lĩnh vực

o Kéo thả 1 DropDownList vào QuanLyTin3Lop.aspx và đặt tên

DropDownListLinhVuc

• Tạo GridView Bản tin

o Kéo thả 1 GridView vào QuanLyTin3Lop.aspx đặt tên GridViewBanTin

o Thay đổi giao diện : phải chuột  Auto format  Select a scheme : chọn

Professional

2 Tạo thư mục App_Code

• Right-Click project, chọn Add ASP.NET Folder  App_Code

3 Tạo lớp DataServcie.cs trong thư mục App_Code và viết code như sau:

• Right-Click thư mục App_Code, chọn Add New Item…

//Thêm vào thư viện SqlClient

using System.Data.SqlClient;

/// <summary>

/// Đây là lớp dịch vụ về thao tác dữ liệu

/// (Kết nối, Truy xuất, Thêm, Xoá, Sửa)

/// </summary>

public class DataService

{

/// <summary>

/// Khai báo các thuộc tính của lớp

/// </summary>

// Biến connection kết nối đến database

private SqlConnection _connection;

// Chuỗi kết nối

Trang 3

private string _connectionString = "server=.; database=WebNews; user id=sa;

password=sa12345";

// Biến command dùng để thực thi câu query hay non-query, store

public SqlCommand _command;

// Biến data adapter để thực thi query, non-query, store

private SqlDataAdapter _dataAdapter;

/// <summary>

/// Định nghĩa các thuộc tính của lớp

/// </summary>

//Contructor : Khởi tạo giá trị cho các đối tượng

public DataService()

{

_connection = new SqlConnection(_connectionString);

_command = new SqlCommand();

_command.Connection = _connection;

_dataAdapter = new SqlDataAdapter();

_dataAdapter.SelectCommand = _command;

_dataAdapter.InsertCommand = _command;

_dataAdapter.UpdateCommand = _command;

_dataAdapter.DeleteCommand = _command;

}

// Mở kết nối dữ liệu

public void openConnection()

{

// Chỉ mở kết nối khi nó đóng

if (_connection.State == ConnectionState.Closed)

_connection.Open();

}

// Đóng kết nối dữ liệu

public void closeConnection()

{

if (_connection.State == ConnectionState.Open)

_connection.Close();

}

// Yêu cầu SQLServer thực thi store (SELECT *) và trả kết quả về

// Sử dụng đối tượng DataAdapter và Command

public DataSet executeSelectStore(string storeName)

{

try

{

DataSet ds = new DataSet();

_command.CommandText = storeName;

_command.CommandType = CommandType.StoredProcedure;

_dataAdapter.Fill(ds);

return ds;

}

catch { throw; }

}

/// Thực thi DML store (INSERT, UPDATE, DELETE)

Trang 4

/// </summary>

public void executeDMLStore(string storeName)

{

try

{

openConnection();

_command.CommandText = storeName;

_command.CommandType = CommandType.StoredProcedure;

_command.ExecuteNonQuery();

}

catch { throw; }

finally

{

// Closes the connection if there is no transaction

if (_command.Transaction == null)

closeConnection();

}

}

}

4 Tạo thư mục BusinessInfo trong thư mục App_Code

5 Tạo lớp LinhVucInfo.cs trong thư mục BusinessInfo và viết code như sau:

/// <summary>

/// Lớp LinhVucInfo đại diện cho bảng LinhVuc trong SQL Server

/// Được sử dụng để lưu giá trị của Lĩnh vực và

/// truyền dữ liệu (tham số) giữa các lớp

/// </summary>

public class LinhVucInfo

{

/// <summary>

/// Khai báo thuộc tính

/// Tất cả đều phải khai báo private

/// </summary>

private string strMaLinhVuc;

private string strTenLinhVuc;

/// <summary>

/// Định nghĩa phương thức

/// (GET: truy xuất, SET: Gán, cập nhật giá trị) cho thuộc tính

/// Tất cả đều phải khai báo public

/// </summary>

public string MaLinhVuc

{

get { return strMaLinhVuc; }

set { strMaLinhVuc = value; }

}

public string TenLinhVuc

{

get { return strTenLinhVuc; }

set { strTenLinhVuc = value; }

}

}

6 Tạo lớp BanTinInfo.cs trong thư mục BusinessInfo và viết code như sau:

/// <summary>

/// Lớp LinhVucInfo đại diện cho bảng LinhVuc trong SQL Server

/// Được sử dụng để lưu giá trị của Lĩnh vực và

/// truyền dữ liệu (tham số) giữa các lớp

/// </summary>

public class BanTinInfo

{

private string strMaBanTin;

Trang 5

private string strTieuDe;

private string strNoiDungTomTat;

private string strNoiDung;

private DateTime dtNgayDangTin;

private string strHinhAnh;

private string strChuThichHinh;

private string strMaLinhVuc;

public string MaBanTin

{

get { return strMaBanTin; }

set { strMaBanTin = value; }

}

public string TieuDe

{

get { return strTieuDe; }

set { strTieuDe = value; }

}

public string NoiDungTomTat

{

get { return strNoiDungTomTat; }

set { strNoiDungTomTat = value; }

}

public string NoiDung

{

get { return strNoiDung; }

set { strNoiDung = value; }

}

public DateTime NgayDangTin

{

get { return dtNgayDangTin; }

set { dtNgayDangTin = value; }

}

public string HinhAnh

{

get { return strHinhAnh; }

set { strHinhAnh = value; }

}

public string ChuThichHinh

{

get { return strChuThichHinh; }

set { strChuThichHinh = value; }

}

public string MaLinhVuc

{

get { return strMaLinhVuc; }

set { strMaLinhVuc = value; }

}

}

7 Viết store cho bảng Lĩnh vực như sau:

/*

===================================================================================== / TABLE: LinhVuc

/

=====================================================================================

*/

/****** Nếu Store đã tồn tại thì xoá ******/

if exists select from dbo.sysobjects where id = object_id( 'LinhVucGet') and

OBJECTPROPERTY(id, N'IsProcedure') = 1)

drop procedure LinhVucGet

GO

Trang 6

if exists select from dbo.sysobjects where id = object_id( 'LinhVucList') and

OBJECTPROPERTY(id, N'IsProcedure') = 1)

drop procedure LinhVucList

GO

if exists select from dbo.sysobjects where id = object_id( 'LinhVucAdd') and

OBJECTPROPERTY(id, N'IsProcedure') = 1)

drop procedure LinhVucAdd

GO

if exists select from dbo.sysobjects where id = object_id( 'LinhVucUpdate') and

OBJECTPROPERTY(id, N'IsProcedure') = 1)

drop procedure LinhVucUpdate

GO

if exists select from dbo.sysobjects where id = object_id( 'LinhVucDelete') and

OBJECTPROPERTY(id, N'IsProcedure') = 1)

drop procedure LinhVucDelete

GO

/* -

/ LinhVucGet

/ - */

CREATE PROCEDURE LinhVucGet

@MaLinhVuc char(10)

AS

SELECT

[MaLinhVuc],

[TenLinhVuc]

FROM LinhVuc

WHERE

[MaLinhVuc] = @MaLinhVuc

GO

/* -

/ LinhVucList

/ - */

CREATE PROCEDURE LinhVucList

AS

SELECT

[MaLinhVuc],

[TenLinhVuc]

FROM LinhVuc

GO

/* -

/ LinhVucAdd

/ - */

CREATE PROCEDURE LinhVucAdd

@MaLinhVuc char(10),

@TenLinhVuc nvarchar(255)

AS

Trang 7

INSERT INTO LinhVuc (

[MaLinhVuc],

[TenLinhVuc]

) VALUES (

@MaLinhVuc,

@TenLinhVuc

)

GO

/* -

/ LinhVucUpdate

/ - */

CREATE PROCEDURE LinhVucUpdate

@MaLinhVuc char(10),

@TenLinhVuc nvarchar(255)

AS

UPDATE LinhVuc SET

[TenLinhVuc] = @TenLinhVuc

WHERE

[MaLinhVuc] = @MaLinhVuc

GO

/* -

/ LinhVucDelete

/ - */

CREATE PROCEDURE LinhVucDelete

@MaLinhVuc char(10)

AS

DELETE FROM LinhVuc

WHERE

[MaLinhVuc] = @MaLinhVuc

GO

8 Viết store cho bảng Bản tin như sau:

/*

===================================================================================== / TABLE: BanTin

/

=====================================================================================

*/

/****** Nếu Store đã tồn tại thì xoá ******/

if exists select from dbo.sysobjects where id = object_id( 'BanTinGet') and

OBJECTPROPERTY(id, N'IsProcedure') = 1)

drop procedure BanTinGet

GO

if exists select from dbo.sysobjects where id = object_id( 'BanTinList') and

OBJECTPROPERTY(id, N'IsProcedure') = 1)

drop procedure BanTinList

GO

if exists select from dbo.sysobjects where id = object_id( 'BanTinGetByLinhVuc') and OBJECTPROPERTY(id, N'IsProcedure') = 1)

drop procedure BanTinGetByLinhVuc

GO

Trang 8

if exists select from dbo.sysobjects where id = object_id( 'BanTinAdd') and

OBJECTPROPERTY(id, N'IsProcedure') = 1)

drop procedure BanTinAdd

GO

if exists select from dbo.sysobjects where id = object_id( 'BanTinUpdate') and

OBJECTPROPERTY(id, N'IsProcedure') = 1)

drop procedure BanTinUpdate

GO

if exists select from dbo.sysobjects where id = object_id( 'BanTinDelete') and

OBJECTPROPERTY(id, N'IsProcedure') = 1)

drop procedure BanTinDelete

GO

/* -

/ BanTinGet

/ - */

CREATE PROCEDURE BanTinGet

@MaBanTin char(10)

AS

SELECT

[MaBanTin],

[TieuDe],

[NoiDungTomTat],

[NoiDung],

[NgayDangTin],

[HinhAnh],

[ChuThichHinh],

[MaLinhVuc]

FROM BanTin

WHERE

[MaBanTin] = @MaBanTin

GO

/* -

/ BanTinList

/ - */

CREATE PROCEDURE BanTinList

AS

SELECT

[MaBanTin],

[TieuDe],

[NoiDungTomTat],

[NoiDung],

[NgayDangTin],

[HinhAnh],

[ChuThichHinh],

[MaLinhVuc]

FROM BanTin

GO

/*

Trang 9

-

/ BanTinGetByLinhVuc

/ - */

CREATE PROCEDURE BanTinGetByLinhVuc

@MaLinhVuc char(10)

AS

SELECT

[MaBanTin],

[TieuDe],

[NoiDungTomTat],

[NoiDung],

[NgayDangTin],

[HinhAnh],

[ChuThichHinh],

[MaLinhVuc]

FROM BanTin

WHERE

[MaLinhVuc]=@MaLinhVuc

GO

/* -

/ BanTinAdd

/ - */

CREATE PROCEDURE BanTinAdd

@MaBanTin char(10),

@TieuDe nvarchar(255),

@NoiDungTomTat nvarchar(500),

@NoiDung ntext,

@NgayDangTin datetime,

@HinhAnh nvarchar(255),

@ChuThichHinh nvarchar(255),

@MaLinhVuc char(10)

AS

INSERT INTO BanTin (

[MaBanTin],

[TieuDe],

[NoiDungTomTat],

[NoiDung],

[NgayDangTin],

[HinhAnh],

[ChuThichHinh],

[MaLinhVuc]

) VALUES (

@MaBanTin,

@TieuDe,

@NoiDungTomTat,

@NoiDung,

@NgayDangTin,

@HinhAnh,

@ChuThichHinh,

@MaLinhVuc

)

GO

/* -

/ BanTinUpdate

/

Trang 10

- */

CREATE PROCEDURE BanTinUpdate

@MaBanTin char(10),

@TieuDe nvarchar(255),

@NoiDungTomTat nvarchar(500),

@NoiDung ntext,

@NgayDangTin datetime,

@HinhAnh nvarchar(255),

@ChuThichHinh nvarchar(255),

@MaLinhVuc char(10)

AS

UPDATE BanTin SET

[TieuDe] = @TieuDe,

[NoiDungTomTat] = @NoiDungTomTat,

[NoiDung] = @NoiDung,

[NgayDangTin] = @NgayDangTin,

[HinhAnh] = @HinhAnh,

[ChuThichHinh] = @ChuThichHinh,

[MaLinhVuc] = @MaLinhVuc

WHERE

[MaBanTin] = @MaBanTin

GO

/* -

/ BanTinDelete

/ - */

CREATE PROCEDURE BanTinDelete

@MaBanTin char(10)

AS

DELETE FROM BanTin

WHERE

[MaBanTin] = @MaBanTin

GO

9 Tạo thư mục DataAccessLayer trong thư mục App_Code

10 Tạo lớp LinhVucDAL.cs trong thư mục DataAccessLayer và viết code như sau:

/// <summary>

/// Lớp LinhVucDAL thông qua lớp dịch vụ

/// DataService để thao tác dữ liệu

/// </summary>

public class LinhVucDAL

{

//Khai báo biến _ds là lớp DataService

private DataService _ds;

//Contructor

public LinhVucDAL()

{

_ds = new DataService();

}

// Lấy danh sách tất cả lĩnh vực

public DataSet GetLinhVuc()

{

return _ds.executeSelectStore("LinhVucList");

}

// Lấy tất cả thông tin của 1 lĩnh vực theo MaLinhVuc

Trang 11

public DataSet GetLinhVuc(string strMaLinhVuc)

{

SqlParameter parMALINHVUC = new SqlParameter("@MaLinhVuc", SqlDbType.NChar, 10);

parMALINHVUC.Value = strMaLinhVuc;

_ds._command.Parameters.Add(parMALINHVUC);

return _ds.executeSelectStore("LinhVucGet");

}

// Thêm 1 lĩnh

public void AddLinhVuc(LinhVucInfo LVInfo)

{

SqlParameter parMaLinhVuc = new SqlParameter("@MaLinhVuc",

SqlDbType.NChar,10);

SqlParameter parTenLinhVuc = new SqlParameter("@TenLinhVuc",

SqlDbType.NVarChar, 255);

parMaLinhVuc.Value = LVInfo.MaLinhVuc;

parTenLinhVuc.Value = LVInfo.TenLinhVuc;

_ds._command.Parameters.Add(parMaLinhVuc);

_ds._command.Parameters.Add(parTenLinhVuc);

_ds.executeDMLStore("LinhVucAdd");

}

// Cập nhật thông tin của 1 lĩnh

public void UpdateLinhVuc(LinhVucInfo LVInfo)

{

SqlParameter parMaLinhVuc = new SqlParameter("@MaLinhVuc", SqlDbType.NChar, 10);

SqlParameter parTenLinhVuc = new SqlParameter("@TenLinhVuc",

SqlDbType.NVarChar, 255);

parMaLinhVuc.Value = LVInfo.MaLinhVuc;

parTenLinhVuc.Value = LVInfo.TenLinhVuc;

_ds._command.Parameters.Add(parMaLinhVuc);

_ds._command.Parameters.Add(parTenLinhVuc);

_ds.executeDMLStore("LinhVucUpdate");

}

// Xoá 1 lĩnh

public void DeleteLinhVuc(LinhVucInfo LVInfo)

{

SqlParameter parMaLinhVuc = new SqlParameter("@MaLinhVuc", SqlDbType.NChar, 10);

parMaLinhVuc.Value = LVInfo.MaLinhVuc;

_ds._command.Parameters.Add(parMaLinhVuc);

_ds.executeDMLStore("LinhVucDelete");

}

}

11 Tạo lớp BanTinDAL.cs trong thư mục DataAccessLayer và viết code như sau:

using System.Data.SqlClient;

/// <summary>

/// Lớp BanTinDAL thông qua lớp dịch vụ

/// DataService để thao tác dữ liệu

Trang 12

/// </summary>

public class BanTinDAL

{

//Khai báo biến _ds là lớp DataService

private DataService _ds;

//Contructor

public BanTinDAL()

{

_ds = new DataService();

}

/// Lấy danh sách Bản tin

public DataSet GetBanTin()

{

return _ds.executeSelectStore("BanTinList");

}

/// Lấy thông tin của 1 bản tin theo MaBanTin

public DataSet GetBanTin(string strMaBanTin)

{

SqlParameter parMaBanTin = new SqlParameter("@MaBanTin", SqlDbType.NChar, 10); parMaBanTin.Value = strMaBanTin;

_ds._command.Parameters.Add(parMaBanTin);

return _ds.executeSelectStore("BanTinGet");

}

/// Lấy bản tin theo MaLinhVuc

public DataSet GetBanTin_MaLinhVuc(string strMaLinhVuc)

{

SqlParameter parMaLinhVuc = new SqlParameter("@MaLinhVuc", SqlDbType.NChar, 10);

parMaLinhVuc.Value = strMaLinhVuc;

_ds._command.Parameters.Add(parMaLinhVuc);

return _ds.executeSelectStore("BanTinGetByLinhVuc");

}

//Tạo các tham số

private void CreateParameter(BanTinInfo BTInfo)

{

SqlParameter parMaBanTin = new SqlParameter("@MaBanTin", SqlDbType.NChar); SqlParameter parTieuDe = new SqlParameter("@TieuDe", SqlDbType.NVarChar); SqlParameter parNoiDungTomTat = new SqlParameter("@NoiDungTomTat",

SqlDbType.NVarChar);

SqlParameter parNoiDung = new SqlParameter("@NoiDung", SqlDbType.NVarChar); SqlParameter parNgayDangTin = new SqlParameter("@NgayDangTin",

SqlDbType.DateTime);

SqlParameter parHinhAnh = new SqlParameter("@HinhAnh", SqlDbType.NVarChar); SqlParameter parChuThichHinh = new SqlParameter("@ChuThichHinh",

SqlDbType.NVarChar);

SqlParameter parMaLinhVuc = new SqlParameter("@MaLinhVuc",

SqlDbType.NVarChar);

parMaBanTin.Value = BTInfo.MaBanTin;

parTieuDe.Value = BTInfo.TieuDe;

parNoiDungTomTat.Value = BTInfo.NoiDungTomTat;

parNoiDung.Value = BTInfo.NoiDung;

parNgayDangTin.Value = BTInfo.NgayDangTin;

parHinhAnh.Value = BTInfo.HinhAnh;

parChuThichHinh.Value = BTInfo.ChuThichHinh;

Ngày đăng: 13/09/2018, 15:00

TỪ KHÓA LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm

w