首先介紹一下MVC的意義,和各個層面之間的用途和功能。mysql
1)實體層。主要用來聲明在視圖層和業務邏輯層之間傳遞數據的載體。一般表明來源與項目數據庫中一個或者多個表組成的一條有意義的記錄。算法
2)業務邏輯層。根據業務邏輯向視圖層提供數據,這個項目中的類有權根據業務邏輯來決定是否調用數據庫訪問層的方法sql
3)數據庫訪問層。項業務邏輯層提供訪問數據的方法。數據庫
4)視圖層。主要是以網站開發爲例。用於提供顯示,添加,編輯和刪除數據。數組
我在VS中建立的項目的結構圖:網站
NetMVC就是所謂的視圖層。Entity是實體層,用來聲明開發過程當中被要使用的全部變量。DAL是數據庫訪問層,主要完成全部操做對數據庫的訪問。BLL是業務邏輯層,處理視圖層傳來的業務邏輯,而後傳遞給數據庫訪問層進行處理。ui
本例主要演示的是登錄頁面的實現,Entity層的代碼以下:編碼
- using System;
- using System.Collections.Generic;
- using System.Text;
-
- namespace Entity
- {
- public class UserInfo
- {
- private int UserId;
-
-
-
-
- public int UserId1
- {
- get { return UserId; }
- set { UserId = value; }
- }
- private string username;
-
-
-
-
- public string Username
- {
- get { return username; }
- set { username = value; }
- }
-
- private string password;
-
-
-
-
- public string Password
- {
- get { return password; }
- set { password = value; }
- }
- private int loginCount;
-
-
-
-
- public int LoginCount
- {
- get { return loginCount; }
- set { loginCount = value; }
- }
- private DateTime regDate;
-
-
-
-
- public DateTime RegDate
- {
- get { return regDate; }
- set { regDate = value; }
- }
- private DateTime lastLoginDate;
-
-
-
-
- public DateTime LastLoginDate
- {
- get { return lastLoginDate; }
- set { lastLoginDate = value; }
- }
- private bool isForbidden;
- private string passwordQuestion;
-
-
-
-
- public string PasswordQuestion
- {
- get { return passwordQuestion; }
- set { passwordQuestion = value; }
- }
- private string passwordAnswer;
-
-
-
-
- public string PasswordAnswer
- {
- get { return passwordAnswer; }
- set { passwordAnswer = value; }
- }
-
-
- }
- }
完成對實體類的建立,接着就要完成對數據庫訪問層的建立(其中須要用到上篇問章所寫的SqlHelper數據庫訪問通用類http://blog.csdn.net/yisuowushinian/article/details/7999305),代碼以下,加密
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
- using Entity;
-
- namespace DAL
- {
- public class UserDal
- {
- private string connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
-
-
-
-
-
-
- public bool AddUser(UserInfo info)
- {
- string sql = "insert into Users(Username,password) values('@Username','@password')";
- SqlParameter[] parameters = new SqlParameter[4];
- parameters[0] = new SqlParameter("@Username", SqlDbType.NVarChar, 30);
- parameters[0].Value = info.Username;
-
- parameters[1] = new SqlParameter("password", SqlDbType.VarChar, 50);
- parameters[1].Value = info.Password;
-
-
- return new SqlDbHelper(connectionString).ExecuteNonQuery(sql) > 0;
- }
-
-
-
-
-
-
- public bool DeleteUser(int UserId)
- {
- string sql = "delete from users where UserId=" + UserId;
- return new SqlDbHelper(connectionString).ExecuteNonQuery(sql) > 0;
- }
-
-
-
-
-
-
- public bool UpDateUser(UserInfo info)
- {
- string sql = "update users set password=@password,loginCount=@loginCount where userid=@userid";
- SqlParameter[] parameters = new SqlParameter[7];
- parameters[0] = new SqlParameter("@password", SqlDbType.VarChar, 30);
- parameters[0].Value = info.Password;
-
- parameters[1] = new SqlParameter("@loginCount", SqlDbType.Int, 4);
- parameters[1].Value = info.LoginCount;
-
- return new SqlDbHelper(connectionString).ExecuteNonQuery(sql) > 0;
- }
-
-
-
-
-
-
- public DataTable GetUser(int userId)
- {
- string sql = "select * from users where userId=@UserId";
- SqlParameter[] parameters = new SqlParameter[1];
- parameters[0] = new SqlParameter("@UserId", SqlDbType.Int, 4);
- parameters[0].Value = userId;
- return new SqlDbHelper(connectionString).ExecuteDataTable(sql, CommandType.Text, parameters);
- }
-
-
-
-
-
-
- public DataTable GetUser(string Username)
- {
- string sql = "select * from users where username=@username";
- SqlParameter[] parameters = new SqlParameter[1];
- parameters[1] = new SqlParameter("@username", SqlDbType.NVarChar, 30);
- parameters[1].Value = Username;
-
- return new SqlDbHelper(connectionString).ExecuteDataTable(sql, CommandType.Text, parameters);
- }
-
-
-
-
-
-
-
- public DataTable GetUserList(int startIndex, int size)
- {
- string sql = "select top " + size + " * from users where UserId not in (select top " + startIndex + " UserId from Users order by UserId asc)order by UserId asc";
- return new SqlDbHelper(connectionString).ExecuteDataTable(sql);
- }
-
-
-
-
-
- public int GetUserCount()
- {
- string sql = "select count(1) from Users";
- return int.Parse(new SqlDbHelper(connectionString).ExecuteScalar(sql).ToString());
- }
- }
- }
而後建立業務邏輯層,代碼以下:spa
這個就完成了對數據庫訪問層,實體層,業務邏輯層的建立工做。由於在建立過程當中被,有不少的重複的工做,我爲了加快速度,就省略了一些的代碼。
在登錄頁面使用時,只須要引入業務邏輯層的方法,而後根據其中的方法,傳入相應的參數,就能夠完成全部的操做。