今天看了三层架构,所以我写了个Login的Test。
DAO.aspx
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using UserModel;
namespace UserDAL
{
public class DAO
{
public UserModel.Class1 excute(string sql)
{
using (SqlConnection conn = new SqlConnection(Utils.GetConString()))
{
UserModel.Class1 user = null;
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = sql;
cmd.Connection = conn;
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
if (user == null)
{
user = new UserModel.Class1();
}
user.UserName = sdr.GetString(0);
user.Password = sdr.GetString(1);
}
return user;
}
}
}
}
BAO.aspx
using System;
using System.Collections.Generic;
using System.Text;
namespace BLL
{
public class BAO
{
public UserModel.Class1 CheckUserName(string userName,string password)
{
string sql = "select userName,password from Users where userName='"+userName+"' and password='"+password+"'";
UserDAL.DAO dao = new UserDAL.DAO();
UserModel.Class1 user=dao.excute(sql);
return user;
}
}
}
Utils.aspx
namespace UserDAL
{
public class Utils
{
public static string GetConString()
{
string mailto:strCon=@%22server=JERRY\SQLEXPRESS;database=jerryzhang;uid=sa;password=123456;";
return strCon;
}
}
}
UserModel
using System;
using System.Collections.Generic;
using System.Text;
namespace UserModel
{
public class Class1
{
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 string email;
public string Email
{
get { return email; }
set { email = value; }
}
private int id;
public int ID
{
get { return id; }
set { id = value; }
}
}
}
Login.aspx
protected void btnLogin_Click(object sender, EventArgs e)
{
BLL.BAO bao = new BAO();
string userName=this.txtUserName.Text.Trim();
string password = this.txtPassword.Text;
UserModel.Class1 user= bao.CheckUserName(userName,password);
Response.Write("Login sucessfully");
}
虽然有些东西为了简单点没有判断,不过感觉还不错!