求ASP.NET三层结构说明
以下是转的:
典型的三层结构范例源码(数据层、商务层、表示层)
数据层:
数据层:
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace YiPu.WebModules.Accounts.Data
{
/// <summary>
/// User 的摘要说明。
/// </summary>
public class User : YiPu.WebModules.Data.DbObject
{
public User(string newConnectionString): base(newConnectionString)
{ }
public bool Create(
string userID,
byte[] password,
string userName,
string company,
string department,
string office,
string duties,
bool sex,
DateTime birth,
string emailAddress,
string mobile,
DateTime registerTime,
DateTime invalidTime,
string question,
string answer,
string country,
string province,
string city,
string code,
string address,
bool isRejected)
{
int rowsAffected;
SqlParameter[]
parameters = {
new SqlParameter("@UserID", SqlDbType.VarChar, 18),
new SqlParameter("@Password", SqlDbType.Binary, 12),
new SqlParameter("@UserName", SqlDbType.VarChar, 8),
new SqlParameter("@Company", SqlDbType.VarChar, 50),
new SqlParameter("@Department", SqlDbType.VarChar, 10),
new SqlParameter("@Office", SqlDbType.VarChar, 10),
new SqlParameter("@Duties", SqlDbType.VarChar, 10),
new SqlParameter("@Sex", SqlDbType.Bit),
new SqlParameter("@Birth", SqlDbType.DateTime),
new SqlParameter("@EmailAddress", SqlDbType.VarChar, 50),
new SqlParameter("@Mobile", SqlDbType.VarChar, 10),
new SqlParameter("@RegisterTime", SqlDbType.DateTime),
new SqlParameter("@InvalidTime", SqlDbType.DateTime),
new SqlParameter("@Question", SqlDbType.VarChar, 50),
new SqlParameter("@Answer", SqlDbType.VarChar, 50),
new SqlParameter("@Country", SqlDbType.VarChar, 10),
new SqlParameter("@Province", SqlDbType.VarChar, 10),
new SqlParameter("@City", SqlDbType.VarChar, 10),
new SqlParameter("@Code", SqlDbType.VarChar, 6),
new SqlParameter("@Address", SqlDbType.VarChar, 50),
new SqlParameter("@IsRejected", SqlDbType.Bit)
};
parameters[0].Value = userID;
parameters[1].Value = password;
parameters[2].Value = userName;
parameters[3].Value = company;
parameters[4].Value = department;
parameters[5].Value = office;
parameters[6].Value = duties;
parameters[7].Value = sex;
parameters[8].Value = birth;
parameters[9].Value = emailAddress;
parameters[10].Value = mobile;
parameters[11].Value = registerTime;
parameters[12].Value = invalidTime;
parameters[13].Value = question;
parameters[14].Value = answer;
parameters[15].Value = country;
parameters[16].Value = province;
parameters[17].Value = city;
parameters[18].Value = code;
parameters[19].Value = address;
parameters[20].Value = isRejected;
RunProcedure("sp_Accounts_CreateUser", parameters, out rowsAffected);
return (rowsAffected == 1);
}
public DataRow Retrieve(string userID)
{
SqlParameter[] parameters = { new SqlParameter("@UserID", SqlDbType.VarChar,18) };
parameters[0].Value = userID;
using (DataSet users = RunProcedure( "sp_Accounts_GetUserDetails", parameters, "Users" ))
{
return users.Tables[0].Rows[0];
}
}
public bool Update(
string userID,
string userName,
byte[] password,
string company,
string department,
string office,
string duties,
bool sex,
DateTime birth,
string emailAddress,
string mobile,
DateTime registerTime,
DateTime invalidTime,
string question,
string answer,
string country,
string province,
string city,
string code,
string address,
bool isRejected)
{
int rowsAffected;
SqlParameter[] parameters = {
new SqlParameter("@UserID", SqlDbType.VarChar, 18),
new SqlParameter("@Password", SqlDbType.Binary, 20),
new SqlParameter("@UserName", SqlDbType.VarChar, 8),
new SqlParameter("@Company", SqlDbType.VarChar, 50),
new SqlParameter("@Department", SqlDbType.VarChar, 10),
new SqlParameter("@Office", SqlDbType.VarChar, 10),
new SqlParameter("@Duties", SqlDbType.VarChar, 10),
new SqlParameter("@Sex", SqlDbType.Bit),
new SqlParameter("@Birth", SqlDbType.DateTime),
new SqlParameter("@EmailAddress", SqlDbType.VarChar, 50),
new SqlParameter("@Mobile", SqlDbType.VarChar, 10),
new SqlParameter("@RegisterTime", SqlDbType.DateTime),
new SqlParameter("@InvalidTime", SqlDbType.DateTime),
new SqlParameter("@Question", SqlDbType.VarChar, 50),
new SqlParameter("@Answer", SqlDbType.VarChar, 50),
new SqlParameter("@Country", SqlDbType.VarChar, 10),
new SqlParameter("@Province", SqlDbType.VarChar, 10),
new SqlParameter("@City", SqlDbType.VarChar, 10),
new SqlParameter("@Code", SqlDbType.VarChar, 6),
new SqlParameter("@Address", SqlDbType.VarChar, 50),
new SqlParameter("@IsRejected", SqlDbType.Bit),
};
parameters[0].Value = userID;
parameters[1].Value = userName;
parameters[2].Value = password;
parameters[3].Value = company;
parameters[4].Value = department;
parameters[5].Value = office;
parameters[6].Value = duties;
parameters[7].Value = sex;
parameters[8].Value = birth;
parameters[9].Value = emailAddress;
parameters[11].Value = mobile;
parameters[12].Value = registerTime;
parameters[13].Value = invalidTime;
parameters[14].Value = question;
parameters[15].Value = answer;
parameters[16].Value = country;
parameters[17].Value = province;
parameters[18].Value = city;
parameters[19].Value = code;
parameters[20].Value = address;
parameters[21].Value = isRejected;
RunProcedure("sp_Accounts_UpdateUser", parameters, out rowsAffected);
return (rowsAffected == 1);
}
public bool Delete(string userID)
{
SqlParameter[] parameters = { new SqlParameter("@UserID", SqlDbType.VarChar,18) };
int rowsAffected;
parameters[0].Value = userID;
RunProcedure( "sp_Accounts_DeleteUser", parameters, out rowsAffected );
return (rowsAffected == 1);
}
public int ValidateLogin(string userID, byte[] encPassword)
{
int rowsAffected;
SqlParameter[] parameters =
{
new SqlParameter("@UserID", SqlDbType.VarChar, 18),
new SqlParameter("@EncryptedPassword", SqlDbType.Binary, 20)
};
parameters[0].Value = userID;
parameters[1].Value = encPassword;
return RunProcedure("sp_Accounts_ValidateLogin", parameters, out rowsAffected);
}
public int TestPassword(string userID, byte[] encPassword)
{
int rowsAffected;
SqlParameter[] parameters =
{
new SqlParameter("@UserID", SqlDbType.VarChar,18),
new SqlParameter("@EncryptedPassword", SqlDbType.Binary, 20)
};
parameters[0].Value = userID;
parameters[1].Value = encPassword;
return RunProcedure("sp_Accounts_TestPassword", parameters, out rowsAffected);
}
public ArrayList GetUserRoles( string userID )
{
ArrayList roles = new ArrayList();
SqlParameter[] parameters = { new SqlParameter("@UserID", SqlDbType.VarChar,18) };
parameters[0].Value = userID;
SqlDataReader tmpReader = RunProcedure("sp_Accounts_GetUserRoles", parameters);
while (tmpReader.Read())
{
roles.Add( tmpReader.GetString(1) );
}
Connection.Close();
return roles;
}
public ArrayList GetEffectivePermissionList( string userID )
{
ArrayList permissions = new ArrayList();
SqlParameter[] parameters = { new SqlParameter("@UserID", SqlDbType.VarChar,18) };
parameters[0].Value = userID;
SqlDataReader tmpReader = RunProcedure("sp_Accounts_GetEffectivePermissionList", parameters);
while (tmpReader.Read())
{
permissions.Add( tmpReader.GetInt32(0) );
}
Connection.Close();
return permissions;
}
public DataSet GetUserList()
{
return RunProcedure("sp_Accounts_GetUsers", new IDataParameter[]{}, "Users");
}
public bool AddRole(string userID, int roleID)
{
int rowsAffected;
SqlParameter[] parameters = {
new SqlParameter("@UserID", SqlDbType.VarChar, 18),
new SqlParameter("@RoleID", SqlDbType.Int, 4)
};
parameters[0].Value = userID;
parameters[1].Value = roleID;
RunProcedure("sp_Accounts_AddUserToRole", parameters, out rowsAffected);
return (rowsAffected == 1);
}
public bool RemoveRole(String userID, int roleID)
{
int rowsAffected;
SqlParameter[] parameters = {
new SqlParameter("@UserID", SqlDbType.VarChar,18),
new SqlParameter("@RoleID", SqlDbType.Int,4 )
};
parameters[0].Value = userID;
parameters[1].Value = roleID;
RunProcedure("sp_Accounts_RemoveUserFromRole", parameters, out rowsAffected);
return (rowsAffected == 1);
}
}
}