zoukankan      html  css  js  c++  java
  • 转:新闻发布系统

     2012年度课程设计---新闻发布系统(小结)

                                                                                                                                                                                                       -----Presented By muximuxi@Achilles
    Tips:因本课程设计大部分代码皆有本人短时间仓促码成,界面恶心,代码丑陋.唯一优点便是:        全部代码都已贴上,并且全部都已注释.另外与Asp.net教程结合恰当,通俗易懂,容易上手.大牛Please PASS.

    需求

    新闻发布系统需求III
    NewsPublish(简称NP)
    功能说明
    本项目用于对新闻发布进行管理。
    1、查看新闻
    所有新闻按时间按降序排列;
    用户登录后在自己主页可以查看自己当前所发布的所有新闻,在系统首页可以查看系统中所有的新闻;
    游客可以查看当前系统所发布的所有新闻。
    2、发布新闻
    用户登录后,通过填写表单,添加附件或者不添加附件,指定接收人进行新闻发布;
    接收人可以为联系人中的某几个人或所有人,其中所有人包括游客。表单见表一。

    表一:表单
    标题 xxxxxxx
    接收人 XXX
    发布人 XXX            添加附件 xxx
    正文 xxxxxxx

    简单需求分析

    简单分析:
    名词:新闻,用户,主页,表单,附件,接收人,联系人,游客.
    抽取名词建立实体类:新闻类(News),用户类(User),附件类(FileService),联系人类(Contact),添加了联系人的新闻类(NewsHaveSetContact)(为了不改变原来的代码,这个类建立应该是很丑陋恶心的,这应该用到设计模式的,这里主要为了展示三层架构就不从设计模式展开,-)_(-)
     
     

    底层之数据库

     
     

    项目文件夹部分截图

    三层架构代码

    Model层

    Model层之NewsModel

    [csharp] view plaincopyprint?
     
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5.   
    6. namespace NewsPublish.Model  
    7. {  
    8.     /// <summary>  
    9.     /// 封装News的字段和属性  
    10.     /// </summary>  
    11.     public class NewsModel  
    12.     {  
    13.  
    14.        
    15.         #region NewsModel构造函数  
    16.   
    17.         /// <summary>  
    18.         /// NewsModel构造函数  
    19.         /// </summary>  
    20.         /// <param name="title">新闻标题</param>  
    21.         /// <param name="date">新闻发布日期</param>  
    22.         /// <param name="content">新闻内容</param>  
    23.         /// <param name="newsID">新闻ID(key)</param>  
    24.         /// <param name="fileID">新闻所含的上传附件ID(or not)</param>  
    25.         public NewsModel(string title, DateTime date, string content, int newsID,int  fileID)  
    26.         {  
    27.             this._title = title;  
    28.   
    29.             this._date = date;  
    30.   
    31.             this._content = content;  
    32.   
    33.             this._newsID = newsID;  
    34.   
    35.             this._fileID = fileID;  
    36.   
    37.         }  
    38.         /// <summary>  
    39.         /// 其实建立这个构造函数是为了弥补这个缺陷的:没有绑定用户名;  
    40.         /// 不过话说是每一个model只是含有这个样例的  
    41.         /// </summary>  
    42.         public NewsModel()  
    43.         {  
    44.       
    45.         }  
    46.         #endregion  
    47.          
    48.  
    49.         #region 设置新闻类的属性  
    50.   
    51.         //News标题  
    52.         private string _title;  
    53.   
    54.         //News发布时间  
    55.         private DateTime _date;  
    56.   
    57.         //发布人  
    58.         private string _userName;  
    59.   
    60.         //News内容  
    61.         private string _content;  
    62.   
    63.         //与主键相对应的字段  
    64.         private int _newsID;  
    65.   
    66.         private int _fileID;  
    67.  
    68.         #endregion  
    69.  
    70.        
    71.         #region 设置成员属性访问器  
    72.   
    73.         public string Title  
    74.         {  
    75.             set  
    76.             {  
    77.                 _title = value;  
    78.             }  
    79.   
    80.             get  
    81.             {  
    82.                 return _title;  
    83.             }  
    84.         }  
    85.   
    86.         public DateTime Date  
    87.         {  
    88.             set  
    89.             {  
    90.                 _date = value;  
    91.             }  
    92.   
    93.             get  
    94.             {  
    95.                 return _date;  
    96.             }  
    97.         }  
    98.   
    99.         public string UserName  
    100.         {  
    101.             set  
    102.             {  
    103.                 _userName = value;  
    104.             }  
    105.   
    106.             get  
    107.             {  
    108.                 return _userName;  
    109.             }  
    110.         }  
    111.   
    112.         public string Content  
    113.         {  
    114.             set  
    115.             {  
    116.                 _content = value;  
    117.             }  
    118.   
    119.             get  
    120.             {  
    121.                 return _content;  
    122.             }  
    123.         }  
    124.   
    125.         public int NewsID  
    126.         {  
    127.             set  
    128.             {  
    129.                 _newsID = value;  
    130.             }  
    131.   
    132.             get  
    133.             {  
    134.                return  _newsID;  
    135.             }  
    136.         }  
    137.   
    138.         public int FileID  
    139.         {  
    140.             set  
    141.             {  
    142.                 _fileID = value;  
    143.             }  
    144.   
    145.             get  
    146.             {  
    147.                 return _fileID;  
    148.             }  
    149.         }  
    150.  
    151.         #endregion  
    152.     }  
    153. }  
     
     

    Model层之UserModel层

    [csharp] view plaincopyprint?
     
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5.   
    6. namespace NewsPublish.Model  
    7. {  
    8.     /// <summary>  
    9.     /// 封装User的字段和属性  
    10.     /// </summary>  
    11.     public class UserModel  
    12.     {  
    13.  
    14.         #region UserModel构造函数  
    15.         /// <summary>  
    16.         /// UserModel构造函数  
    17.         /// </summary>  
    18.         /// <param name="userID">用户ID</param>  
    19.         /// <param name="userName">用户名</param>  
    20.         /// <param name="password">用户密码</param>  
    21.         public UserModel(string userID, string userName, string password)  
    22.         {  
    23.             this._userID = userID;  
    24.             this._userName = userName;  
    25.             this._password = password;  
    26.         }  
    27.         public UserModel()  
    28.         {   
    29.           
    30.         }  
    31.         #endregion  
    32.  
    33.  
    34.         #region 定义用户类的属性(注意是private)  
    35.   
    36.         private string _userID;  
    37.         private string _userName;  
    38.         private string _password;  
    39.  
    40.         #endregion  
    41.  
    42.     
    43.         #region 设置成员属性访问器  
    44.   
    45.         public string UserID  
    46.         {  
    47.             set  
    48.             {  
    49.                 _userID = value;  
    50.             }  
    51.             get  
    52.             {  
    53.                 return _userID;  
    54.             }  
    55.         }  
    56.   
    57.         public string Password  
    58.         {  
    59.             set  
    60.             {  
    61.                 _password = value;  
    62.             }  
    63.             get  
    64.             {  
    65.                 return _password;  
    66.             }  
    67.         }  
    68.   
    69.         public string UserName  
    70.         {  
    71.             set  
    72.             {  
    73.                 _userName = value;  
    74.             }  
    75.             get  
    76.             {  
    77.                 return _userName;  
    78.             }  
    79.         }  
    80.         
    81.         #endregion  
    82.     }  
    83. }  


    Model层之FileServiceModel

    [csharp] view plaincopyprint?
     
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5.   
    6. namespace NewsPublish.Model  
    7. {  
    8.     public class FileServiceModel  
    9.     {  
    10.         #region NewsModel构造函数  
    11.   
    12.        /// <summary>  
    13.         /// NewsModel构造函数  
    14.        /// </summary>  
    15.        /// <param name="fileID">该新闻上传的附件的ID</param>  
    16.        /// <param name="fileTitle">附件标题</param>  
    17.        /// <param name="fileContent">附件内容</param>  
    18.        /// <param name="fileType">附件格式,这是和附件以二进制流的形式存在数据库的内容紧密相关的,不懂度娘去(附件上传数据库)</param>  
    19.         public FileServiceModel(int fileID,string fileTitle,byte[] fileContent ,string fileType )  
    20.         {  
    21.             this._fileID = fileID;  
    22.   
    23.             this._fileTitle = fileTitle;  
    24.   
    25.             this._fileContent = fileContent;  
    26.   
    27.             this._fileType = fileType;  
    28.   
    29.               
    30.         }  
    31.         public FileServiceModel()  
    32.         {   
    33.           
    34.         }  
    35.         #endregion  
    36.          
    37.  
    38.         #region 设置新闻类的属性  
    39.   
    40.         private int _fileID;  
    41.   
    42.         private string _fileTitle;  
    43.   
    44.         
    45.         private byte [] _fileContent;  
    46.   
    47.   
    48.         private string _fileType;  
    49.  
    50.         #endregion  
    51.  
    52.        
    53.         #region 设置成员属性访问器  
    54.   
    55.         public int FileID  
    56.         {  
    57.             set  
    58.             {  
    59.                 _fileID = value;  
    60.             }  
    61.   
    62.             get  
    63.             {  
    64.                 return _fileID;  
    65.             }  
    66.         }  
    67.   
    68.         public string FileTitle  
    69.         {  
    70.             set  
    71.             {  
    72.                 _fileTitle = value;  
    73.             }  
    74.   
    75.             get  
    76.             {  
    77.                 return _fileTitle;  
    78.             }  
    79.         }  
    80.   
    81.         public byte[] FileContent  
    82.         {  
    83.             set  
    84.             {  
    85.                 _fileContent = value;  
    86.             }  
    87.   
    88.             get  
    89.             {  
    90.                 return _fileContent;  
    91.             }  
    92.         }  
    93.   
    94.         public string FileType  
    95.         {  
    96.             set  
    97.             {  
    98.                 _fileType = value;  
    99.             }  
    100.   
    101.             get  
    102.             {  
    103.                 return _fileType;  
    104.             }  
    105.         }  
    106.  
    107.       
    108.  
    109.         #endregion  
    110.     }  
    111. }  


    Model层之ContactModel

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5.   
    6. namespace NewsPublish.Model  
    7. {  
    8.     public class ContactModel  
    9.     {  
    10.           
    11.        #region ContactModel构造函数  
    12.   
    13.        /// <summary>  
    14.        /// ContactModel的构造函数  
    15.        /// </summary>  
    16.        /// <param name="contactName">联系人</param>  
    17.        /// <param name="userName">用户</param>  
    18.        
    19.        public ContactModel(string contactName,string userName)  
    20.        {  
    21.            this._contactName = contactName;  
    22.   
    23.            this._userName = userName;  
    24.   
    25.      
    26.        }  
    27.        public ContactModel()  
    28.        {   
    29.          
    30.        }  
    31.        #endregion  
    32.          
    33.         #region 设置新闻类的属性  
    34.         //用户的联系人  
    35.         private string _contactName = null;  
    36.         //用户名  
    37.         private string _userName = null;  
    38.         
    39.   
    40.         #endregion  
    41.   
    42.         #region 设置成员属性访问器  
    43.   
    44.        public string ContactName  
    45.         {  
    46.             set  
    47.             {  
    48.                 _contactName = value;  
    49.             }  
    50.   
    51.             get  
    52.             {  
    53.                 return _contactName;  
    54.             }  
    55.         }  
    56.   
    57.         public string UserName  
    58.         {  
    59.             set  
    60.             {  
    61.                 _userName = value;  
    62.             }  
    63.   
    64.             get  
    65.             {  
    66.                 return _userName;  
    67.             }  
    68.         }  
    69.          
    70.   
    71.         #endregion  
    72.   
    73.   
    74.   
    75.     }  
    76. }  


    Model层之NewsHaveSetContactModel

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5.   
    6.   
    7. namespace NewsPublish.Model  
    8. {  
    9.     public class NewsHaveSetContactModel  
    10.     {  
    11.   
    12.   
    13.           
    14.        #region NewsHaveSetContactModel构造函数  
    15.   
    16.   
    17.          
    18.         public NewsHaveSetContactModel(string contactName, int newsID)  
    19.        {  
    20.            this._contactName = contactName;  
    21.   
    22.   
    23.            this._newsID = newsID;  
    24.   
    25.   
    26.        }  
    27.         public NewsHaveSetContactModel()  
    28.         {   
    29.           
    30.         }  
    31.        #endregion  
    32.          
    33.         #region 设置已添加联系人的新闻类成员变量  
    34.           
    35.         private string _contactName = "VISITOR";//!!!!  
    36.       
    37.         private int _newsID ;  
    38.   
    39.   
    40.         #endregion  
    41.         #region 设置成员变量访问器  
    42.     
    43.         public string  ContactName  
    44.         {  
    45.             set  
    46.             {  
    47.                 _contactName = value;  
    48.             }  
    49.   
    50.   
    51.             get  
    52.             {  
    53.                 return _contactName;  
    54.             }  
    55.         }  
    56.   
    57.   
    58.         public int NewsID  
    59.         {  
    60.             set  
    61.             {  
    62.                 _newsID = value;  
    63.             }  
    64.   
    65.   
    66.             get  
    67.             {  
    68.                 return _newsID;  
    69.             }  
    70.         }  
    71.         #endregion  
    72.     }  
    73. }  
     

    IDAL层

    IDAL层之INewsDAL

     
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using NewsPublish.Model;  
    6.   
    7. namespace NewsPublish.IDAL  
    8. {  
    9.     public interface INewsDAL  
    10.     {  
    11.   
    12.         //建立接口,是为了上一层依赖于这个一层的抽象而不是实现(也就是不必知道具体的方法怎么执行)  
    13.         //只需要知道传什么参数返回什么东西即可..接口无限强大,什么函数都调用(C#语法书没看,摸黑过来,百度后推测应该是这样)  
    14.   
    15.   
    16.         /// <summary>  
    17.         /// 得到所有新闻列表,主要用于主页显示  
    18.         /// </summary>  
    19.         /// <returns>所有新闻列表</returns>  
    20.         List<NewsModel> GetAllNewsList();  
    21.   
    22.   
    23.         
    24.         /// <summary>  
    25.         /// 根据用户名,得到用户已发布的新闻列表  
    26.         /// </summary>  
    27.         /// <param name="userName">用户名</param>  
    28.         /// <returns>用户已发布的新闻列表</returns>  
    29.         List<NewsModel> GetUserNewsListByUserName(string userName);  
    30.           
    31.   
    32.           
    33.        /// <summary>  
    34.        /// 发布新闻  
    35.        /// </summary>  
    36.        /// <param name="title">新闻标题</param>  
    37.        /// <param name="date">发布新闻系统时间</param>  
    38.        /// <param name="userName">用户名(新闻,和用户名都有这么一个属性)</param>  
    39.        /// <param name="content">新闻内容</param>  
    40.        /// <param name="fileID">新闻附件ID</param>  
    41.        /// <returns>返回插入的新闻ID,这对后续处理很有帮助,good</returns>  
    42.         int PublishNews(string title, DateTime date, string userName, string content,int fileID);  
    43.           
    44.   
    45.           
    46.         /// <summary>  
    47.         ///  由新闻Id得到具体新闻内容  
    48.         /// </summary>  
    49.         /// <param name="newsID">新闻ID</param>  
    50.         /// <returns>详细新闻</returns>  
    51.         NewsModel GetDetailNewsByNewsID(int newsID);  
    52.     
    53.           
    54.     }  
    55. }  


    IDAL层之IUserDAL

     
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using NewsPublish.Model;  
    6.   
    7. namespace NewsPublish.IDAL  
    8. {  
    9.     public interface IUserDAL  
    10.     {  
    11.         /// <summary>  
    12.         /// 登录  
    13.         /// </summary>  
    14.         /// <param name="userName">用户名</param>  
    15.         /// <param name="password">密码</param>  
    16.         /// <returns>登录是否成功</returns>  
    17.       bool Login(string userName, string password);  
    18.   
    19.         /// <summary>  
    20.         /// 注册  
    21.         /// </summary>  
    22.         /// <param name="userName">用户名</param>  
    23.         /// <param name="password">密码</param>  
    24.       /// <returns>注册是否成功,只不过用int类型更好的变化://flag=1,2,3  分别为:用户名已存在/注册成功/注册失败</returns>  
    25.       int Register(string userName, string password);  
    26.     }  
    27. }  


    IDAL层之IFileServiceDAL

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Data;  
    4. using System.Linq;  
    5. using System.Web;  
    6.   
    7. namespace NewsPublish.IDAL  
    8. {  
    9.     public interface IFileServiceDAL  
    10.     {  
    11.   
    12.   
    13.         /// <summary>  
    14.         /// 上传附件  
    15.         /// </summary>  
    16.         /// <param name="fileTitle">附件标题</param>  
    17.         /// <param name="fileContent">附件内容</param>  
    18.         /// <param name="fileType">附件格式</param>  
    19.         /// <returns>返回附件插入数据库的即时ID</returns>  
    20.         int UpLoadFile(string fileTitle, byte[] fileContent, string fileType);  
    21.          
    22.         
    23.          
    24.   
    25.            
    26.         /// <summary>  
    27.         /// 通过fileID下载新闻附件  
    28.         /// </summary>  
    29.         /// <param name="fileID">附件ID</param>  
    30.         /// <returns>通过DataTable类型返回文件</returns>  
    31.         DataTable GetHadUpLoadFileByFileID(int fileID);  
    32.   
    33.   
    34.   
    35.         /// <summary>  
    36.         /// 通过newsID获取fileID;  
    37.         /// </summary>  
    38.         /// <param name="newsID">新闻ID</param>  
    39.         /// <returns>附件ID</returns>  
    40.         int GetFileIDByNewsID(int newsID);  
    41.   
    42.   
    43.     }  
    44. }  


    IDAL层之IContactDAL

     
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.Threading.Tasks;  
    6. using NewsPublish.Model;  
    7.   
    8. namespace NewsPublish.IDAL  
    9. {  
    10.     public  interface IContactDAL  
    11.     {  
    12.   
    13.   
    14.   
    15.         /// <summary>  
    16.         /// 通过用户名获取联系人  
    17.         /// </summary>  
    18.         /// <param name="userName">用户名</param>  
    19.         /// <returns>联系人列表</returns>  
    20.         List<ContactModel> GetMyContactByUserName(string userName);  
    21.   
    22.   
    23.         /// <summary>  
    24.         /// 根据用户名和联系人添加联系人  
    25.         /// </summary>  
    26.         /// <param name="newContact">用户名</param>  
    27.         /// <returns>添加是否成功</returns>  
    28.         bool AddContact(string userName, string newContact);  
    29.     }  
    30. }  


    IDAL层之INewsHaveSetContactDAL

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.Threading.Tasks;  
    6. using NewsPublish.Model;  
    7.   
    8. namespace NewsPublish.IDAL  
    9. {  
    10.     public interface INewsHaveSetContactDAL  
    11.     {  
    12.   
    13.         /// <summary>  
    14.         /// 得到将我加入联系人的新闻的新闻ID列表--------未登录主页+个人中心  
    15.         /// </summary>  
    16.         /// <returns>将我加入联系人的新闻的新闻ID列表</returns>  
    17.         List<NewsHaveSetContactModel> GetNewsIDOfNewsHaveAddMeIntoContact(string contactName);  
    18.   
    19.   
    20.         /// <summary>  
    21.         /// 根据相应逻辑获取到的新闻ID列表(根据联系人来分)   然后得到相应逻辑的新闻列表  
    22.         /// </summary>  
    23.         /// <returns>将我加入联系人的新闻列表</returns>  
    24.         List<NewsModel> GetNewsListOfNewsHaveAddMeIntoContact(List<NewsHaveSetContactModel> newsIDList);  
    25.   
    26.   
    27.   
    28.         /// <summary>  
    29.         ///  得到将游客和我作为接收人的新闻的新闻ID列表---------登录主页  
    30.         /// 其实一开始我还忘了我本人发布的新闻也放在登录主页呢..囧..  
    31.         /// </summary>  
    32.         /// <param name="contactName">作为联系人的名字---"我"</param>  
    33.         /// <returns>得到将游客,我作为接收人,以及我发布的新闻的ID列表</returns>  
    34.         List<NewsHaveSetContactModel> GetNewsIDOfVisitorAndMe(string contactName);  
    35.   
    36.   
    37.   
    38.        /// <summary>  
    39.         /// 从联系人添加新闻接收人到具体新闻  
    40.        /// </summary>  
    41.        /// <param name="newsID">新闻ID</param>  
    42.        /// <param name="contactName">联系人名字</param>  
    43.        /// <returns>添加是否成功</returns>  
    44.         bool AddNewsReceiver(int newsID, string contactName);  
    45.     }  
    46. }  


    DAL层

    DAL层之SQLHelper(这个是作为数据库操作写的辅助类,对减少冗余代码起了不少作用,不过是从老师课题笔记那摘过来的,网上也有一大堆下载)

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Data;  
    6. using System.Data.SqlClient;  
    7. using System.Configuration;  
    8.   
    9. namespace NewsPublish.DAL  
    10. {  
    11.     public class SQLHelper  
    12.     {  
    13.           
    14.         string connectionString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;  
    15.   
    16.         /// <summary>  
    17.         /// 构造函数  
    18.         /// </summary>  
    19.         public SQLHelper()  
    20.         {  
    21.         }  
    22.   
    23.         /// <summary>  
    24.         /// ExecuteNonQuery操作,对数据库进行 增、删、改 操作((1)  
    25.         /// </summary>  
    26.         /// <param name="sql">要执行的SQL语句 </param>  
    27.         /// <returns> </returns>  
    28.         public int ExecuteNonQuery(string sql)  
    29.         {  
    30.             return ExecuteNonQuery(sql, CommandType.Text, null);  
    31.         }  
    32.   
    33.         /// <summary>  
    34.         /// ExecuteNonQuery操作,对数据库进行 增、删、改 操作(2)  
    35.         /// </summary>  
    36.         /// <param name="sql">要执行的SQL语句 </param>  
    37.         /// <param name="commandType">要执行的查询类型(存储过程、SQL文本) </param>  
    38.         /// <returns> </returns>  
    39.         public int ExecuteNonQuery(string sql, CommandType commandType)  
    40.         {  
    41.             return ExecuteNonQuery(sql, commandType, null);  
    42.         }  
    43.   
    44.         /// <summary>  
    45.         /// ExecuteNonQuery操作,对数据库进行 增、删、改 操作(3)  
    46.         /// </summary>  
    47.         /// <param name="sql">要执行的SQL语句 </param>  
    48.         /// <param name="commandType">要执行的查询类型(存储过程、SQL文本) </param>  
    49.         /// <param name="parameters">参数数组 </param>  
    50.         /// <returns> </returns>  
    51.         public int ExecuteNonQuery(string sql, CommandType commandType, SqlParameter[] parameters)  
    52.         {  
    53.             //影响的记录数量  
    54.             int count = 0;  
    55.             //创建连接  
    56.             using (SqlConnection connection = new SqlConnection(connectionString))  
    57.             {  
    58.                 //创建命令对象  
    59.                 using (SqlCommand command = new SqlCommand(sql, connection))  
    60.                 {  
    61.                     //设置命令类型  
    62.                     command.CommandType = commandType;  
    63.                       
    64.                     //是否有参数  
    65.                     if (parameters != null)  
    66.                     {  
    67.                         //向命令对象添加参数  
    68.                         foreach (SqlParameter parameter in parameters)  
    69.                         {  
    70.                             command.Parameters.Add(parameter);  
    71.                         }  
    72.                     }  
    73.   
    74.                     //打开连接  
    75.                     connection.Open();  
    76.   
    77.                     //返回操作影响的记录数量  
    78.                     count = command.ExecuteNonQuery();  
    79.                 }  
    80.             }  
    81.             return count;  
    82.         }  
    83.   
    84.         /// <summary>  
    85.         /// SqlDataAdapter的Fill方法执行一个查询,并返回一个DataSet类型结果(1)  
    86.         /// </summary>  
    87.         /// <param name="sql">要执行的SQL语句 </param>  
    88.         /// <returns> </returns>  
    89.         public DataSet ExecuteDataSet(string sql)  
    90.         {  
    91.             return ExecuteDataSet(sql, CommandType.Text, null);  
    92.         }  
    93.   
    94.         /// <summary>  
    95.         /// SqlDataAdapter的Fill方法执行一个查询,并返回一个DataSet类型结果(2)  
    96.         /// </summary>  
    97.         /// <param name="sql">要执行的SQL语句 </param>  
    98.         /// <param name="commandType">要执行的查询类型(存储过程、SQL文本) </param>  
    99.         /// <returns> </returns>  
    100.         public DataSet ExecuteDataSet(string sql, CommandType commandType)  
    101.         {  
    102.             return ExecuteDataSet(sql, commandType, null);  
    103.         }  
    104.   
    105.         /// <summary>  
    106.         /// SqlDataAdapter的Fill方法执行一个查询,并返回一个DataSet类型结果(3)  
    107.         /// </summary>  
    108.         /// <param name="sql">要执行的SQL语句 </param>  
    109.         /// <param name="commandType">要执行的查询类型(存储过程、SQL文本) </param>  
    110.         /// <param name="parameters">参数数组 </param>  
    111.         /// <returns> </returns>  
    112.         public DataSet ExecuteDataSet(string sql, CommandType commandType, SqlParameter[] parameters)  
    113.         {  
    114.             DataSet ds = new DataSet();  
    115.   
    116.             using (SqlConnection connection = new SqlConnection(connectionString))  
    117.             {  
    118.                 using (SqlCommand command = new SqlCommand(sql, connection))  
    119.                 {  
    120.                     command.CommandType = commandType;  
    121.   
    122.                     if (parameters != null)  
    123.                     {  
    124.                         foreach (SqlParameter parameter in parameters)  
    125.                         {  
    126.                             command.Parameters.Add(parameter);  
    127.                         }  
    128.                     }  
    129.   
    130.                     SqlDataAdapter adapter = new SqlDataAdapter(command);  
    131.   
    132.                     adapter.Fill(ds);  
    133.                 }  
    134.             }  
    135.             return ds;  
    136.         }  
    137.   
    138.         /// <summary>  
    139.         /// SqlDataAdapter的Fill方法执行一个查询,并返回一个DataTable类型结果(1)  
    140.         /// </summary>  
    141.         /// <param name="sql">要执行的SQL语句 </param>  
    142.         /// <returns> </returns>  
    143.         public DataTable ExecuteDataTable(string sql)  
    144.         {  
    145.             return ExecuteDataTable(sql, CommandType.Text, null);  
    146.         }  
    147.   
    148.         /// <summary>  
    149.         /// SqlDataAdapter的Fill方法执行一个查询,并返回一个DataTable类型结果(2)  
    150.         /// </summary>  
    151.         /// <param name="sql">要执行的SQL语句 </param>  
    152.         /// <param name="commandType">要执行的查询类型(存储过程、SQL文本) </param>  
    153.         /// <returns> </returns>  
    154.         public DataTable ExecuteDataTable(string sql, CommandType commandType)  
    155.         {  
    156.             return ExecuteDataTable(sql, commandType, null);  
    157.         }  
    158.   
    159.         /// <summary>  
    160.         /// SqlDataAdapter的Fill方法执行一个查询,并返回一个DataTable类型结果(3)  
    161.         /// </summary>  
    162.         /// <param name="sql">要执行的SQL语句 </param>  
    163.         /// <param name="commandType">要执行的查询类型(存储过程、SQL文本) </param>  
    164.         /// <param name="parameters">参数数组 </param>  
    165.         /// <returns> </returns>  
    166.         public DataTable ExecuteDataTable(string sql, CommandType commandType, SqlParameter[] parameters)  
    167.         {  
    168.             DataTable data = new DataTable();  
    169.             using (SqlConnection connection = new SqlConnection(connectionString))  
    170.             {  
    171.                 using (SqlCommand command = new SqlCommand(sql, connection))  
    172.                 {  
    173.                     command.CommandType = commandType;  
    174.                     if (parameters != null)  
    175.                     {  
    176.                         foreach (SqlParameter parameter in parameters)  
    177.                         {  
    178.                             command.Parameters.Add(parameter);  
    179.                         }  
    180.                     }  
    181.                     SqlDataAdapter adapter = new SqlDataAdapter(command);  
    182.                     adapter.Fill(data);  
    183.                 }  
    184.             }  
    185.             return data;  
    186.         }  
    187.   
    188.         /// <summary>  
    189.         /// ExecuteReader执行一查询,返回一SqlDataReader对象实例(1)  
    190.         /// </summary>  
    191.         /// <param name="sql">要执行的SQL语句 </param>  
    192.         /// <returns> </returns>  
    193.         public SqlDataReader ExecuteReader(string sql)  
    194.         {  
    195.             return ExecuteReader(sql, CommandType.Text, null);  
    196.         }  
    197.   
    198.         /// <summary>  
    199.         /// ExecuteReader执行一查询,返回一SqlDataReader对象实例(2)  
    200.         /// </summary>  
    201.         /// <param name="sql">要执行的SQL语句 </param>  
    202.         /// <param name="commandType">要执行的查询类型(存储过程、SQL文本) </param>  
    203.         /// <returns> </returns>  
    204.         public SqlDataReader ExecuteReader(string sql, CommandType commandType)  
    205.         {  
    206.             return ExecuteReader(sql, commandType, null);  
    207.         }  
    208.   
    209.         /// <summary>  
    210.         /// ExecuteReader执行一查询,返回一SqlDataReader对象实例(3)  
    211.         /// </summary>  
    212.         /// <param name="sql">要执行的SQL语句 </param>  
    213.         /// <param name="commandType">要执行的查询类型(存储过程、SQL文本) </param>  
    214.         /// <param name="parameters">参数数组 </param>  
    215.         /// <returns> </returns>  
    216.         public SqlDataReader ExecuteReader(string sql, CommandType commandType, SqlParameter[] parameters)  
    217.         {  
    218.             SqlConnection connection = new SqlConnection(connectionString);  
    219.             SqlCommand command = new SqlCommand(sql, connection);  
    220.             command.CommandType = commandType;  
    221.             if (parameters != null)  
    222.             {  
    223.                 foreach (SqlParameter parameter in parameters)  
    224.                 {  
    225.                     command.Parameters.Add(parameter);  
    226.                 }  
    227.             }  
    228.             connection.Open();  
    229.             return command.ExecuteReader(CommandBehavior.CloseConnection);  
    230.         }  
    231.   
    232.         /// <summary>  
    233.         /// ExecuteScalar执行一查询,返回查询结果的第一行第一列(1)  
    234.         /// </summary>  
    235.         /// <param name="sql">要执行的SQL语句 </param>  
    236.         /// <returns> </returns>  
    237.         public Object ExecuteScalar(string sql)  
    238.         {  
    239.             return ExecuteScalar(sql, CommandType.Text, null);  
    240.         }  
    241.   
    242.         /// <summary>  
    243.         /// ExecuteScalar执行一查询,返回查询结果的第一行第一列(2)  
    244.         /// </summary>  
    245.         /// <param name="sql">要执行的SQL语句 </param>  
    246.         /// <param name="commandType">要执行的查询类型(存储过程、SQL文本) </param>  
    247.         /// <returns> </returns>  
    248.         public Object ExecuteScalar(string sql, CommandType commandType)  
    249.         {  
    250.             return ExecuteScalar(sql, commandType, null);  
    251.         }  
    252.   
    253.         /// <summary>  
    254.         /// ExecuteScalar执行一查询,返回查询结果的第一行第一列(3)  
    255.         /// </summary>  
    256.         /// <param name="sql">要执行的SQL语句 </param>  
    257.         /// <param name="commandType">要执行的查询类型(存储过程、SQL文本) </param>  
    258.         /// <returns> </returns>  
    259.         public Object ExecuteScalar(string sql, CommandType commandType, SqlParameter[] parameters)  
    260.         {  
    261.             object result = null;  
    262.             using (SqlConnection connection = new SqlConnection(connectionString))  
    263.             {  
    264.                 using (SqlCommand command = new SqlCommand(sql, connection))  
    265.                 {  
    266.                     command.CommandType = commandType;  
    267.                     if (parameters != null)  
    268.                     {  
    269.                         foreach (SqlParameter parameter in parameters)  
    270.                         {  
    271.                             command.Parameters.Add(parameter);  
    272.                         }  
    273.                     }  
    274.                     connection.Open();  
    275.                     result = command.ExecuteScalar();  
    276.                 }  
    277.             }  
    278.             return result;  
    279.         }  
    280.   
    281.         /// <summary>  
    282.         /// 返回当前连接的数据库中所有由用户创建的数据库  
    283.         /// </summary>  
    284.         /// <returns> </returns>  
    285.         public DataTable GetTables()  
    286.         {  
    287.             DataTable data = null;  
    288.             using (SqlConnection connection = new SqlConnection(connectionString))  
    289.             {  
    290.                 connection.Open();  
    291.                 data = connection.GetSchema("Tables");  
    292.             }  
    293.             return data;  
    294.         }   
    295.     }  
    296.       
    297.     }  


    DAL层之NewsDAL

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using NewsPublish.IDAL;  
    6. using NewsPublish.Model;  
    7. using System.Data.SqlClient;  
    8. using NewsPublish.DAL;  
    9. using System.Data;  
    10.   
    11. namespace NewsPublish.DAL  
    12. {  
    13.     public class NewsDAL : INewsDAL  
    14.     {  
    15.         //这个原本想是利用微软的sqlhelper类来写的网上大量有得下载,不过既然老师的课堂代码刚好有就直接搞过来了  
    16.         private SQLHelper sqlhelper = null;  
    17.   
    18.         //构造函数  
    19.         public NewsDAL()  
    20.         {  
    21.             sqlhelper = new SQLHelper();  
    22.         }  
    23.   
    24.   
    25.         /// <summary>  
    26.         /// 得到所有新闻列表,,主要用于主页显示(不过因为第二阶段需求之后,此函数已无用)  
    27.         /// </summary>  
    28.         /// <returns>所有新闻列表</returns>  
    29.         public List<NewsModel> GetAllNewsList()  
    30.         {  
    31.             //创建新闻对象列表  
    32.             List<NewsModel> newsList = new List<NewsModel>();  
    33.   
    34.             //选择dbo.Table_News中的新闻标题、作者、发布日期列  
    35.             string sql = "select Title,UserName,Date,NewsID from dbo.Table_News order by Date desc";  
    36.   
    37.             //这个SQLHelper类中的ExecuteReader方法中会建立commond对象,返回的是DataReader对象  
    38.             SqlDataReader res = sqlhelper.ExecuteReader(sql);  
    39.   
    40.             //通过reader方法自增获取每一行的相应所要的列内容,然后添加到list中建立新闻对象.类似foreach  
    41.             while (res.Read())  
    42.             {  
    43.                 //创建新闻对象,无参数的构造函数的好处体现在这  
    44.                 NewsModel news = new NewsModel();  
    45.   
    46.                 //给新闻对象属性赋值  
    47.                 //使用SqlDataReader的GetOrdinal()方法(这个方法我不求甚解,自己百度吧),获得列的序号,输入参数为列名  
    48.                 news.Title = res.GetString(res.GetOrdinal("Title"));  
    49.                 news.UserName = res.GetString(res.GetOrdinal("UserName"));  
    50.                 news.NewsID = res.GetInt32(res.GetOrdinal("NewsID"));  
    51.                 news.Date = res.GetDateTime((res.GetOrdinal("Date")));  
    52.                 //将新闻添加到列表中,easy吧..-_-  
    53.                 newsList.Add(news);  
    54.             }  
    55.             return newsList;  
    56.         }  
    57.   
    58.   
    59.         /// <summary>  
    60.         /// 根据用户名,得到用户已发布的新闻列表  
    61.         /// </summary>  
    62.         /// <param name="userName">用户名</param>  
    63.         /// <returns>用户已发布的新闻列表</returns>  
    64.         public List<NewsModel> GetUserNewsListByUserName(string userName)  
    65.         {  
    66.   
    67.             //创建新闻对象列表  
    68.             List<NewsModel> userNewsList = new List<NewsModel>();  
    69.   
    70.             //选择dbo.Table_News中该用户已发布新闻的标题、作者、发布日期  
    71.             string sql = "select NewsID, Title,UserName,Date from dbo.Table_News where UserName=@UserName  order by Date desc";  
    72.   
    73.             SqlParameter[] param = new SqlParameter[1]{  
    74.                  
    75.                 new SqlParameter("@UserName", userName)  
    76.               
    77.             };  
    78.             //含有参数的的传参方法,迷惑的话可以看一下Sqlhelper类(在DAL层中)  
    79.             SqlDataReader res = sqlhelper.ExecuteReader(sql, CommandType.Text, param);  
    80.   
    81.             //获取新闻对象  
    82.             while (res.Read())  
    83.             {  
    84.                 //创建新闻对象  
    85.                 NewsModel news = new NewsModel();  
    86.   
    87.                 //给新闻对象属性赋值  
    88.                 //使用SqlDataReader的GetOrdinal()方法,获得列的序号,输入参数为列名  
    89.                 news.NewsID = res.GetInt32(res.GetOrdinal("NewsID"));  
    90.                 news.Title = res.GetString(res.GetOrdinal("Title"));  
    91.                 news.UserName = res.GetString(res.GetOrdinal("UserName"));  
    92.                 news.Date = res.GetDateTime(res.GetOrdinal("Date"));  
    93.                 //将新闻添加到列表中  
    94.                 userNewsList.Add(news);  
    95.             }  
    96.             return userNewsList;  
    97.         }  
    98.   
    99.   
    100.         /// <summary>  
    101.         /// 发布新闻  
    102.         /// </summary>  
    103.         /// <param name="title">新闻标题</param>  
    104.         /// <param name="date">发布新闻系统时间</param>  
    105.         /// <param name="userName">用户名(新闻,和用户名都有这么一个属性)</param>  
    106.         /// <param name="content">新闻内容</param>  
    107.         /// <param name="fileID">新闻附件ID</param>  
    108.         /// <returns>返回插入的新闻ID,这对后续处理很有帮助,good</returns>  
    109.         public int PublishNews(string title, DateTime Date, string userName, string content, int fileID)  
    110.         {  
    111.              
    112.             //观察这条sql语句的结尾处是返回插入的即时ID的关键,不懂的话度娘吧.-_-  
    113.             string sql = "insert  into dbo.Table_News(Title,UserName,Date,Content,FileID )values (@Title,@UserName,@Date,@Content,@FileID);Select @@IDENTITY";  
    114.             //话说这样的传参方式我真是不习惯.不过好在貌似很实用.@Parameter充当的是中介的变量  
    115.             SqlParameter[] param = new SqlParameter[5]  
    116.             {  
    117.                   new SqlParameter("@Title", title),  
    118.                   new SqlParameter("@UserName", userName),  
    119.                   new SqlParameter("@Date",Date),  
    120.                   new SqlParameter("@Content", content),  
    121.                   new SqlParameter("@FileID",fileID)  
    122.               
    123.             };  
    124.             //调用数据库操作  
    125.             int res = Convert.ToInt32(sqlhelper.ExecuteScalar(sql, CommandType.Text, param).ToString());  
    126.             if (res != 0)  
    127.             {  
    128.                 return res;//直接返回就是操作后的FileID(注意这样的用法仅限于key值,因为ExecuteScalar只能获取第一行第一列值)  
    129.             }  
    130.             else  
    131.             {  
    132.                 return 0;  
    133.             }  
    134.   
    135.         }  
    136.   
    137.         /// <summary>  
    138.         /// 根据新闻ID,得到详细新闻  
    139.         /// </summary>  
    140.         /// <param name="newsID">新闻ID</param>  
    141.         /// <returns>详细新闻</returns>  
    142.         public NewsModel GetDetailNewsByNewsID(int newsID)  
    143.         {  
    144.   
    145.             NewsModel news = new NewsModel();  
    146.             string sql = "Select Title ,Content,Date,UserName,NewsID from dbo.Table_News where NewsID=@NewsID";  
    147.   
    148.             SqlParameter[] param = new SqlParameter[1]  
    149.             {  
    150.                
    151.                 new SqlParameter("@NewsID", newsID)  
    152.               
    153.             };  
    154.             SqlDataReader res = sqlhelper.ExecuteReader(sql, CommandType.Text, param);  
    155.   
    156.             while (res.Read())  
    157.             {  
    158.                 //给新闻对象属性赋值  
    159.                 //使用SqlDataReader的GetOrdinal()方法,获得列的序号,输入参数为列名  
    160.                 news.NewsID = res.GetInt32(res.GetOrdinal("NewsID"));  
    161.                 news.Title = res.GetString(res.GetOrdinal("Title"));  
    162.                 news.UserName = res.GetString(res.GetOrdinal("UserName"));  
    163.                 news.Date = res.GetDateTime(res.GetOrdinal("Date"));  
    164.                 news.Content = res.GetString(res.GetOrdinal("Content"));  
    165.             }  
    166.                 return news;  
    167.               
    168.   
    169.         }  
    170.   
    171.   
    172.   
    173.     }  
    174. }  


    DAL层之UserDAL

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using NewsPublish.IDAL;  
    6. using System.Data.SqlClient;  
    7. using NewsPublish.Model;  
    8. using System.Data;  
    9.   
    10. namespace NewsPublish.DAL  
    11. {  
    12.     public class UserDAL : IUserDAL  
    13.     {  
    14.         private SQLHelper sqlhelper = null;  
    15.   
    16.         //构造函数  
    17.         public UserDAL()  
    18.         {  
    19.             sqlhelper = new SQLHelper();  
    20.         }  
    21.   
    22.   
    23.         /// <summary>  
    24.         /// 登录  
    25.         /// </summary>  
    26.         /// <param name="userName">用户名</param>  
    27.         /// <param name="password">密码</param>  
    28.         /// <returns>登录是否成功</returns>  
    29.         public bool Login(string userName, string password)  
    30.         {  
    31.   
    32.             string sql = "Select UserName,Password from dbo.Table_User where UserName=@UserName and Password=@Password";  
    33.             //创建参数数组  
    34.             SqlParameter[] param = new SqlParameter[2]  
    35.             {  
    36.                  
    37.                  new SqlParameter("@UserName", userName),  
    38.                  new SqlParameter("@Password", password)  
    39.             };  
    40.   
    41.               
    42.             string i = Convert.ToString(sqlhelper.ExecuteScalar(sql, CommandType.Text, param));  
    43.   
    44.   
    45.             if (string.IsNullOrEmpty(i))  
    46.             {  
    47.                 return false;  
    48.             }  
    49.             else  
    50.             {  
    51.                 return true;  
    52.             }  
    53.   
    54.         }  
    55.   
    56.         /// <summary>  
    57.         /// 注册  
    58.         /// </summary>  
    59.         /// <param name="userName">用户名</param>  
    60.         /// <param name="password">密码</param>  
    61.         /// <returns>注册是否成功,只不过用int类型更好的变化</returns>  
    62.         public int Register(string userName, string password)  
    63.         {  
    64.             int flag;  //flag=1,2,3  分别为:用户名已存在/注册成功/注册失败  
    65.             string sql = "select COUNT(*)from dbo.Table_User where UserName=@UserName";  
    66.             //创建参数数组  
    67.             SqlParameter[] param = new SqlParameter[1]  
    68.             {  
    69.             new SqlParameter("@UserName", userName)  
    70.             };  
    71.   
    72.             int i = Convert.ToInt32(sqlhelper.ExecuteScalar(sql, CommandType.Text, param));  
    73.   
    74.             if (i == 0)  
    75.             {  
    76.                 string sql1 = "insert into dbo.Table_User(UserName,Password) values (@UserName,@Password)";  
    77.   
    78.                 //创建参数数组  
    79.                 SqlParameter[] param1 = new SqlParameter[2]  
    80.                 {  
    81.                 new SqlParameter("@UserName", userName),  
    82.                 new SqlParameter("@Password", password)  
    83.                 };  
    84.                 int res = sqlhelper.ExecuteNonQuery(sql1, CommandType.Text, param1);  
    85.                 if (res != 0)  
    86.                  {  
    87.                      flag2;  
    88.                  }  
    89.                 else  
    90.                  {  
    91.                      flag3;  
    92.                  }  
    93.             }  
    94.             else  
    95.             {   
    96.                 flag=1;  
    97.                   
    98.             }  
    99.             return flag;  
    100.   
    101.         }  
    102.     }  
    103. }  


    DAL层之FileServiceDAL

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Data;  
    4. using System.Data.SqlClient;  
    5. using System.Linq;  
    6. using System.Web;  
    7. using NewsPublish.IDAL;  
    8. using NewsPublish.Model;  
    9.   
    10. namespace NewsPublish.DAL  
    11. {  
    12.     public class FileServiceDAL : IFileServiceDAL  
    13.     {  
    14.         private SQLHelper sqlhelper = null;  
    15.         int tmpFileID;//方便下面的获取文件ID  
    16.         public FileServiceDAL()  
    17.         {  
    18.             sqlhelper = new SQLHelper();  
    19.         }  
    20.   
    21.   
    22.         /// <summary>  
    23.         /// 上传附件  
    24.         /// </summary>  
    25.         /// <param name="fileTitle">附件标题</param>  
    26.         /// <param name="fileContent">附件内容</param>  
    27.         /// <param name="fileType">附件格式</param>  
    28.         /// <returns>返回附件插入数据库的即时ID</returns>  
    29.         public int UpLoadFile(string fileTitle, byte[] fileContent, string fileType)  
    30.         {  
    31.   
    32.             //返回ID的sql语句,这个看尾部...Select @@IDENTITY  
    33.             string sql = "insert  into Table_UploadFile(FileTitle,FileContent, FileType)values (@FileTitle,@FileContent,@FileType);Select @@IDENTITY";  
    34.             //给sql语句添加参数  
    35.             SqlParameter[] param = new SqlParameter[3]  
    36.             {  
    37.                 
    38.                   new SqlParameter("@FileTitle", fileTitle),  
    39.                   new SqlParameter("@FileContent", fileContent),  
    40.                   new SqlParameter("@FileType",fileType)  
    41.                      
    42.             };  
    43.             //调用数据库操作  
    44.             //与Select @@IDENTITY配套的ExecuteScalar  
    45.             int res = Convert.ToInt32(sqlhelper.ExecuteScalar(sql, CommandType.Text, param).ToString());  
    46.             if (res != 0)  
    47.             {  
    48.                 return res;//直接返回就是操作后的FileID  
    49.             }  
    50.             else  
    51.             {  
    52.                 return 0;  
    53.             }  
    54.   
    55.               
    56.         }  
    57.   
    58.   
    59.         /// <summary>  
    60.         /// 通过fileID下载新闻附件  
    61.         /// </summary>  
    62.         /// <param name="fileID">附件ID</param>  
    63.         /// <returns>通过DataTable类型返回文件</returns>  
    64.         public DataTable GetHadUpLoadFileByFileID(int fileID)  
    65.         {  
    66.             FileServiceModel file = new FileServiceModel();  
    67.             string sql = "Select FileTitle,FileContent,FileType from dbo.Table_UploadFile where FileID=@FileID";  
    68.   
    69.             SqlParameter[] param = new SqlParameter[1]  
    70.             {  
    71.                  
    72.                 new SqlParameter("@FileID", fileID)  
    73.               
    74.             };  
    75.             //基本数据库操作  
    76.             SqlDataReader res = sqlhelper.ExecuteReader(sql, CommandType.Text, param);  
    77.             DataTable db = new DataTable();  
    78.             db.Load(res);  
    79.             return db;  
    80.   
    81.         }  
    82.   
    83.   
    84.         /// <summary>  
    85.         /// 通过newsID获取fileID;  
    86.         /// </summary>  
    87.         /// <param name="newsID">新闻ID</param>  
    88.         /// <returns>附件ID</returns>  
    89.         public int GetFileIDByNewsID(int newsID)  
    90.         {  
    91.   
    92.             string sql = "Select FileID from dbo.Table_News where NewsID=@NewsID";  
    93.   
    94.             SqlParameter[] param = new SqlParameter[1]{  
    95.                  
    96.                 new SqlParameter("@NewsID", newsID)  
    97.               
    98.             };  
    99.             SqlDataReader res = sqlhelper.ExecuteReader(sql, CommandType.Text, param);  
    100.               
    101.             while (res.Read())  
    102.             {  
    103.                 //res.GetInt32(res.GetOrdinal("FileID"));//原本是在这样,但是报错了  
    104.                 //因为FileID也许为空,所以就要先判断是否为空,因为这个GetOrdinal()很矫情,受不了  
    105.                 if (!res.IsDBNull(res.GetOrdinal("FileID")))  
    106.                     tmpFileID = res.GetInt32(res.GetOrdinal("FileID"));  
    107.             }  
    108.               
    109.             return tmpFileID;  
    110.   
    111.         }  
    112.     }  
    113. }  


    DAL层之ContactDAL

     
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using NewsPublish.IDAL;  
    6. using System.Data.SqlClient;  
    7. using NewsPublish.Model;  
    8. using System.Data;  
    9. using System.Text.RegularExpressions;  
    10.   
    11.   
    12. namespace NewsPublish.DAL  
    13. {  
    14.     public class ContactDAL : IContactDAL  
    15.     {  
    16.         private SQLHelper sqlhelper = null;  
    17.    
    18.   
    19.         //构造函数  
    20.         public  ContactDAL()  
    21.         {  
    22.             sqlhelper = new SQLHelper();  
    23.         }  
    24.   
    25.   
    26.   
    27.   
    28.         /// <summary>  
    29.         /// 通过用户名获取联系人  
    30.         /// </summary>  
    31.         /// <param name="userName">用户名</param>  
    32.         /// <returns>联系人列表</returns>  
    33.         public List<ContactModel> GetMyContactByUserName(string userName)  
    34.         {  
    35.             string myContact = null;  
    36.             List<ContactModel> contactList = new List<ContactModel>();  
    37.             //选择dbo.Table_Contact中该用户的联系人  
    38.             string sql = "select ContactName from dbo.Table_Contact where UserName=@UserName ";  
    39.             SqlParameter[] param = new SqlParameter[1]  
    40.             {  
    41.                 new SqlParameter("@UserName", userName)  
    42.             };  
    43.   
    44.             SqlDataReader res = sqlhelper.ExecuteReader(sql, CommandType.Text, param);  
    45.   
    46.             while (res.Read())  
    47.             {  
    48.                 //myContact = res.GetString(res.GetOrdinal("ContactName"));  
    49.                 //因为FileID也许为空,所以就要先判断是否为空  
    50.                 if (!res.IsDBNull(res.GetOrdinal("ContactName")))  
    51.                 {  
    52.                     myContact = res.GetString(res.GetOrdinal("ContactName"));  
    53.   
    54.                 }  
    55.                  
    56.             }  
    57.             if (myContact != null)  
    58.             {  
    59.                 //这个函数是根据逗号把字符串的联系人分割开来,放进列表中  
    60.                 String[] array = Regex.Split(myContact, ",", RegexOptions.IgnoreCase);  
    61.   
    62.                 for (int i = 0; i < array.Length; i++)  
    63.                 {  
    64.                     //c#的创建对象真是都人性化,这样子把相应的属性就赋值了,nice  
    65.                     ContactModel contact = new ContactModel()  
    66.                     {  
    67.                         ContactName = array[i]  
    68.   
    69.                     };  
    70.                     contactList.Add(contact);  
    71.                 }  
    72.   
    73.                   
    74.             }  
    75.   
    76.             return contactList;  
    77.         }  
    78.   
    79.   
    80.   
    81.   
    82.   
    83.   
    84.         /// <summary>  
    85.         /// 根据用户名和联系人添加联系人  
    86.         /// </summary>  
    87.         /// <param name="newContact">用户名</param>  
    88.         /// <returns>添加是否成功</returns>  
    89.         public bool AddContact(string userName,string newContact)  
    90.         {  
    91.               
    92.             //因为考虑到第一次由空联系人添加联系人会有些麻烦,所以每次添加的时候都判断一下联系人是否为空  
    93.             //并且在前端要有提示:没输入一个联系人就要用逗号隔开  
    94.             string contactName = string.Empty;  
    95.             //选择dbo.Table_Contact中该用户的联系人  
    96.             string sql = "select ContactName from dbo.Table_Contact where UserName=@UserName ";  
    97.             SqlParameter[] param = new SqlParameter[1]  
    98.             {  
    99.                 new SqlParameter("@UserName", userName)  
    100.             };  
    101.             SqlDataReader res = sqlhelper.ExecuteReader(sql, CommandType.Text, param);  
    102.   
    103.             while (res.Read())  
    104.             {  
    105.                  
    106.                 if (!res.IsDBNull(res.GetOrdinal("ContactName")))  
    107.                     contactName = res.GetString(res.GetOrdinal("ContactName"));  
    108.             }  
    109.             //注意了哟:如果一开始的时候,数据为空的话,就插入,否则则是更新..因为一开始没数据的话更新个屌阿-_-  
    110.             if (contactName.Length == 0)  
    111.             {  
    112.                 contactName = newContact;  
    113.                 sql = "insert into dbo.Table_Contact(UserName,ContactName) values (@UserName,@ContactName)";  
    114.                 SqlParameter[] parm1 = new SqlParameter[2]  
    115.                 {  
    116.                    new SqlParameter("@UserName",userName),  
    117.                    new SqlParameter("@ContactName",contactName)  
    118.                 };  
    119.                 int tmp1 = sqlhelper.ExecuteNonQuery(sql,CommandType.Text,parm1);  
    120.                 if (tmp1 != 0)  
    121.                 {  
    122.                     return true;  
    123.                 }  
    124.                 else  
    125.                 {  
    126.                     return false;  
    127.                 }  
    128.             }  
    129.                   
    130.             else  
    131.             {  
    132.                 contactName = contactName + newContact;  
    133.                 sql = "update dbo.Table_Contact set ContactName=@ContactName where UserName=@UserName  ";  
    134.                 //创建参数数组  
    135.                 SqlParameter[] param1 = new SqlParameter[2]  
    136.                 {  
    137.                 new SqlParameter("@UserName", userName),  
    138.                 new SqlParameter("@ContactName", contactName)  
    139.                   
    140.                 };  
    141.                 int tmp2 = sqlhelper.ExecuteNonQuery(sql, CommandType.Text, param1);  
    142.                 if (tmp2 != 0)  
    143.                 {  
    144.                     return true;  
    145.                 }  
    146.                 else  
    147.                 {  
    148.                     return false;  
    149.                 }  
    150.             }  
    151.                   
    152.   
    153.                
    154.         }  
    155.     }  
    156. }  


    DAL层之NewsHaveSetContactDAL

     
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using NewsPublish.IDAL;  
    6. using System.Data.SqlClient;  
    7. using NewsPublish.Model;  
    8. using System.Data;  
    9. namespace NewsPublish.DAL  
    10. {  
    11.     public class NewsHaveSetContactDAL : INewsHaveSetContactDAL,IComparer<NewsModel>  
    12.     {  
    13.         private SQLHelper sqlhelper = null;  
    14.         public NewsHaveSetContactDAL()  
    15.         {  
    16.             sqlhelper = new SQLHelper();  
    17.         }  
    18.   
    19.   
    20.         /// <summary>  
    21.         /// 哈哈,这个是查字典写的快排的Compare方法,下文中根据ID列表一条一条的去新闻的话,这样子是不会  
    22.         /// 自动排序的嘛,所以自己就随便调用一下系统的快排队时间进行排序嘛.  
    23.         /// </summary>  
    24.         /// <param name="x"></param>  
    25.         /// <param name="y"></param>  
    26.         /// <returns></returns>  
    27.         public int Compare(NewsModel x, NewsModel y)  
    28.         {   
    29.              if(x.Date>y.Date)  
    30.                  return -1;  
    31.              if (x.Date < y.Date)  
    32.                  return 1;  
    33.              return 0;  
    34.         }  
    35.   
    36.   
    37.         /// <summary>  
    38.         /// 得到将我加入联系人的新闻的新闻ID列表--------邮箱新闻或者未登录主页(传参"游客")  
    39.         /// </summary>  
    40.         /// <returns>将我加入联系人的新闻的新闻ID列表</returns>  
    41.         public List<NewsHaveSetContactModel> GetNewsIDOfNewsHaveAddMeIntoContact(string contactName)  
    42.         {  
    43.               
    44.             //其实这个方法是有漏洞的呢,因为like的不严谨.也是C语言的字符串匹配内容,待会再查查KMP吧,必须严谨才行  
    45.             //创建新闻ID列表  
    46.             List<NewsHaveSetContactModel> newsIDOfTheNewsHaveContactList = new List<NewsHaveSetContactModel>();  
    47.             //选择dbo.Table_Contact中该用户的联系人  
    48.   
    49.            //注意标点符号写法.  
    50.             string sql = "select NewsID from dbo.Table_NewsHaveSetContact where ContactName like '%'+@ContactName+'%'";  
    51.             //“Select * From table Where field like ’%‘+@field+’%‘”;//必须感谢  
    52.             
    53.              SqlParameter[] param = new SqlParameter[1]  
    54.              {  
    55.                  
    56.                  new SqlParameter("@ContactName", contactName)  
    57.   
    58.              };  
    59.                 
    60.             SqlDataReader res = sqlhelper.ExecuteReader(sql, CommandType.Text, param);  
    61.               
    62.   
    63.               
    64.              
    65.             //获取新闻ID  
    66.             while (res.Read())  
    67.             {  
    68.                 NewsHaveSetContactModel newsList = new NewsHaveSetContactModel();  
    69.   
    70.                 //给新闻ID变量赋值  
    71.                 //使用SqlDataReader的GetOrdinal()方法,获得列的序号,输入参数为列名  
    72.                 newsList.NewsID = res.GetInt32(res.GetOrdinal("NewsID"));  
    73.                 newsIDOfTheNewsHaveContactList.Add(newsList);  
    74.             }  
    75.   
    76.             return newsIDOfTheNewsHaveContactList;  
    77.         }  
    78.   
    79.   
    80.   
    81.   
    82.          /// <summary>  
    83.          /// 根据相应逻辑获取到的新闻ID列表(根据联系人来分)   然后得到相应逻辑的新闻列表  
    84.          /// </summary>  
    85.          /// <param name="newsIDList">相应逻辑获取到的新闻ID列表</param>  
    86.          /// <returns>相应逻辑的新闻列表</returns>  
    87.         public List<NewsModel> GetNewsListOfNewsHaveAddMeIntoContact(List<NewsHaveSetContactModel> newsIDList)  
    88.         {  
    89.             //创建新闻对象列表  
    90.             List<NewsModel> newsList = new List<NewsModel>();  
    91.             for (int i = 0; i < newsIDList.Count; i++)  
    92.             {  
    93.                 int newsID = newsIDList[i].NewsID;  
    94.                 //选择dbo.Table_News中该用户已发布新闻的标题、作者、发布日期  
    95.                 string sql = "select top 10 NewsID, Title,UserName,Date from dbo.Table_News where NewsID=@NewsID  order by Date desc";  
    96.                 SqlParameter[] param = new SqlParameter[1]  
    97.                 {  
    98.                  
    99.                    new SqlParameter("@NewsID", newsID)  
    100.   
    101.                 };  
    102.                 SqlDataReader res = sqlhelper.ExecuteReader(sql,CommandType.Text,param);  
    103.   
    104.                 //获取新闻对象  
    105.                  
    106.                 while (res.Read())  
    107.                 {  
    108.                     //创建新闻对象  
    109.                     NewsModel news = new NewsModel();  
    110.                      
    111.                     //给新闻对象属性赋值  
    112.                     //使用SqlDataReader的GetOrdinal()方法,获得列的序号,输入参数为列名  
    113.                     news.NewsID = res.GetInt32(res.GetOrdinal("NewsID"));  
    114.                     news.Title = res.GetString(res.GetOrdinal("Title"));  
    115.                     news.UserName = res.GetString(res.GetOrdinal("UserName"));  
    116.                     news.Date = res.GetDateTime(res.GetOrdinal("Date"));  
    117.                     //将新闻添加到列表中  
    118.                     newsList.Add(news);  
    119.                 }  
    120.             }  
    121.             //给获取到的新闻列表排序  
    122.             NewsHaveSetContactDAL dc = new NewsHaveSetContactDAL();  
    123.             newsList.Sort(dc);  
    124.             return newsList;  
    125.         }  
    126.   
    127.   
    128.         /// <summary>  
    129.         ///  得到将游客和我作为接收人的新闻的新闻ID列表---------登录主页  
    130.         /// 其实一开始我还忘了我本人发布的新闻也放在登录主页呢..囧..  
    131.         /// </summary>  
    132.         /// <param name="contactName">作为联系人的名字---"我"</param>  
    133.         /// <returns>得到将游客,我作为接收人,以及我发布的新闻的ID列表</returns>  
    134.         public List<NewsHaveSetContactModel> GetNewsIDOfVisitorAndMe(string contactName)   
    135.         {  
    136.             //创建新闻ID列表  
    137.             List<NewsHaveSetContactModel> newsIDOfTheNewsHaveContactList = new List<NewsHaveSetContactModel>();  
    138.             //选择dbo.Table_Contact中该用户的联系人  
    139.   
    140.             //------------(1)选择本人作为新闻联系人的newsID  
    141.             string sql = "select NewsID from dbo.Table_NewsHaveSetContact where ContactName like '%'+@ContactName+'%'";  
    142.             //“Select * From table Where field like ’%‘+@field+’%‘”;//必须感谢  
    143.   
    144.             SqlParameter[] param = new SqlParameter[1]  
    145.              {  
    146.                  
    147.                  new SqlParameter("@ContactName", contactName)  
    148.   
    149.              };  
    150.   
    151.             SqlDataReader res = sqlhelper.ExecuteReader(sql, CommandType.Text, param);  
    152.   
    153.   
    154.   
    155.   
    156.             //获取新闻ID  
    157.             while (res.Read())  
    158.             {  
    159.                 NewsHaveSetContactModel newsList = new NewsHaveSetContactModel();  
    160.   
    161.                 //给新闻ID变量赋值  
    162.                 //使用SqlDataReader的GetOrdinal()方法,获得列的序号,输入参数为列名  
    163.                 newsList.NewsID = res.GetInt32(res.GetOrdinal("NewsID"));  
    164.                 newsIDOfTheNewsHaveContactList.Add(newsList);  
    165.             }  
    166.   
    167.   
    168.             //-------------(2)获取面向游客新闻的newsID  
    169.             string sql1 = "select NewsID from dbo.Table_NewsHaveSetContact where ContactName like '游客'";  
    170.             //“Select * From table Where field like ’%‘+@field+’%‘”;//必须感谢  
    171.   
    172.              
    173.   
    174.             SqlDataReader res1 = sqlhelper.ExecuteReader(sql1);  
    175.             //获取新闻ID  
    176.             while (res1.Read())  
    177.             {  
    178.                 NewsHaveSetContactModel newsList = new NewsHaveSetContactModel();  
    179.   
    180.                 //给新闻ID变量赋值  
    181.                 //使用SqlDataReader的GetOrdinal()方法,获得列的序号,输入参数为列名  
    182.                 newsList.NewsID = res1.GetInt32(res1.GetOrdinal("NewsID"));  
    183.                 newsIDOfTheNewsHaveContactList.Add(newsList);  
    184.             }  
    185.   
    186.   
    187.             //------------(3)获取本人发布的新闻  
    188.             //选择dbo.Table_News中该用户已发布新闻的标题、作者、发布日期  
    189.             string sql2 = "select NewsID from dbo.Table_News where UserName=@ContactName  order by Date desc";  
    190.   
    191.             SqlParameter[] param2 = new SqlParameter[1]{  
    192.                  
    193.                 new SqlParameter("@ContactName", contactName)  
    194.               
    195.             };  
    196.             //含有参数的的传参方法,迷惑的话可以看一下Sqlhelper类(在DAL层中)  
    197.             SqlDataReader res2 = sqlhelper.ExecuteReader(sql2, CommandType.Text, param2);  
    198.   
    199.             //获取新闻对象  
    200.             while (res2.Read())  
    201.             {  
    202.                 NewsHaveSetContactModel newsList = new NewsHaveSetContactModel();  
    203.   
    204.                 //给新闻ID变量赋值  
    205.                 //使用SqlDataReader的GetOrdinal()方法,获得列的序号,输入参数为列名  
    206.                 newsList.NewsID = res2.GetInt32(res1.GetOrdinal("NewsID"));  
    207.                 //将新闻添加到列表中  
    208.                 newsIDOfTheNewsHaveContactList.Add(newsList);  
    209.             }  
    210.             //------------  
    211.   
    212.   
    213.             return newsIDOfTheNewsHaveContactList;  
    214.         }  
    215.   
    216.         /// <summary>  
    217.         /// 从联系人添加新闻接收人到具体新闻  
    218.         /// </summary>  
    219.         /// <param name="newsID">新闻ID</param>  
    220.         /// <param name="contactName">联系人名字</param>  
    221.         /// <returns>添加是否成功</returns>  
    222.         public bool AddNewsReceiver(int newsID, string contactName)  
    223.         {  
    224.             string sql = "insert  into dbo.Table_NewsHaveSetContact(NewsID,ContactName )values (@NewsID,@ContactName)";  
    225.             SqlParameter[] param = new SqlParameter[2]  
    226.             {  
    227.                  
    228.                 new SqlParameter("@NewsID", newsID),  
    229.                 new SqlParameter("@ContactName", contactName)  
    230.                  
    231.               
    232.             };  
    233.             //调用数据库操作  
    234.             int res = sqlhelper.ExecuteNonQuery(sql, CommandType.Text, param);  
    235.   
    236.             if (res != 0)  
    237.             {  
    238.                 return true;  
    239.             }  
    240.             else  
    241.             {  
    242.                 return false;  
    243.             }  
    244.         }  
    245.   
    246.       
    247.   
    248. }  
    249. }  


    IBLL层

    IBLL层之INewsBLL

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using NewsPublish.Model;  
    6.   
    7. namespace NewsPublish.IBLL  
    8. {  
    9.     public interface INewsBLL  
    10.     {  
    11.         /// <summary>  
    12.         /// 得到所有新闻列表,主要用于主页显示  
    13.         /// </summary>  
    14.         /// <returns>所有新闻列表</returns>  
    15.         List<NewsModel> GetAllNewsList();  
    16.   
    17.         /// <summary>  
    18.         /// 根据用户名,得到用户已发布的新闻列表  
    19.         /// </summary>  
    20.         /// <param name="userName">用户名</param>  
    21.         /// <returns>用户已发布的新闻列表</returns>  
    22.         List<NewsModel> GetUserNewsListByUserName(string userName);  
    23.   
    24.         /// <summary>  
    25.         /// 发布新闻  
    26.         /// </summary>  
    27.         /// <param name="title">新闻标题</param>  
    28.         /// <param name="date">发布新闻系统时间</param>  
    29.         /// <param name="userName">用户名(新闻,和用户名都有这么一个属性)</param>  
    30.         /// <param name="content">新闻内容</param>  
    31.         /// <param name="fileID">新闻附件ID</param>  
    32.         /// <returns>返回插入的新闻ID,这对后续处理很有帮助,good</returns>  
    33.         int PublishNews(string title, DateTime date, string userName, string content, int fileID);  
    34.   
    35.           
    36.         /// <summary>  
    37.         /// 根据新闻ID,得到详细新闻  
    38.         /// </summary>  
    39.         /// <param name="newsID">新闻ID</param>  
    40.         /// <returns>详细新闻</returns>  
    41.         NewsModel GetDetailNewsByNewsID(int newsID);  
    42.     }  
    43. }  


    IBLL层之IUserBLL

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using NewsPublish.Model;  
    6.   
    7. namespace NewsPublish.IBLL  
    8. {  
    9.     public interface IUserBLL  
    10.     {  
    11.         /// <summary>  
    12.         /// 登录  
    13.         /// </summary>  
    14.         /// <param name="userName">用户名</param>  
    15.         /// <param name="password">密码</param>  
    16.         /// <returns>登录是否成功</returns>  
    17.         bool Login(string userName, string password);  
    18.   
    19.         /// <summary>  
    20.         /// 注册  
    21.         /// </summary>  
    22.         /// <param name="userName">用户名</param>  
    23.         /// <param name="password">密码</param>  
    24.         /// <returns>注册是否成功,只不过用int类型更好的变化://flag=1,2,3  分别为:用户名已存在/注册成功/注册失败</returns>  
    25.         int Register(string userName, string password);  
    26.     }  
    27. }  


    IBLL层之IFileServiceBLL

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Data;  
    4. using System.Linq;  
    5. using System.Web;  
    6.   
    7. namespace NewsPublish.IBLL  
    8. {  
    9.     public interface IFileServiceBLL  
    10.     {  
    11.   
    12.         /// <summary>  
    13.         /// 上传附件  
    14.         /// </summary>  
    15.         /// <param name="fileTitle">附件标题</param>  
    16.         /// <param name="fileContent">附件内容</param>  
    17.         /// <param name="fileType">附件格式</param>  
    18.         /// <returns>返回附件插入数据库的即时ID</returns>  
    19.         int UpLoadFile(string fileTitle, byte[] fileContent, string fileType);  
    20.   
    21.   
    22.         /// <summary>  
    23.         /// 通过fileID下载新闻附件  
    24.         /// </summary>  
    25.         /// <param name="fileID">附件ID</param>  
    26.         /// <returns>通过DataTable类型返回文件</returns>  
    27.         DataTable GetHadUpLoadFileByFileID(int fileID);  
    28.   
    29.   
    30.   
    31.         /// <summary>  
    32.         /// 通过newsID获取fileID;  
    33.         /// </summary>  
    34.         /// <param name="newsID">新闻ID</param>  
    35.         /// <returns>附件ID</returns>  
    36.          int GetFileIDByNewsID(int newsID);  
    37.     }  
    38. }  


    IBLL层之IContactBLL

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using NewsPublish.Model;  
    6.   
    7. namespace NewsPublish.IBLL  
    8. {  
    9.     public interface IContactBLL  
    10.     {  
    11.         /// <summary>  
    12.         /// 通过用户名获取联系人  
    13.         /// </summary>  
    14.         /// <param name="userName">用户名</param>  
    15.         /// <returns>联系人列表</returns>  
    16.         List<ContactModel> GetMyContactByUserName(string userName);  
    17.   
    18.   
    19.         /// <summary>  
    20.         /// 根据用户名和联系人添加联系人  
    21.         /// </summary>  
    22.         /// <param name="newContact">用户名</param>  
    23.         /// <returns>添加是否成功</returns>  
    24.         bool AddContact(string userName, string newContact);  
    25.     }  
    26. }  


    IBLL层之INewsHaveSetContactBLL

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using NewsPublish.Model;  
    6.   
    7.   
    8. namespace NewsPublish.IBLL  
    9. {  
    10.     public interface INewsHaveSetContactBLL  
    11.     {  
    12.   
    13.   
    14.         /// <summary>  
    15.         /// 得到将我加入联系人的新闻的新闻ID列表--------未登录主页+个人中心  
    16.         /// </summary>  
    17.         /// <returns>将我加入联系人的新闻的新闻ID列表</returns>  
    18.         List<NewsHaveSetContactModel> GetNewsIDOfNewsHaveAddMeIntoContact(string contactName);  
    19.   
    20.   
    21.         /// <summary>  
    22.         /// 根据相应逻辑获取到的新闻ID列表(根据联系人来分)   然后得到相应逻辑的新闻列表  
    23.         /// </summary>  
    24.         /// <returns>将我加入联系人的新闻列表</returns>  
    25.         List<NewsModel> GetNewsListOfNewsHaveAddMeIntoContact(List<NewsHaveSetContactModel> newsIDList);  
    26.   
    27.   
    28.         /// <summary>  
    29.         ///  得到将游客和我作为接收人的新闻的新闻ID列表---------登录主页  
    30.         /// 其实一开始我还忘了我本人发布的新闻也放在登录主页呢..囧..  
    31.         /// </summary>  
    32.         /// <param name="contactName">作为联系人的名字---"我"</param>  
    33.         /// <returns>得到将游客,我作为接收人,以及我发布的新闻的ID列表</returns>  
    34.         List<NewsHaveSetContactModel> GetNewsIDOfVisitorAndMe(string contactName);  
    35.   
    36.   
    37.         /// <summary>  
    38.         /// 从联系人添加新闻接收人到具体新闻  
    39.         /// </summary>  
    40.         /// <param name="newsID">新闻ID</param>  
    41.         /// <param name="contactName">联系人名字</param>  
    42.         /// <returns>添加是否成功</returns>  
    43.         bool AddNewsReceiver(int newsID, string contactName);  
    44.     }  
    45. }  


    BLL层

    BLL层之NewsBLL

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using NewsPublish.IBLL;  
    6. using NewsPublish.IDAL;  
    7. using NewsPublish.Model;  
    8.   
    9. namespace NewsPublish.BLL  
    10. {  
    11.     public class NewsBLL : INewsBLL  
    12.     {  
    13.   
    14.         INewsDAL _newsDAL = Factory.DALFactory.CreateNewsDAL();  
    15.   
    16.         /// <summary>  
    17.         /// 得到所有新闻列表,主要用于主页显示  
    18.         /// </summary>  
    19.         /// <returns>所有新闻列表</returns>  
    20.         public List<NewsModel> GetAllNewsList()  
    21.         {  
    22.             return _newsDAL.GetAllNewsList();  
    23.         }  
    24.   
    25.         /// <summary>  
    26.         /// 根据用户名,得到用户已发布的新闻列表  
    27.         /// </summary>  
    28.         /// <param name="userName">用户名</param>  
    29.         /// <returns>用户已发布的新闻列表</returns>  
    30.         public List<NewsModel> GetUserNewsListByUserName(string userName)  
    31.         {  
    32.             return _newsDAL.GetUserNewsListByUserName(userName);  
    33.         }  
    34.   
    35.         /// <summary>  
    36.         /// 发布新闻  
    37.         /// </summary>  
    38.         /// <param name="title">新闻标题</param>  
    39.         /// <param name="date">发布新闻系统时间</param>  
    40.         /// <param name="userName">用户名(新闻,和用户名都有这么一个属性)</param>  
    41.         /// <param name="content">新闻内容</param>  
    42.         /// <param name="fileID">新闻附件ID</param>  
    43.         /// <returns>返回插入的新闻ID,这对后续处理很有帮助,good</returns>  
    44.         public int PublishNews(string title, DateTime date, string userName, string content, int fileID)  
    45.         {  
    46.             return _newsDAL.PublishNews(title, date, userName, content,fileID);  
    47.         }  
    48.   
    49.         /// <summary>  
    50.         /// 根据新闻ID,得到详细新闻  
    51.         /// </summary>  
    52.         /// <param name="newsID">新闻ID</param>  
    53.         /// <returns>详细新闻</returns>  
    54.         public NewsModel GetDetailNewsByNewsID(int newsID)  
    55.         {  
    56.   
    57.             return _newsDAL.GetDetailNewsByNewsID(newsID);  
    58.         }  
    59.     }  
    60. }  


    BLL层之UserBLL

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Data;  
    6. using NewsPublish.Model;  
    7. using NewsPublish.IDAL;  
    8. using NewsPublish.IBLL;  
    9.   
    10. namespace NewsPublish.BLL  
    11. {  
    12.     public class UserBLL:IUserBLL  
    13.     {  
    14.   
    15.         IUserDAL _userDAL = Factory.DALFactory.CreateUserDAL();  
    16.   
    17.   
    18.         /// <summary>  
    19.         /// 登录  
    20.         /// </summary>  
    21.         /// <param name="userName">用户名</param>  
    22.         /// <param name="password">密码</param>  
    23.         /// <returns>登录是否成功</returns>  
    24.         public bool Login(string userName, string password)  
    25.         {  
    26.             return _userDAL.Login(userName, password);  
    27.         }  
    28.   
    29.         /// <summary>  
    30.         /// 注册  
    31.         /// </summary>  
    32.         /// <param name="userName">用户名</param>  
    33.         /// <param name="password">密码</param>  
    34.         /// <returns>注册是否成功,只不过用int类型更好的变化://flag=1,2,3  分别为:用户名已存在/注册成功/注册失败</returns>  
    35.         public int Register(string userName, string password)  
    36.         {  
    37.             return _userDAL.Register(userName, password);  
    38.         }  
    39.           
    40.     }  
    41. }  


    BLL层之FileServiceBLL

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Data;  
    4. using System.Linq;  
    5. using System.Web;  
    6. using NewsPublish.IBLL;  
    7. using NewsPublish.IDAL;  
    8.   
    9. namespace NewsPublish.BLL  
    10. {  
    11.     public class FileServiceBLL : IFileServiceBLL  
    12.     {  
    13.         IFileServiceDAL _fileServiceDAL = Factory.DALFactory.CreateFileServiceDAL();  
    14.   
    15.         /// <summary>  
    16.         /// 上传附件  
    17.         /// </summary>  
    18.         /// <param name="fileTitle">附件标题</param>  
    19.         /// <param name="fileContent">附件内容</param>  
    20.         /// <param name="fileType">附件格式</param>  
    21.         /// <returns>返回附件插入数据库的即时ID</returns>  
    22.         public int UpLoadFile(string fileTitle, byte[] fileContent, string fileType)  
    23.         {  
    24.             return _fileServiceDAL.UpLoadFile(fileTitle, fileContent, fileType);  
    25.         }  
    26.   
    27.   
    28.         /// <summary>  
    29.         /// 通过fileID下载新闻附件  
    30.         /// </summary>  
    31.         /// <param name="fileID">附件ID</param>  
    32.         /// <returns>通过DataTable类型返回文件</returns>  
    33.         public DataTable GetHadUpLoadFileByFileID(int fileID)  
    34.         {  
    35.             return _fileServiceDAL.GetHadUpLoadFileByFileID(fileID);  
    36.         }  
    37.   
    38.   
    39.   
    40.         /// <summary>  
    41.         /// 通过newsID获取fileID;  
    42.         /// </summary>  
    43.         /// <param name="newsID">新闻ID</param>  
    44.         /// <returns>附件ID</returns>  
    45.         public int GetFileIDByNewsID(int newsID)  
    46.         {  
    47.             return _fileServiceDAL.GetFileIDByNewsID(newsID);  
    48.         }  
    49.     }  
    50. }  


    BLL层之ContactBLL

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Data;  
    6. using NewsPublish.Model;  
    7. using NewsPublish.IDAL;  
    8. using NewsPublish.IBLL;  
    9. namespace NewsPublish.BLL  
    10. {  
    11.     public class ContactBLL :IContactBLL  
    12.     {  
    13.         IContactDAL _contactDAL = Factory.DALFactory.CreateContactDAL();  
    14.   
    15.         /// <summary>  
    16.         /// 通过用户名获取联系人  
    17.         /// </summary>  
    18.         /// <param name="userName">用户名</param>  
    19.         /// <returns>联系人列表</returns>  
    20.         public List<ContactModel> GetMyContactByUserName(string userName)  
    21.         {  
    22.             return _contactDAL.GetMyContactByUserName(userName);  
    23.         }  
    24.   
    25.   
    26.         /// <summary>  
    27.         /// 根据用户名和联系人添加联系人  
    28.         /// </summary>  
    29.         /// <param name="newContact">用户名</param>  
    30.         /// <returns>添加是否成功</returns>  
    31.        public bool AddContact(string userName, string newContact)  
    32.        {  
    33.            return _contactDAL.AddContact(userName, newContact);  
    34.        }  
    35.     }  
    36. }  


    BLL层之NewsHaveSetContactBLL

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using NewsPublish.IBLL;  
    6. using NewsPublish.IDAL;  
    7. using NewsPublish.Model;  
    8.   
    9. namespace NewsPublish.BLL  
    10. {  
    11.     public class NewsHaveSetContactBLL :INewsHaveSetContactBLL  
    12.     {  
    13.         INewsHaveSetContactDAL _newsHaveSetContactDAL = Factory.DALFactory.CreateNewsHaveSetContactDAL();  
    14.   
    15.   
    16.         /// <summary>  
    17.         /// 得到将我加入联系人的新闻的新闻ID列表--------未登录主页+个人中心  
    18.         /// </summary>  
    19.         /// <returns>将我加入联系人的新闻的新闻ID列表</returns>  
    20.         public List<NewsHaveSetContactModel> GetNewsIDOfNewsHaveAddMeIntoContact(string contactName)  
    21.         {  
    22.             return _newsHaveSetContactDAL.GetNewsIDOfNewsHaveAddMeIntoContact(contactName);  
    23.         }  
    24.   
    25.   
    26.   
    27.         /// <summary>  
    28.         /// 根据相应逻辑获取到的新闻ID列表(根据联系人来分)   然后得到相应逻辑的新闻列表  
    29.         /// </summary>  
    30.         /// <param name="newsIDList">相应逻辑获取到的新闻ID列表</param>  
    31.         /// <returns>相应逻辑的新闻列表</returns>  
    32.         public List<NewsModel> GetNewsListOfNewsHaveAddMeIntoContact(List<NewsHaveSetContactModel> newsIDList)  
    33.         {  
    34.             return _newsHaveSetContactDAL.GetNewsListOfNewsHaveAddMeIntoContact(newsIDList);  
    35.         }  
    36.   
    37.         /// <summary>  
    38.         ///  得到将游客和我作为接收人的新闻的新闻ID列表---------登录主页  
    39.         /// 其实一开始我还忘了我本人发布的新闻也放在登录主页呢..囧..  
    40.         /// </summary>  
    41.         /// <param name="contactName">作为联系人的名字---"我"</param>  
    42.         /// <returns>得到将游客,我作为接收人,以及我发布的新闻的ID列表</returns>  
    43.         public List<NewsHaveSetContactModel> GetNewsIDOfVisitorAndMe(string contactName)  
    44.         {  
    45.             return _newsHaveSetContactDAL.GetNewsIDOfVisitorAndMe(contactName);  
    46.         }  
    47.   
    48.   
    49.   
    50.         /// <summary>  
    51.         /// 从联系人添加新闻接收人到具体新闻  
    52.         /// </summary>  
    53.         /// <param name="newsID">新闻ID</param>  
    54.         /// <param name="contactName">联系人名字</param>  
    55.         /// <returns>添加是否成功</returns>  
    56.         public bool AddNewsReceiver(int newsID, string contactName)  
    57.         {  
    58.             return _newsHaveSetContactDAL.AddNewsReceiver(newsID, contactName);  
    59.         }  
    60.     }  
    61. }  


    Factory层

    Factory层之DALFactory

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using NewsPublish.IDAL;  
    6. using NewsPublish.DAL;  
    7.   
    8. namespace NewsPublish.Factory  
    9. {  
    10.     public static class DALFactory  
    11.     {  
    12.         /// <summary>  
    13.         /// 建立工厂是为了使得建立对象的时候不用了解另一层中类中的细节降低耦合,只需知道工厂会帮你生产出来需要用的对象即可  
    14.         /// </summary>  
    15.         /// <returns></returns>  
    16.         public static IUserDAL CreateUserDAL()  
    17.         {  
    18.             return new UserDAL();  
    19.         }  
    20.   
    21.         public static INewsDAL CreateNewsDAL()  
    22.         {  
    23.             return new NewsDAL();  
    24.         }  
    25.         public static IContactDAL CreateContactDAL()  
    26.         {   
    27.              return new ContactDAL();  
    28.         }  
    29.         public static INewsHaveSetContactDAL CreateNewsHaveSetContactDAL()  
    30.         {  
    31.             return new NewsHaveSetContactDAL();  
    32.         }  
    33.   
    34.         public static IFileServiceDAL CreateFileServiceDAL()  
    35.         {  
    36.             return new FileServiceDAL();  
    37.         }  
    38.     }  
    39. }  


    Factory层之BLLFactory

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using NewsPublish.IBLL;  
    6. using NewsPublish.BLL;  
    7.   
    8. namespace NewsPublish.Factory  
    9. {  
    10.     public  class BLLFactory  
    11.     {  
    12.         public static IUserBLL CreatUserBLL()  
    13.         {  
    14.             return new UserBLL();  
    15.         }  
    16.   
    17.         public static INewsBLL CreateNewsBLL()  
    18.         {  
    19.             return new NewsBLL();  
    20.         }  
    21.   
    22.         public static IContactBLL CreateContactBLL()  
    23.         {  
    24.             return new ContactBLL();  
    25.         }  
    26.   
    27.         public static INewsHaveSetContactBLL CreateNewsHaveSetContactBLL()  
    28.         {  
    29.             return new NewsHaveSetContactBLL();  
    30.         }  
    31.   
    32.         public static IFileServiceBLL CreateFileServiceBLL()  
    33.         {  
    34.             return new FileServiceBLL();  
    35.         }  
    36.     }  
    37. }  

    界面层

    界面层之homepage(游客主页)

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.UI;  
    6. using System.Web.UI.WebControls;  
    7. using NewsPublish.IBLL;  
    8. using NewsPublish.Factory;  
    9. using NewsPublish.Model;  
    10.   
    11. namespace 新闻发布系统  
    12. {  
    13.     public partial class WebForm2 : System.Web.UI.Page  
    14.     {  
    15.         //依赖于下一层的接口  
    16.         INewsHaveSetContactBLL _iBLL;  
    17.         protected void Page_Load(object sender, EventArgs e)  
    18.         {  
    19.   
    20.             //如果是游客的话则会显示游客,并且把游客能看的新闻显示出来,否则已经用不上了.因为我重新设计了一个登录后的主页.  
    21.             if (Session["userName"] == null)  
    22.             {  
    23.                 LabelName.Text = "游客";//获取当前客户端的用户名  
    24.                 _iBLL = BLLFactory.CreateNewsHaveSetContactBLL();  
    25.                 List<NewsHaveSetContactModel> list = _iBLL.GetNewsIDOfNewsHaveAddMeIntoContact(LabelName.Text);  
    26.                   
    27.                 //DataGrid绑定数据  
    28.                 GdvhomePage.DataSource = _iBLL.GetNewsListOfNewsHaveAddMeIntoContact(list);  
    29.                 GdvhomePage.DataBind();  
    30.             }  
    31.             else  
    32.             {  
    33.                 LabelName.Text = Session["userName"].ToString();  
    34.             }  
    35.         }  
    36.     }  
    37. }  


    界面层之newscontent(游客查看具体新闻页)

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.UI;  
    6. using System.Web.UI.WebControls;  
    7. using NewsPublish.IBLL;  
    8. using NewsPublish.Factory;  
    9. using System.Data;  
    10. using NewsPublish.Model;  
    11. namespace 新闻发布系统  
    12. {  
    13.     public partial class newscontent : System.Web.UI.Page  
    14.     {  
    15.         //依赖于下一层的接口  
    16.         INewsBLL _ibll = BLLFactory.CreateNewsBLL();  
    17.         IFileServiceBLL _ifile = BLLFactory.CreateFileServiceBLL();  
    18.         protected void Page_Load(object sender, EventArgs e)  
    19.         {  
    20.               
    21.             //另一种接受客户端参数的形式  
    22.             ViewState["NewsID"] = Request.QueryString[0];  
    23.             int id = Convert.ToInt32(ViewState["NewsID"]);  
    24.   
    25.             //根据NewsID找到具体的新闻内容返回  
    26.             NewsModel newsModel = new NewsModel();  
    27.             newsModel=_ibll.GetDetailNewsByNewsID(id);  
    28.             TxtTitle.Text = newsModel.Title;  
    29.             TxtDate.Text = newsModel.Date.ToString();  
    30.             TxtUserName.Text = newsModel.UserName;  
    31.             TxtContent.Text = newsModel.Content;  
    32.              
    33.             //绑定下载数据源文件标题  
    34.             int fileID = _ifile.GetFileIDByNewsID(id);  
    35.             if (fileID != 0)//如果某一条新闻没有fileID的话,就要做相应的措施,否则会报错  
    36.             {  
    37.                 DataTable dt = new DataTable();  
    38.                 dt = _ifile.GetHadUpLoadFileByFileID(fileID);  
    39.                 LabelDisPlayFileTitle.Text = dt.Rows[0]["FileTitle"].ToString();  
    40.             }  
    41.             else  
    42.             {  
    43.                 LabelDisPlayFileTitle.Text = "没有下载文件";  
    44.                 BtnDownLoadFile.Visible = false;  
    45.             }  
    46.   
    47.         }  
    48.   
    49.   
    50.   
    51.   
    52.         /// <summary>  
    53.         /// 下载附件  
    54.         /// </summary>  
    55.         /// <param name="sender"></param>  
    56.         /// <param name="e"></param>  
    57.         protected void BtnDownLoadFile_Click(object sender, EventArgs e)  
    58.         {  
    59.   
    60.             ViewState["NewsID"] = Request.QueryString[0];  
    61.   
    62.             int id = Convert.ToInt32(ViewState["NewsID"]);  
    63.   
    64.             int fileID = _ifile.GetFileIDByNewsID(id);  
    65.   
    66.             //拿到要下载的文件    
    67.             DataTable dt = new DataTable();  
    68.             dt = _ifile.GetHadUpLoadFileByFileID(fileID);  
    69.   
    70.   
    71.             //其实只要拿到Data Table之类的数据表即可,接下来的一堆Response都是下载,我也看不大懂.-_-  
    72.             Response.Buffer = true;  
    73.             Response.Clear();  
    74.             //注意了:字节流(字节数组)成功的转为string类型  
    75.             Response.ContentType = dt.Rows[0]["FileType"].ToString();  
    76.             Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(dt.Rows[0]["FileTitle"].ToString()));  
    77.             Response.BinaryWrite((Byte[])dt.Rows[0]["FileContent"]);  
    78.             Response.Flush();  
    79.             Response.End();  
    80.         }  
    81.   
    82.   
    83.   
    84.         /// <summary>  
    85.         /// 返回主页  
    86.         /// </summary>  
    87.         /// <param name="sender"></param>  
    88.         /// <param name="e"></param>  
    89.         protected void BtnBackHome_Click(object sender, EventArgs e)  
    90.         {  
    91.             if (Session["userName"] == null)  
    92.             {  
    93.                 Response.Redirect("homepage.aspx");  
    94.             }  
    95.             else  
    96.             {  
    97.                 Response.Redirect("userhomepage.aspx");  
    98.             }  
    99.         }  
    100.   
    101.   
    102.   
    103.     }  
    104. }  


    界面层之login(登录页面)

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.UI;  
    6. using System.Web.UI.WebControls;  
    7. using NewsPublish.IBLL;  
    8.   
    9. namespace 新闻发布系统  
    10. {  
    11.     public partial class WebForm4 : System.Web.UI.Page  
    12.     {  
    13.         //依赖于下一层的接口  
    14.         IUserBLL _userBLL = NewsPublish.Factory.BLLFactory.CreatUserBLL();  
    15.         protected void Page_Load(object sender, EventArgs e)  
    16.         {  
    17.   
    18.         }  
    19.   
    20.         protected void BtnLogin_Click(object sender, EventArgs e)  
    21.         {  
    22.             string userName = TextUserName.Text.ToString();  
    23.             string password = TextPassWord.Text.ToString();  
    24.   
    25.             //对登录账号进行验证  
    26.             if (_userBLL.Login(userName, password))  
    27.             {  
    28.                 Response.Write("<script language=javascript>alert('登录成功!');</script>");  
    29.                 Session["userName"] = userName;  
    30.                 //Response.Redirect("personnel.aspx");  
    31.                 //注意了哦,如果用Response.Write弹出窗口的话,是使用不了那个Response.Redirect()进行跳转的  
    32.                 Response.Write("<script> window.location='personnel.aspx'</script>");  
    33.             }  
    34.             else  
    35.             {  
    36.                 Response.Write("<script language=javascript>alert('账号或者密码错误,登录失败!');</script>");  
    37.             }  
    38.         }  
    39.     }  
    40. }  


    界面层之register(注册页面)

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.UI;  
    6. using System.Web.UI.WebControls;  
    7. using NewsPublish.IBLL;  
    8.   
    9. namespace 新闻发布系统  
    10. {  
    11.     public partial class 测试 : System.Web.UI.Page  
    12.     {  
    13.   
    14.         //依赖于下一层的接口  
    15.         IUserBLL _userBLL = NewsPublish.Factory.BLLFactory.CreatUserBLL();  
    16.         protected void Page_Load(object sender, EventArgs e)  
    17.         {  
    18.               
    19.         }  
    20.   
    21.   
    22.         /// <summary>  
    23.         /// 提交注册按钮  
    24.         /// </summary>  
    25.         /// <param name="sender"></param>  
    26.         /// <param name="e"></param>  
    27.         protected void BtnSubmit_Click(object sender, EventArgs e)  
    28.         {  
    29.   
    30.             string userName = this.TbxUserName.Text.Trim().ToString();  
    31.             string pwd = this.TbxPsw.Text.Trim().ToString();  
    32.             string ConPwd = this.TbxConfPsw.Text.Trim().ToString();  
    33.   
    34.             //实在悲剧,一开始注册用的是验证性控件,后来测试的时候不知道为什么会和Register函数起冲突,  
    35.             //即,Register函数呗忽视而不会执行..只好改为人工码农丑陋代码进行判断了.-_-  
    36.             if(userName.Length==0)  
    37.             {  
    38.                   Response.Write("<script language=javascript>alert('用户名不能为空!');</script>");                
    39.             }  
    40.             else if(pwd.Length==0||ConPwd.Length==0)  
    41.             {  
    42.                   Response.Write("<script language=javascript>alert('密码不能为空!');</script>");     
    43.             }  
    44.             else if(pwd.Equals(ConPwd)==false)  
    45.             {  
    46.                  Response.Write("<script language=javascript>alert('密码不相等,请重新输入!');</script>");   
    47.             }  
    48.             else   
    49.             {  
    50.                 int tmp = _userBLL.Register(userName, pwd);  //tmp=1,2,3  分别为:用户名已存在/注册成功/注册失败  
    51.                 if (tmp == 2)  
    52.                 {  
    53.                     Response.Write("<script language=javascript>alert('注册成功');</script>");  
    54.                     Response.Write("<script>window.location='login.aspx'</script>");  
    55.                 }  
    56.                 else if (tmp == 1)  
    57.                 {  
    58.                     Response.Write("<script language=javascript>alert('用户名已经存在!');</script>");  
    59.                 }  
    60.                 else if (tmp == 3)  
    61.                 {  
    62.                     Response.Write("<script language=javascript>alert('注册出错,请检查输入的信息是否符合要求');</script>");  
    63.                     //Response.Write("<script>window.location='register.aspx'</script>");  
    64.                 }  
    65.             }  
    66.               
    67.         }  
    68.     }  
    69. }  


    界面层之personnel(个人中心)

     

    界面层之usernewscontent(登录后查看具体新闻)

            
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.UI;  
    6. using System.Web.UI.WebControls;  
    7. using NewsPublish.IBLL;  
    8. using NewsPublish.Factory;  
    9. using System.Data;  
    10. using NewsPublish.Model;  
    11.   
    12. namespace 新闻发布系统  
    13. {  
    14.     public partial class newscontent_user : System.Web.UI.Page  
    15.     {  
    16.   
    17.         //依赖于下一层的接口  
    18.         INewsBLL _ibll = BLLFactory.CreateNewsBLL();  
    19.         IFileServiceBLL _ifile = BLLFactory.CreateFileServiceBLL();  
    20.   
    21.   
    22.         protected void Page_Load(object sender, EventArgs e)  
    23.         {  
    24.             //另一种接受客户端参数的形式  
    25.             ViewState["NewsID"] = Request.QueryString[0];  
    26.   
    27.             int id = Convert.ToInt32(ViewState["NewsID"]);  
    28.   
    29.             //根据NewsID找到具体的新闻内容返回  
    30.             NewsModel newsModel = new NewsModel();  
    31.             newsModel = _ibll.GetDetailNewsByNewsID(id);  
    32.             TxtTitle.Text = newsModel.Title;  
    33.             TxtDate.Text = newsModel.Date.ToString();  
    34.             TxtUserName.Text = newsModel.UserName;  
    35.             TxtContent.Text = newsModel.Content;  
    36.             /*  
    37.             TxtTitle.Text = _ibll.GetDetailNewsByNewsID(id).Title;  
    38.             TxtDate.Text = _ibll.GetDetailNewsByNewsID(id).Date.ToString();  
    39.             TxtUserName.Text = _ibll.GetDetailNewsByNewsID(id).UserName;  
    40.             TxtContent.Text = _ibll.GetDetailNewsByNewsID(id).Content;  
    41.             */  
    42.   
    43.   
    44.             //绑定下载数据源文件标题  
    45.             int fileID = _ifile.GetFileIDByNewsID(id);  
    46.             if (fileID != 0)//如果某一条新闻没有fileID的话,就要做相应的措施  
    47.             {  
    48.                 DataTable dt = new DataTable();  
    49.                 dt = _ifile.GetHadUpLoadFileByFileID(fileID);  
    50.                 LabelDisPlayFileTitle.Text = dt.Rows[0]["FileTitle"].ToString();  
    51.             }  
    52.             else  
    53.             {  
    54.                 LabelDisPlayFileTitle.Text = "没有下载文件";  
    55.                 BtnDownLoadFile.Visible = false;  
    56.             }  
    57.         }  
    58.   
    59.   
    60.   
    61.         /// <summary>  
    62.         /// 下载附件  
    63.         /// </summary>  
    64.         /// <param name="sender"></param>  
    65.         /// <param name="e"></param>  
    66.         protected void BtnDownLoadFile_Click(object sender, EventArgs e)  
    67.         {  
    68.           
    69.             ViewState["NewsID"] = Request.QueryString[0];  
    70.   
    71.             int id = Convert.ToInt32(ViewState["NewsID"]);  
    72.   
    73.             int fileID = _ifile.GetFileIDByNewsID(id);  
    74.   
    75.             //拿到要下载的文件    
    76.             DataTable dt = new DataTable();  
    77.             dt = _ifile.GetHadUpLoadFileByFileID(fileID);  
    78.   
    79.   
    80.             //其实只要拿到Data Table之类的数据表即可,接下来的一堆Response都是下载,我也看不大懂.-_-  
    81.             Response.Buffer = true;  
    82.             Response.Clear();  
    83.   
    84.             //注意了:字节流(字节数组)成功的转为string类型  
    85.             Response.ContentType = dt.Rows[0]["FileType"].ToString();  
    86.             Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(dt.Rows[0]["FileTitle"].ToString()));  
    87.             Response.BinaryWrite((Byte[])dt.Rows[0]["FileContent"]);  
    88.             Response.Flush();  
    89.             Response.End();  
    90.          }  
    91.   
    92.     }  
    93. }  

    界面层之userhomepage(登录后首页)

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.UI;  
    6. using System.Web.UI.WebControls;  
    7. using NewsPublish.Factory;  
    8. using NewsPublish.IBLL;  
    9. using NewsPublish.Model;  
    10.   
    11. namespace 新闻发布系统  
    12. {  
    13.     public partial class userhomepage : System.Web.UI.Page  
    14.     {  
    15.         //依赖于下一层的接口  
    16.         INewsHaveSetContactBLL _iBLL;  
    17.         protected void Page_Load(object sender, EventArgs e)  
    18.         {  
    19.             //获取当前客户端的用户名  
    20.             string contactName = Session["userName"].ToString();  
    21.             _iBLL = BLLFactory.CreateNewsHaveSetContactBLL();  
    22.   
    23.             //获取我发布的+游客能看得+@我的  这些新闻  
    24.             List<NewsHaveSetContactModel> list = _iBLL.GetNewsIDOfVisitorAndMe(contactName);  
    25.   
    26.             //DataGrid绑定数据  
    27.             GdvUsHoPage.DataSource = _iBLL.GetNewsListOfNewsHaveAddMeIntoContact(list);  
    28.             GdvUsHoPage.DataBind();  
    29.         }  
    30.     }  
    31. }  


    界面层之publish(发布新闻页面)

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.UI;  
    6. using System.Web.UI.WebControls;  
    7. using NewsPublish.IBLL;  
    8. using NewsPublish.Model;  
    9. using System.IO;  
    10.   
    11. namespace 新闻发布系统  
    12. {  
    13.     public partial class publish_new_ : System.Web.UI.Page  
    14.     {  
    15.   
    16.         //依赖于下一层的接口  
    17.         INewsBLL _newsBLL = NewsPublish.Factory.BLLFactory.CreateNewsBLL();  
    18.         IFileServiceBLL _fileServiceBLL = NewsPublish.Factory.BLLFactory.CreateFileServiceBLL();  
    19.         IContactBLL _contactBLL = NewsPublish.Factory.BLLFactory.CreateContactBLL();  
    20.         INewsHaveSetContactBLL _newsHaveSetContactBLL = NewsPublish.Factory.BLLFactory.CreateNewsHaveSetContactBLL();  
    21.   
    22.         int fileID;//用于下方添加一条新闻的时候的绑定  
    23.         protected void Page_Load(object sender, EventArgs e)  
    24.         {  
    25.             string publisher = Session["userName"].ToString();  
    26.             LabelUserName.Text = publisher;  
    27.   
    28.             //新闻接收人控件绑定数据,首先得取出联系人列表  
    29.             List<ContactModel> tmp = new List<ContactModel>();  
    30.             tmp = _contactBLL.GetMyContactByUserName(publisher);  
    31.             if (tmp != null && tmp.Count > 0)  
    32.             {  
    33.                 LabelDisplayMyContacts2.Text = "您的联系人:";  //因为之前那个页面也有这个控件,所以用2来区分识别  
    34.                 this.BulletedListNewsContactChoose.DataSource = _contactBLL.GetMyContactByUserName(publisher);  
    35.                 this.BulletedListNewsContactChoose.DataTextField = "ContactName";  
    36.                 //this.BulletedList.DataValueField = "FileID";  
    37.                 this.BulletedListNewsContactChoose.DataBind();  
    38.             }  
    39.             else  
    40.             {  
    41.                 LabelDisplayMyContacts2.Text = "您现在还没有联系人";  
    42.             }  
    43.   
    44.         }  
    45.   
    46.   
    47.   
    48.   
    49.         /// <summary>  
    50.         /// 其实在这里我是硬性规定上传附件和新闻一定是绑定在一个按钮一起提交的,  
    51.         /// 这个举措是为了不让用户随便上传附件,这个会给数据库带来一定的负担  
    52.         /// 但我这个是牺牲在对上传附件的是否成功验证的前提下取得的...  
    53.         /// </summary>  
    54.         /// <param name="sender"></param>  
    55.         /// <param name="e"></param>  
    56.         protected void BulletedListNewsContactChoose_Click(object sender, BulletedListEventArgs e)  
    57.         {  
    58.             //每一个名字取出来了  
    59.             TextBoxDisplayChoise.Focus();//文本框获取焦点  
    60.   
    61.             //这个和BulletedList的功能有紧密关系了,获取点击事件,然后根据此获得其选项内容  
    62.             string contactName = ((BulletedList)sender).Items[e.Index].Value.ToString();  
    63.   
    64.             //把添加的新闻联系人逐个显示在textbox控件上面,就像是EMAIL一样,只不过我的控件界面其丑无比,  
    65.             //不过话说回来,基本上没怎么碰过界面的(当然也包括本人过于菜,所有控件都还木有熟悉)  
    66.             TextBoxDisplayChoise.Text += contactName;  
    67.             TextBoxDisplayChoise.Text += ",";  
    68.              
    69.         }  
    70.   
    71.   
    72.   
    73.   
    74.         /// <summary>  
    75.         /// 提交附件+发布新闻按钮  
    76.         /// </summary>  
    77.         /// <param name="sender"></param>  
    78.         /// <param name="e"></param>  
    79.         protected void BtnPublish_Click(object sender, EventArgs e)  
    80.         {  
    81.               
    82.             string title = Txt_Title.Text.ToString();  
    83.             string publisher = Session["userName"].ToString();  
    84.             string content = Txt_Content.Text.ToString();  
    85.   
    86.             //注意用trim方法去掉左右空格  
    87.             if (title.Trim().Length == 0 || content.Trim().Length == 0)  
    88.             {  
    89.                 Response.Write("<script language=javascript>alert('标题或者内容不能为空!请重新输入!');</script>");  
    90.             }  
    91.             else  
    92.             {  
    93.                 //---//  
    94.                 //获取上传的文件所有文件   
    95.   
    96.                 HttpFileCollection fileList = Request.Files;  
    97.                 for (int i = 0; i < fileList.Count; i++)//其实这个count只是1个.哈哈.我测试过了,只不过我把模板拷贝过来的时候懒得修改了  
    98.                 {  
    99.                     HttpPostedFile postedFile = fileList[i];  
    100.   
    101.                     //文件名    
    102.                     string fileName = postedFile.FileName;  
    103.                     //文件类型    
    104.                     string fileType = postedFile.ContentType;  
    105.                       
    106.                       
    107.                     //文件转换为二进制流    
    108.                     int fileLength = postedFile.ContentLength;  
    109.                     byte[] fileContent = new byte[fileLength];  
    110.                     Stream fileStream = postedFile.InputStream;  
    111.                     fileStream.Read(fileContent, 0, fileLength);  
    112.                     //存储文件    
    113.                     //之前写的文件服务类    
    114.                     if (fileName.Trim().Length == 0)  
    115.                         fileID = 0;  
    116.                     else  
    117.                     {  
    118.                         fileID = _fileServiceBLL.UpLoadFile(fileName, fileContent, fileType);//返回来的是FileID  
    119.                         if (fileID != 0)  
    120.                         {  
    121.                             Response.Write("<script language=javascript>alert('上传附件成功!');</script>");  
    122.                         }  
    123.                         else  
    124.                         {  
    125.                             Response.Write("<script language=javascript>alert('上传附件失败!请重新上传');</script>");  
    126.                         }  
    127.                     }  
    128.   
    129.   
    130.                 }  
    131.                 //---//  
    132.   
    133.                 DateTime date = DateTime.Now;  
    134.                 int tmp = _newsBLL.PublishNews(title, date, publisher, content, fileID);//fileID是会为0的  
    135.                 if (tmp != 0)  //返回来的是NewsID  
    136.                 {  
    137.   
    138.                     //我把联系人当成字符串进行保存了,如果为空的话是会自动默认面向游客开放的新闻,即所有人  
    139.                     string contactNameSet = TextBoxDisplayChoise.Text.Trim();  
    140.                     if (contactNameSet.Length == 0)  
    141.                     {  
    142.                         contactNameSet = "游客";  
    143.                     }  
    144.                     _newsHaveSetContactBLL.AddNewsReceiver(tmp, contactNameSet);  
    145.                     Response.Write("<script language=javascript>alert('发布新闻成功!');</script>");  
    146.   
    147.                 }  
    148.                 else  
    149.                 {  
    150.                     Response.Write("<script language=javascript>alert('发布新闻失败!');</script>");  
    151.                 }  
    152.                 Session["userName"] = publisher;  
    153.                 Response.Write("<script> window.location='personnel.aspx'</script>");  
    154.             }  
    155.         }  
    156.     }  
    157. }  


    界面层之personnelContacts(联系人页面)

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.UI;  
    6. using System.Web.UI.WebControls;  
    7. using NewsPublish.IBLL;  
    8. using NewsPublish.Model;  
    9.   
    10. namespace 新闻发布系统  
    11. {  
    12.     public partial class personnelContacts : System.Web.UI.Page  
    13.     {  
    14.         //依赖于下一层的接口  
    15.         IContactBLL _contactBLL = NewsPublish.Factory.BLLFactory.CreateContactBLL();  
    16.         protected void Page_Load(object sender, EventArgs e)  
    17.         {  
    18.   
    19.             //获取当前客户端的用户名  
    20.             string userName = Session["userName"].ToString();  
    21.             List<ContactModel> tmp = new List<ContactModel>();  
    22.   
    23.             //获取我的联系人  
    24.             tmp = _contactBLL.GetMyContactByUserName(userName);  
    25.             if (tmp != null && tmp.Count > 0)  
    26.             {  
    27.                 LabelDisplayMyContacts.Text = "您的联系人:";  
    28.                 this.BulletedList.DataSource = _contactBLL.GetMyContactByUserName(userName);  
    29.   
    30.                 //BulletedList绑定数据,这个绑定数据类似下拉框..  
    31.                 this.BulletedList.DataTextField = "ContactName";//其实就是给选项框选中一个数据库字段  
    32.                 //this.BulletedList.DataValueField = "FileID";//这个就是选项的序号,如果没有给出,则会以字段的值作为序号  
    33.                 this.BulletedList.DataBind();   
    34.             }  
    35.             else  
    36.             {  
    37.                 LabelDisplayMyContacts.Text = "您现在还没有联系人";  
    38.             }  
    39.               
    40.               
    41.         }  
    42.     }  
    43. }  


    界面层之email-news(@我新闻页面)

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.UI;  
    6. using System.Web.UI.WebControls;  
    7. using NewsPublish.IBLL;  
    8. using NewsPublish.Factory;  
    9. using NewsPublish.Model;  
    10.   
    11. namespace 新闻发布系统  
    12. {  
    13.     public partial class WebForm5 : System.Web.UI.Page  
    14.     {  
    15.         //依赖于下一层的接口  
    16.         INewsHaveSetContactBLL _iBLL;  
    17.         protected void Page_Load(object sender, EventArgs e)  
    18.         {  
    19.             //获取当前客户端的用户名  
    20.             string contactName = Session["userName"].ToString();  
    21.             _iBLL = BLLFactory.CreateNewsHaveSetContactBLL();  
    22.   
    23.             //把登陆后"我"作为联系人contactName进行传参,返回的是@我的新闻,在这里独立抽取出来了  
    24.             List<NewsHaveSetContactModel> list = _iBLL.GetNewsIDOfNewsHaveAddMeIntoContact(contactName);  
    25.               
    26.             //DataGrid绑定数据  
    27.             GdvEmNews.DataSource = _iBLL.GetNewsListOfNewsHaveAddMeIntoContact(list);  
    28.             GdvEmNews.DataBind();  
    29.         }  
    30.     }  
    31. }  


    界面层之Web.sitemap(导航截图,在母版页中存在)

    1. <?xml version="1.0" encoding="utf-8" ?>  
    2. <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >  
    3.   <siteMapNode title="主页" description="Home" url="~/userhomepage.aspx" >  
    4.     <siteMapNode title="个人中心" description="Services we offer"  
    5.       url="~/personnel.aspx">  
    6.       <siteMapNode title="发布新闻" description="Training classes"  
    7.         url="~/publish.aspx" />  
    8.       <siteMapNode title="添加联系人" description="Consulting services"  
    9.         url="~/Addcontact.aspx" />  
    10.       <siteMapNode title="@我新闻" description="Consulting services"  
    11.         url="~/email-news.aspx" />  
    12.     </siteMapNode>  
    13.   </siteMapNode>  
    14. </siteMap>  

    尾记

     小小总结,just do it........
  • 相关阅读:
    javamail.providers not found
    vue.js实现购物车功能2.0版本
    vue.js实现购物车功能
    iframe高度自适应
    C语言 自定义字符串拼接函数
    php安装
    Apache安装
    python:爬虫初体验
    c:forEach 显示下拉框并回显
    数据结构 --- 线性表学习(php模拟)
  • 原文地址:https://www.cnblogs.com/xieon1986/p/3441855.html
Copyright © 2011-2022 走看看