网站功能模块的实现——BLL文件夹

来源:互联网 发布:opencv编程案例详解pdf 编辑:程序博客网 时间:2024/06/09 23:55

在web.config中配置文件

<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="myFoodCon" connectionString="server=(local);uid=sa;pwd=sa;database=jk800;"/>

</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<pages>
<controls>
<add assembly="Bjxmh.Jingci" namespace="Bjxmh.Jingci.Controls" tagPrefix="jingci" />
</controls>
</pages>
</system.web>
</configuration>

创建Bll文件夹

Category.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Anylen.Base.SqlServer;
using System.Data.SqlClient;


namespace Bjxmh.Jingci {


/// <summary>
/// 食物类别数据库访问类
/// </summary>
public class Category {
private static string SqlCon = System.Configuration.ConfigurationManager.ConnectionStrings["myFoodCon"].ConnectionString;
private const string SQL_INSERT = "insert into food_Category (name,imageurl) values (@name,@imageurl)";
private const string SQL_UPDATE = "update food_Category set name=@name,imageurl=@imageurl where id=@id";
private const string SQL_DELETE = "delete food_Category where id=@id";
private const string SQL_SELECT_SINGLE = "select id,name,ImageUrl from food_Category where id=@id";
private const string SQL_SELECT_ALL = "select id,name,ImageUrl from food_Category";
/// <summary>
/// 添加栏目
/// </summary>
/// <param name="c">栏目名称</param>
public void AddCateogoryInfo(CategoryInfo c) {
IList<CategoryInfo> infoes = GetCategoryInfoes();
bool isHave = false;
foreach (CategoryInfo info in infoes) {
if (info.Title == c.Title) {
isHave = true;
break;
}
}


if (isHave) {
throw new Exception(string.Format("名称为{0}的栏目已经存在,不能重复添加。", c));
}


SqlParameter[] parm = new SqlParameter[] {  
new SqlParameter("@Title", c.Title),
new SqlParameter("@imageUrl", c.ImageUrl)
};
SqlHelper.ExecuteNonQuery(SqlCon, CommandType.Text, SQL_INSERT, parm);
}


/// <summary>
/// 增加栏目
/// </summary>
/// <param name="c"></param>
public void UpdateCateogoryInfo(CategoryInfo c) {
IList<CategoryInfo> infoes = GetCategoryInfoes();
bool isHave = false;
foreach (CategoryInfo info in infoes) {
if (info.Title == c.Title && info.Id !=c.Id) {
isHave = true;
break;
}
}


if (isHave) {
throw new Exception(string.Format("名称为{0}的栏目已经存在,不能重复添加。", c.Title));
}


SqlParameter[] parms = new SqlParameter[]{
new SqlParameter("@name", SqlDbType.NVarChar, 20),
new SqlParameter("@id",SqlDbType.Int),
new SqlParameter("@imageUrl", SqlDbType.NVarChar, 100)
};
parms[0].Value = c.Title;
parms[1].Value = c.Id;
parms[2].Value = c.ImageUrl;


SqlHelper.ExecuteNonQuery(SqlCon, CommandType.Text, SQL_UPDATE, parms);
}


/// <summary>
/// 删除栏目
/// </summary>
/// <param name="id">id</param>
public void DeleteCategoryInfo(int id) {
SqlParameter parm = new SqlParameter("@id", SqlDbType.Int);
parm.Value = id;


SqlHelper.ExecuteNonQuery(SqlCon, CommandType.Text, SQL_DELETE, parm);
}


/// <summary>
/// 删除栏目
/// </summary>
/// <param name="name">栏目名称</param>
public void DeleteCategoryInfo(string name) {
IList<CategoryInfo> infoes = GetCategoryInfoes();
foreach (CategoryInfo info in infoes) {
if (info.Title == name) {
DeleteCategoryInfo(info.Id);
break;
}
}
}


/// <summary>
/// 返回指定id栏目
/// </summary>
/// <param name="id">id</param>
/// <returns></returns>
public CategoryInfo GetCategoryInfoById(int id) {
CategoryInfo categoryInfo = null;
SqlParameter parm = new SqlParameter("@id", SqlDbType.Int);
parm.Value = id;
using (SqlDataReader sdr = SqlHelper.ExecuteReader(SqlCon, CommandType.Text, SQL_SELECT_SINGLE, parm)) {
if (sdr.Read()) {
categoryInfo = new CategoryInfo() {
Id = sdr.GetInt32(0),
Title = sdr.GetString(1)
};
if (sdr.IsDBNull(2))
categoryInfo.ImageUrl = string.Empty;
else
categoryInfo.ImageUrl = sdr.GetString(2);
}
}
return categoryInfo;
}


/// <summary>
/// 返回所有的栏目
/// </summary>
/// <returns></returns>
public IList<CategoryInfo> GetCategoryInfoes() {


IList<CategoryInfo> infoes = new List<CategoryInfo>();
using (SqlDataReader sdr = SqlHelper.ExecuteReader(SqlCon, CommandType.Text, SQL_SELECT_ALL)) {
while (sdr.Read()) {
var info = new CategoryInfo() 
{ Id = sdr.GetInt32(0), 
Title = sdr.GetString(1) };
if (sdr.IsDBNull(2))
info.ImageUrl = string.Empty;
else
info.ImageUrl = sdr.GetString(2);
infoes.Add(info);
}
}
return infoes;
}
}
}

创建文件food.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Anylen.Base.SqlServer;
using System.Data.SqlClient;


namespace Bjxmh.Jingci {
/// <summary>
/// 文章访问数据库
/// </summary>
public class Food {
private static string SqlCon = System.Configuration.ConfigurationManager.ConnectionStrings["myFoodCon"].ConnectionString;
private const string SQL_INSERT = "insert into food_Content (CategoryId, imageurl, Title, FullTitle, Renqun, Hot) values (@CategoryId,@imageurl,@Title,@FullTitle,@Renqun,@Hot)";
private const string SQL_UPDATE = "update food_Content set CategoryId=@CategoryId,FullTitle=@FullTitle, Title=@Title, Renqun=@Renqun,Hot=@Hot,imageurl=@imageurl where id=@id";
private const string SQL_DELETE = "delete food_Content where id=@id";
private const string SQL_SELECT_SINGLE = "select id,categoryid,title,fulltitle,renqun,hot,createtime,imageurl from food_Content where id=@id";
private const string SQL_SELECT_DOUBLE = "select id, CategoryId, Title, FullTitle, Renqun, Hot, CreateTime, ImageUrl from food_Content where categoryid =@categoryid";
private const string SQL_SELECT_ALL = "select id,categoryid,title,fulltitle,renqun,hot,createtime,imageurl from food_content";

/// <summary>
/// 添加内容
/// </summary>
/// <param name="c"></param>
public void AddContentInfoes(FoodInfo c) {
IList<FoodInfo> infoes = GetContentInfoes();
bool isHave = false;
foreach (FoodInfo info in infoes) {
if (info.Title == c.Title) {
isHave = true;
break;
}
}


if (isHave) {
throw new Exception(string.Format("名称为{0}的文章已经存在,不能重复添加。", c.Title));
}
SqlParameter[] parm = new SqlParameter[] { 
new SqlParameter("@CategoryId", c.CategoryId), 
new SqlParameter("@Title", c.Title),
new SqlParameter("@FullTitle", c.FullTitle),
new SqlParameter("@Renqun", c.Renqun),
new SqlParameter("@Hot", c.Hot),
new SqlParameter("@imageUrl", c.ImageUrl),
};

SqlHelper.ExecuteNonQuery(SqlCon, CommandType.Text, SQL_INSERT, parm);
}
/// <summary>
/// 更新
/// </summary>
/// <param name="c"></param>
public void UpdateContentInfo(FoodInfo c) {
IList<FoodInfo> infoes = GetContentInfoes();
bool isHave = false;
foreach (FoodInfo info in infoes) {
if (info.Title == c.Title && info.Id != c.Id) {
isHave = true;
break;
}
}


if (isHave) {
throw new Exception(string.Format("名称为{0}的文章已经存在,不能重复添加。", c.Title));
}


SqlParameter[] parm = new SqlParameter[] { 
new SqlParameter("@id", c.Id), 
new SqlParameter("@CategoryId", c.CategoryId), 
new SqlParameter("@Title", c.Title),
new SqlParameter("@FullTitle", c.FullTitle),
new SqlParameter("@Renqun", c.Renqun),
new SqlParameter("@imageUrl", c.ImageUrl),
new SqlParameter("@Hot", c.Hot)
};


SqlHelper.ExecuteNonQuery(SqlCon, CommandType.Text, SQL_UPDATE, parm);
}


/// <summary>
/// 删除文章
/// </summary>
/// <param name="id"></param>
public void DeleteContentInfo(int id) {
SqlParameter parm = new SqlParameter("@id", SqlDbType.Int);
parm.Value = id;


SqlHelper.ExecuteNonQuery(SqlCon, CommandType.Text, SQL_DELETE, parm);
}
///
public void DeleteContentInfo(FoodInfo c) {
IList<FoodInfo> infoes = GetContentInfoes();
foreach (FoodInfo info in infoes) {
if (info.Title == c.Title) {
DeleteContentInfo(info.Id);
break;
}
}
}


/// <summary>
/// 返回指定id栏目
/// </summary>
/// <param name="id">id</param>
/// <returns></returns>
public FoodInfo GetContentInfoById(int id) {
FoodInfo foodInfo = null;


SqlParameter parm = new SqlParameter("@id", SqlDbType.Int);
parm.Value = id;
using (SqlDataReader sdr = SqlHelper.ExecuteReader(SqlCon, CommandType.Text, SQL_SELECT_SINGLE, parm)) {
if (sdr.Read()) {
foodInfo = new FoodInfo() {
Id = sdr.GetInt32(0),
CategoryId = sdr.GetInt32(1),
Title = sdr.GetString(2),
FullTitle = sdr.GetString(3),
Renqun = sdr.GetString(4),
Hot = sdr.GetInt32(5),
CreateTime = sdr.GetDateTime(6)
};
if (sdr.IsDBNull(7))
foodInfo.ImageUrl = string.Empty;
else
foodInfo.ImageUrl = sdr.GetString(7);
}
}
return foodInfo;
}
/// <summary>
/// 返回同类别的所有文章
/// </summary>
/// <param name="categoryId"></param>
/// <returns></returns>
public IList<FoodInfo> GetContentInfoByCategoryId(int categoryId) {
SqlParameter parm = new SqlParameter("@categoryid", SqlDbType.Int);
parm.Value = categoryId;
IList<FoodInfo> Cateinfoes = new List<FoodInfo>();
using (SqlDataReader sdr = SqlHelper.ExecuteReader(SqlCon, CommandType.Text, SQL_SELECT_DOUBLE, parm)) {
while (sdr.Read()) {
var Cateinfo = new FoodInfo() {
Id = sdr.GetInt32(0),
CategoryId = sdr.GetInt32(1),
Title = sdr.GetString(2),
FullTitle = sdr.GetString(3),
Renqun = sdr.GetString(4),
Hot = sdr.GetInt32(5),
CreateTime = sdr.GetDateTime(6)
};
if (sdr.IsDBNull(7))
Cateinfo.ImageUrl = string.Empty;
else
Cateinfo.ImageUrl = sdr.GetString(7);
Cateinfoes.Add(Cateinfo);
}
}
return Cateinfoes;
}


/// <summary>
/// 返回所有的文章
/// </summary>
/// <returns></returns>
public IList<FoodInfo> GetContentInfoes() {


IList<FoodInfo> infoes = new List<FoodInfo>();
using (SqlDataReader sdr = SqlHelper.ExecuteReader(SqlCon, CommandType.Text, SQL_SELECT_ALL)) {
while (sdr.Read()) {
var info = new FoodInfo() {
Id = sdr.GetInt32(0),
CategoryId = sdr.GetInt32(1),
Title = sdr.GetString(2),
FullTitle = sdr.GetString(3),
Renqun = sdr.GetString(4),
Hot = sdr.GetInt32(5),
CreateTime = sdr.GetDateTime(6)
};
if (sdr.IsDBNull(7))
info.ImageUrl = string.Empty;
else
info.ImageUrl = sdr.GetString(7);
infoes.Add(info);
}
}
return infoes;
}
}
}