MVC三层架构
时间:2010-09-17 来源:eva.xiao
Entities_ItemCategoryInfo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Entities
{
public class ItemCategoryInfo
{
private int _CategoryID;
public int CategoryID
{
get { return _CategoryID; }
set { _CategoryID = value; }
}
private string _CategoryName;
public string CategoryName
{
get { return _CategoryName; }
set { _CategoryName = value; }
}
}
}
Business_ItemCategoryBLL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using DALC;
namespace Business
{
public class ItemCategoryBLL
{
ItemCategoryDALC itemCategoryDALC = new ItemCategoryDALC();
public List<ItemCategoryInfo> GetAllCategoryList()
{
return itemCategoryDALC.GetAllCategoryList();
}
public List<ItemCategoryInfo> GetCategoryList(string CategoryName)
{
return itemCategoryDALC.GetCategoryList(CategoryName);
}
public int InsertCategory(string CategoryName)
{
return itemCategoryDALC.InsertCategory(CategoryName);
}
public int UpdateCategory(string NewName, int CategoryID)
{
return itemCategoryDALC.UpdateCategory(NewName, CategoryID);
}
public int DeleteCategory(int CategoryID)
{
return itemCategoryDALC.DeleteCategory(CategoryID);
}
}
}
DALC_ItemCategoryDALC
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using System.Data.SqlClient;
namespace DALC
{
public class ItemCategoryDALC
{
SqlConnection _con = new SqlConnection(@"Data Source=stkwx028\sqlexpress;Initial Catalog=WMDemo_JDE;Integrated Security=True");
List<ItemCategoryInfo> ItemCategoryList = new List<ItemCategoryInfo>();
public List<ItemCategoryInfo> GetAllCategoryList()//get all Category List
{
List<ItemCategoryInfo> CategoryList = new List<ItemCategoryInfo>();
SqlCommand cmd = new SqlCommand();
cmd.Connection = _con;
cmd.CommandText = "SELECT CategoryID,CategoryName FROM ItemCategory";
_con.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
ItemCategoryInfo itemCategoryInfo = new ItemCategoryInfo();
itemCategoryInfo.CategoryID = Convert.ToInt32(reader["CategoryID"]);
itemCategoryInfo.CategoryName = reader["CategoryName"].ToString();
ItemCategoryList.Add(itemCategoryInfo);
}
if (reader != null)
{
reader.Close();
}
_con.Close();
return ItemCategoryList;
}
else
{
if (reader != null)
{
reader.Close();
}
_con.Close();
return null;
}
}
public List<ItemCategoryInfo> GetCategoryList(string CategoryName)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = _con;
cmd.CommandText = "SELECT * FROM ItemCategory" +
" WHERE CategoryName LIKE '%'+@CategoryName+'%'" +
" ORDER BY CategoryID";
cmd.Parameters.AddWithValue("@CategoryName", CategoryName);
_con.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
ItemCategoryInfo itemCategoryInfo = new ItemCategoryInfo();
itemCategoryInfo.CategoryID = Convert.ToInt32(reader["CategoryID"]);
itemCategoryInfo.CategoryName = reader["CategoryName"].ToString();
ItemCategoryList.Add(itemCategoryInfo);
}
if (reader != null)
{
reader.Close();
}
_con.Close();
return ItemCategoryList;
}
else
{
if (reader != null)
{
reader.Close();
}
_con.Close();
return null;
}
}
public int InsertCategory(string CategoryName)
{
ItemCategoryInfo itemCategoryInfo = new ItemCategoryInfo();
SqlCommand cmd = new SqlCommand();
cmd.Connection = _con;
cmd.CommandText = "INSERT INTO ItemCategory(CategoryName)" +
" VALUES(@CategoryName)";
cmd.Parameters.AddWithValue("@CategoryName", CategoryName);
_con.Open();
int i = cmd.ExecuteNonQuery();
_con.Close();
return i;
}
public int UpdateCategory(string NewName,int CategoryID)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = _con;
cmd.CommandText = "UPDATE ItemCategory" +
" SET CategoryName=@NewName" +
" WHERE CategoryID=@CategoryID";
cmd.Parameters.AddWithValue("@NewName", NewName);
//cmd.Parameters.AddWithValue("@OldName", OldName);
cmd.Parameters.AddWithValue("@CategoryID", CategoryID);
_con.Open();
int i = cmd.ExecuteNonQuery();
_con.Close();
return i;
}
public int DeleteCategory(int CategoryID)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = _con;
cmd.CommandText = "DELETE FROM ItemCategory" +
" WHERE CategoryID=@CategoryID";
cmd.Parameters.AddWithValue("@CategoryID", CategoryID);
_con.Open();
int i = cmd.ExecuteNonQuery();
_con.Close();
return i;
}
}
}