信息发布系统 Jquery+MVC架构开发(5)DAL层

 

跟model层一样,我们同样可以用动软代码生成器来生成DAL层。

这一层主要是数据库的访问,不再多介绍.

需要说明的是我们要把微软的SqlHelper类引入我们的项目,这样我们访问数据库用这个类就够了,关于这个类大伙可以参见微软petshop中的SqlHelper类。

 

1.  首先定义接口

我们创建三个接口:

       

1)  IInfo

InfoResult Add(Info info);
InfoResult Update(Info info);
InfoResult Delete(int infoId);
InfoList GetInfoList(SearchInfo searchInfo);
InfoList GetInfoById(int infoId);

2)  IInfoType
跟上面相同,不再赘述。

3)  IUserInfo
跟上面相同,不再赘述。

 

2.实现这些接口,总结完毕后,我会把代码上传上来,现在暂时把有一部分代码放到这里

     于InfoDal为例:

      

using System;

using System.Collections.Generic;

using System.Data;

using System.Data.SqlClient;

using System.Linq;

using System.Text;

using InfoPub.Modal;

 

namespace InfoPub.DAL

{

    public class InfoDal:IInfo

    {

        public InfoResult Add(Info model)

        {

            InfoResult infoResult = new InfoResult();

            StringBuilder strSql = new StringBuilder();

            try

            {

                strSql.Append("insert into Info(");

                strSql.Append("InfoId,infoname,InfoContent,TypeId,PictureUrl,CreateId,CreateDate,ModifyDate,AttachMentUrl,IsTop)");

                strSql.Append(" values (");

                strSql.Append("@InfoId,@infoname,@InfoContent,@TypeId,@PictureUrl,@CreateId,@CreateDate,@ModifyDate,@AttachMentUrl,@IsTop)");

                SqlParameter[] parameters = {

                                               new SqlParameter("@InfoId", SqlDbType.Int,4),

                                               new SqlParameter("@infoname", SqlDbType.NVarChar,200),

                                               new SqlParameter("@InfoContent", SqlDbType.Text),

                                               new SqlParameter("@TypeId", SqlDbType.Int,4),

                                               new SqlParameter("@PictureUrl", SqlDbType.NVarChar,4000),

                                               new SqlParameter("@CreateId", SqlDbType.Int,4),

                                               new SqlParameter("@CreateDate", SqlDbType.DateTime),

                                               new SqlParameter("@ModifyDate", SqlDbType.DateTime),

                                               new SqlParameter("@AttachMentUrl", SqlDbType.NVarChar,4000),

                                               new SqlParameter("@IsTop", SqlDbType.TinyInt,1)};

 

                parameters[0].Value = model.InfoId;

                parameters[1].Value = model.Infoname;

                parameters[2].Value = model.InfoContent;

                parameters[3].Value = model.TypeId;

                parameters[4].Value = model.PictureUrl;

                parameters[5].Value = model.CreateId;

                parameters[6].Value = model.CreateDate;

                parameters[7].Value = model.ModifyDate;

                parameters[8].Value = model.AttachMentUrl;

                parameters[9].Value = model.IsTop;

 

                SqlHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters);

                infoResult.Code = 0;

            }

            catch (Exception ex)

            {

                infoResult.Code = 100;

                infoResult.Message = ex.Message + ex.StackTrace + ex.InnerException;

            }

           

 

            return infoResult;

        }

        public InfoResult Update(Info model)

        {

            InfoResult infoResult = new InfoResult();

            StringBuilder strSql = new StringBuilder();

            try

            {

                strSql.Append("update Info set ");

                strSql.Append("infoname=@infoname,");

                strSql.Append("InfoContent=@InfoContent,");

                strSql.Append("TypeId=@TypeId,");

                strSql.Append("PictureUrl=@PictureUrl,");

                strSql.Append("CreateId=@CreateId,");

                strSql.Append("CreateDate=@CreateDate,");

                strSql.Append("ModifyDate=@ModifyDate,");

                strSql.Append("AttachMentUrl=@AttachMentUrl,");

                strSql.Append("IsTop=@IsTop");

                strSql.Append(" where InfoId=@InfoId ");

                SqlParameter[] parameters = {

                                               new SqlParameter("@infoname", SqlDbType.NVarChar,200),

                                               new SqlParameter("@InfoContent", SqlDbType.Text),

                                               new SqlParameter("@TypeId", SqlDbType.Int,4),

                                               new SqlParameter("@PictureUrl", SqlDbType.NVarChar,4000),

                                               new SqlParameter("@CreateId", SqlDbType.Int,4),

                                               new SqlParameter("@CreateDate", SqlDbType.DateTime),

                                               new SqlParameter("@ModifyDate", SqlDbType.DateTime),

                                               new SqlParameter("@AttachMentUrl", SqlDbType.NVarChar,4000),

                                               new SqlParameter("@IsTop", SqlDbType.TinyInt,1),

                                               new SqlParameter("@InfoId", SqlDbType.Int,4)};

                parameters[0].Value = model.Infoname;

                parameters[1].Value = model.InfoContent;

                parameters[2].Value = model.TypeId;

                parameters[3].Value = model.PictureUrl;

                parameters[4].Value = model.CreateId;

                parameters[5].Value = model.CreateDate;

                parameters[6].Value = model.ModifyDate;

                parameters[7].Value = model.AttachMentUrl;

                parameters[8].Value = model.IsTop;

                parameters[9].Value = model.InfoId;

                SqlHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters);

                infoResult.Code = 0;

 

            }

            catch (Exception ex)

            {

                infoResult.Code = 100;

                infoResult.Message = ex.Message + ex.StackTrace + ex.InnerException;

            }

 

            return infoResult;

           

        }

        public InfoResult Delete(int infoId)

        {

            InfoResult infoResult = new InfoResult();

            StringBuilder strSql = new StringBuilder();

            try

            {

                strSql.Append("delete from Info ");

                strSql.Append(" where InfoId=@InfoId ");

                SqlParameter[] parameters = {

                                               new SqlParameter("@InfoId", SqlDbType.Int,4)};

                parameters[0].Value = infoId;

                SqlHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters);

                infoResult.Code = 0;

            }

            catch (Exception ex)

            {

                infoResult.Code = 100;

                infoResult.Message = ex.Message + ex.StackTrace + ex.InnerException;

               

            }

            return infoResult;

        }

        // SearchInfo是自定义的查询类

        public InfoList GetInfoList(SearchInfo searchInfo)

        {

            InfoList infoList=new InfoList();

            StringBuilder strSql = new StringBuilder();

            StringBuilder strwhere=new StringBuilder();

            if (!string.IsNullOrEmpty(searchInfo.Infoname))

            {

                strwhere.Append((string.IsNullOrEmpty(strwhere.ToString()) ? "where " : " and ") + @"infoname like '%'+@infoname+'%' ");

            }

            if (searchInfo.BeginDate != Convert.ToDateTime("0001/1/1") || searchInfo.EndDate != Convert.ToDateTime("0001/1/1"))

            {

                strwhere.Append((string.IsNullOrEmpty(strwhere.ToString()) ? "where " : " and ") + @"CreateDate between @startdate and @enddate ");

            }

            try

            {

                strSql.Append(@" SELECT count(1) as maxcount from Info " + strwhere.ToString() + "; ");

                strSql.Append(@" WITH Row AS

                                      (SELECT ROW_NUMBER() OVER(ORDER BY InfoId) AS rownumber, InfoId FROM Info (NOLOCK) "

                                            + strwhere.ToString() + ") ");

                strSql.Append("select InfoId,infoname,InfoContent,TypeId,PictureUrl,CreateId,CreateDate,ModifyDate,AttachMentUrl,IsTop from Info inner join Row on Info.InfoId=Row.InfoId WHERE rownumber BETWEEN @StartNum AND @StartNum + @pageSize - 1 ");

 

                SqlParameter[] parameters = {

                                               new SqlParameter("@infoname", SqlDbType.NVarChar,200),

                                               new SqlParameter("@startdate", SqlDbType.DateTime),

                    new SqlParameter("@enddate", SqlDbType.DateTime),

                    new SqlParameter("@StartNum",SqlDbType.Int),

                    new SqlParameter("@pageSize",SqlDbType.Int)};

                parameters[0].Value = searchInfo.Infoname;

                parameters[1].Value = searchInfo.BeginDate;

                parameters[2].Value = searchInfo.EndDate;

                parameters[3].Value = searchInfo.pageInfo.startNum;

                parameters[4].Value = searchInfo.pageInfo.pageSize;

 

                using (IDataReader dataReader = SqlHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameters))

                {

                    if (dataReader.Read())

                    {

 

                        PageInfo pageInfo=new PageInfo();

                        pageInfo.Max = dataReader.GetInt32(0);

                        if (dataReader.NextResult())

                        {

                            while (dataReader.Read())

                            {

                                Info info=new Info();

                                info = PopulateInfoEntityFromDr(dataReader);

                                infoList.infoList.Add(info);

                            }

                        }

                        infoList.infoResult.Code = 0;

                    }

                    else

                    {

                        infoList.infoResult.Code = 101;

                        infoList.infoResult.Message = "没?找¨°到Ì?相¨¤关?联¢a的Ì?信?息¡é!ê?";

                    }

                }

            }

            catch (Exception ex)

            {

                infoList.infoResult.Code = 100;

                infoList.infoResult.Message = ex.Message + ex.StackTrace + ex.InnerException;

              

            }

 

            return infoList;

        }

        public InfoList GetInfoById(int infoId)

        {

            InfoList infoList = new InfoList();

            StringBuilder strSql = new StringBuilder();

            try

            {

                strSql.Append("select InfoId,infoname,InfoContent,TypeId,PictureUrl,CreateId,CreateDate,ModifyDate,AttachMentUrl,IsTop from Info ");

                strSql.Append(" where InfoId=@InfoId ");

                SqlParameter[] parameters = {

                                               new SqlParameter("@InfoId", SqlDbType.Int,4)};

                parameters[0].Value = infoId;

                using (IDataReader dataReader = SqlHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameters))

                {

                    if (dataReader.Read())

                    {

                        Info info=new Info();

                        info = PopulateInfoEntityFromDr(dataReader);

                        infoList.infoList.Add(info);

                        infoList.infoResult.Code = 0;

                    }

                    else

                    {

                        infoList.infoResult.Code = 101;

                        infoList.infoResult.Message = "没?找¨°到Ì?相¨¤关?联¢a的Ì?信?息¡é!ê?";

                    }

                }

            }

            catch (Exception ex)

            {

                infoList.infoResult.Code = 100;

                infoList.infoResult.Message = ex.Message + ex.StackTrace + ex.InnerException;

 

            }

 

            return infoList;

 

        }

 

        /// <summary>

        /// 得Ì?到Ì?  info 数ºy据Y实º¦Ì体¬?

        /// </summary>

        /// <param name="dr">dr</param>

        /// <returns>info 数ºy据Y实º¦Ì体¬?</returns>

        public Info PopulateInfoEntityFromDr(IDataReader dr)

        {

            Info Obj = new Info();

 

            Obj.InfoId = ((dr["InfoId"]) == DBNull.Value) ? 0 : Convert.ToInt32(dr["InfoId"]);

            Obj.Infoname = dr["infoname"].ToString();

            Obj.InfoContent = dr["InfoContent"].ToString();

            Obj.TypeId = ((dr["TypeId"]) == DBNull.Value) ? 0 : Convert.ToInt32(dr["TypeId"]);

            Obj.PictureUrl = dr["PictureUrl"].ToString();

            Obj.CreateId = ((dr["CreateId"]) == DBNull.Value) ? 0 : Convert.ToInt32(dr["CreateId"]);

            Obj.CreateDate = ((dr["CreateDate"]) == DBNull.Value) ? Convert.ToDateTime(1900 - 1 - 1) : Convert.ToDateTime(dr["CreateDate"]);

            Obj.ModifyDate = ((dr["ModifyDate"]) == DBNull.Value) ? Convert.ToDateTime(1900 - 1 - 1) : Convert.ToDateTime(dr["ModifyDate"]);

            Obj.AttachMentUrl = dr["AttachMentUrl"].ToString();

            Obj.IsTop = ((dr["IsTop"]) == DBNull.Value) ? Convert.ToByte(0) : Convert.ToByte(dr["IsTop"]);

 

            return Obj;

        }

 

    }

}

 

 

   

你可能感兴趣的:(jquery,数据库,mvc,exception,delete,Parameters)