首页 > 代码库 > ASP.NET 数据访问层对用户列表的操作封装

ASP.NET 数据访问层对用户列表的操作封装

用户列表对数据库的操作 使用到了模型MODEL层和DAL数据访问

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using CZBK.TestProject.Model;
using System.Data.SqlClient;

namespace CZBK.TestProject.DAL
{
public class EmployeeDal
{

public List<Employee> GetEntityList()
{
string sql = "select * from Employee";
DataTable dt =SqlHelper.GetTable(sql, CommandType.Text);
List<Employee> list = null;
if (dt.Rows.Count > 0)
{
list = new List<Employee>();
Employee employee = null;
foreach(DataRow rows in dt.Rows)
{
employee = new Employee();
LoadEntity(rows, employee);
list.Add(employee);
}
}
return list;
}

private void LoadEntity(DataRow row,Employee employee)
{
employee.EmpId = Convert.ToInt32(row["EmpId"]);
employee.EmpName = row["EmpName"] != DBNull.Value ? row["EmpName"].ToString() : string.Empty;
employee.EmpAge = Convert.ToInt32(row["EmpAge"]);
employee.DelFlag = Convert.ToInt32(row["DelFlag"]);
}

public Employee GetEntityModel(int id)
{
string sql = "select * from employee where empid=@id";
DataTable dt = SqlHelper.GetTable(sql, CommandType.Text, new SqlParameter("@id", id));
Employee employee = null;
if (dt.Rows.Count > 0)
{
employee = new Employee();
LoadEntity(dt.Rows[0],employee);
}
return employee;
}

public int DeleteEmployee(int id)
{
string sql = "DELETE FROM Employee WHERE EmpId=@id";
return SqlHelper.ExecuteNoQuery(sql, CommandType.Text, new SqlParameter("@id",id));
}

public int UpdateEmployee(Employee em)
{
string sql = "Update employee set EmpName=@EmpName,EmpAge=@EmpAge,DelFlag=@DelFlag WHERE EmpId=@id";
SqlParameter[] pare = { new SqlParameter("@id", SqlDbType.Int), new SqlParameter("@EmpName", SqlDbType.VarChar), new SqlParameter("@EmpAge", SqlDbType.VarChar), new SqlParameter("@DelFlag", SqlDbType.VarChar) };
pare[0].Value = http://www.mamicode.com/em.EmpId;
pare[1].Value =http://www.mamicode.com/em.EmpName ;
pare[2].Value = http://www.mamicode.com/em.EmpAge;
pare[3].Value = http://www.mamicode.com/em.DelFlag;
return SqlHelper.ExecuteNoQuery(sql, CommandType.Text, pare);
}
}
}

ASP.NET 数据访问层对用户列表的操作封装