技术开发 频道

ASP.NET简单的三层程序设计实例解读

  下面用一用户登陆演示项目

  DBEntity添加UserInfo.cs,代表数据库实体,一般是和数据库一一对应的

using System;
using System.Collections.Generic;
using System.Text;

namespace DBEntity
{
    
public class UserInfo
    {
        
private int _id;
        
private string _userName;
        
private string _passWord;

        
public int Id
        {
            
get { return _id; }
            
set { _id = value; }
        }

        
public string UserName
        {
            
get { return _userName; }
            
set { _userName = value; }
        }
      
        
public string PassWord
        {
            
get { return _passWord; }
            
set { _passWord = value; }
        }
    }
}

 

  DAL里添加UserDAL.cs

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using DBEntity;

namespace DAL
{
    
public class UserDAL
    {
        
private string ConnectionString = ConfigurationManager.AppSettings["ConnectionString"].ToString();
        
public UserInfo Login(string userName, string passWord)
        {
            UserInfo info
= new UserInfo();
            
string strSql = "select id,userName,passWord from Users where userName=@userName and passWord=@passWord";
            SqlConnection conn
= new SqlConnection(ConnectionString);
            conn.Open();
            SqlCommand com
= new SqlCommand();
            com.CommandType
= CommandType.Text;
            com.CommandText
= strSql;
            com.Connection
= conn;
            com.Parameters.AddWithValue(
"@userName", userName);
            com.Parameters.AddWithValue(
"@passWord", passWord);
            SqlDataReader dr
= com.ExecuteReader(CommandBehavior.CloseConnection);
            
if (dr.Read())
            {
                info.Id
= Convert.ToInt32(dr["id"]);
                info.UserName
= dr["userName"].ToString();
                info.PassWord
= dr["passWord"].ToString();
                return info;
            }
            
else
            {
                return
null;
            }
        }
    }
}

 

  BLL里添加UserBLL.cs

using System;
using System.Collections.Generic;
using System.Text;
using DBEntity;
using DAL;

namespace BLL
{
    
public class UserBLL
    {
        UserDAL dal
= new UserDAL();
        
public UserInfo Login(string userName, string passWord)
        {
            return dal.Login(userName, passWord);
        }
    }
}

 

  Web里Login.aspx对应的后台代码

using System;
using BLL;
using DBEntity;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(
object sender, EventArgs e)
    {

    }
    protected void Button1_Click(
object sender, EventArgs e)
    {
        UserBLL data
= new UserBLL();
        UserInfo info
= new UserInfo();
        info
= data.Login(TextBox1.Text, TextBox2.Text);
        
if (info != null)
        {
            
//登陆成功
            Response.Write(
"<script>alert(OK!)</script>");
        }
        
else
        {
            
//登陆失败
            Response.Write(
"<script>alert(ERROR!)</script>");
        }
    }
}

 

  至此,简单的三层架构用户登陆完成了!

0
相关文章