2012年度课程设计---新闻公布系统(小结)
-----Presented By muximuxi@Achilles
Tips:因本课程设计大部分代码皆有本人短时间仓促码成,界面恶心,代码丑陋.唯一长处便是: 所有代码都已贴上,而且所有都已凝视.另外与Asp.net教程结合恰当,通俗易懂,easy上手.
需求
新闻公布系统需求III NewsPublish(简称NP) 功能说明 本项目用于对新闻公布进行管理。 1、查看新闻 全部新闻按时间按降序排列; 用户登录后在自己主页能够查看自己当前所公布的全部新闻,在系统首页能够查看系统中全部的新闻; 游客能够查看当前系统所公布的全部新闻。 2、公布新闻 用户登录后,通过填写表单,加入附件或者不加入附件,指定接收人进行新闻公布; 接收人能够为联系人中的某几个人或全部人,当中全部人包含游客。表单见表一。
表一:表单 标题 xxxxxxx 接收人 XXX 公布人 XXX 加入附件 xxx 正文 xxxxxxx
简单需求分析
简单分析: 名词:新闻,用户,主页,表单,附件,接收人,联系人,游客. 抽取名词建立实体类:新闻类(News),用户类(User),附件类(FileService),联系人类(Contact),加入了联系人的新闻类(NewsHaveSetContact)(为了不改变原来的代码,这个类建立应该是非常丑陋恶心的,这应该用到设计模式的,这里主要为了展示三层架构就不从设计模式展开,-)_(-)
底层之数据库
项目目录部分截图
三层架构代码
Model层
Model层之NewsModel
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace NewsPublish.Model{ ////// 封装News的字段和属性 /// public class NewsModel { #region NewsModel构造函数 ////// NewsModel构造函数 /// /// 新闻标题 /// 新闻公布日期 /// 新闻内容 /// 新闻ID(key) /// 新闻所含的上传附件ID(or not) public NewsModel(string title, DateTime date, string content, int newsID,int fileID) { this._title = title; this._date = date; this._content = content; this._newsID = newsID; this._fileID = fileID; } ////// 事实上建立这个构造函数是为了弥补这个缺陷的:没有绑定username; /// 只是话说是每个model仅仅是含有这个例子的 /// public NewsModel() { } #endregion #region 设置新闻类的属性 //News标题 private string _title; //News公布时间 private DateTime _date; //公布人 private string _userName; //News内容 private string _content; //与主键相相应的字段 private int _newsID; private int _fileID; #endregion #region 设置成员属性訪问器 public string Title { set { _title = value; } get { return _title; } } public DateTime Date { set { _date = value; } get { return _date; } } public string UserName { set { _userName = value; } get { return _userName; } } public string Content { set { _content = value; } get { return _content; } } public int NewsID { set { _newsID = value; } get { return _newsID; } } public int FileID { set { _fileID = value; } get { return _fileID; } } #endregion }}
Model层之UserModel层
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace NewsPublish.Model{ ////// 封装User的字段和属性 /// public class UserModel { #region UserModel构造函数 ////// UserModel构造函数 /// /// 用户ID /// username /// 用户密码 public UserModel(string userID, string userName, string password) { this._userID = userID; this._userName = userName; this._password = password; } public UserModel() { } #endregion #region 定义用户类的属性(注意是private) private string _userID; private string _userName; private string _password; #endregion #region 设置成员属性訪问器 public string UserID { set { _userID = value; } get { return _userID; } } public string Password { set { _password = value; } get { return _password; } } public string UserName { set { _userName = value; } get { return _userName; } } #endregion }}
Model层之FileServiceModel
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace NewsPublish.Model{ public class FileServiceModel { #region NewsModel构造函数 ////// NewsModel构造函数 /// /// 该新闻上传的附件的ID /// 附件标题 /// 附件内容 /// 附件格式,这是和附件以二进制流的形式存在数据库的内容紧密相关的,不懂度娘去(附件上传数据库) public FileServiceModel(int fileID,string fileTitle,byte[] fileContent ,string fileType ) { this._fileID = fileID; this._fileTitle = fileTitle; this._fileContent = fileContent; this._fileType = fileType; } public FileServiceModel() { } #endregion #region 设置新闻类的属性 private int _fileID; private string _fileTitle; private byte [] _fileContent; private string _fileType; #endregion #region 设置成员属性訪问器 public int FileID { set { _fileID = value; } get { return _fileID; } } public string FileTitle { set { _fileTitle = value; } get { return _fileTitle; } } public byte[] FileContent { set { _fileContent = value; } get { return _fileContent; } } public string FileType { set { _fileType = value; } get { return _fileType; } } #endregion }}
Model层之ContactModel
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace NewsPublish.Model{ public class ContactModel { #region ContactModel构造函数 ////// ContactModel的构造函数 /// /// 联系人 /// 用户 public ContactModel(string contactName,string userName) { this._contactName = contactName; this._userName = userName; } public ContactModel() { } #endregion #region 设置新闻类的属性 //用户的联系人 private string _contactName = null; //username private string _userName = null; #endregion #region 设置成员属性訪问器 public string ContactName { set { _contactName = value; } get { return _contactName; } } public string UserName { set { _userName = value; } get { return _userName; } } #endregion }}
Model层之NewsHaveSetContactModel
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace NewsPublish.Model{ public class NewsHaveSetContactModel { #region NewsHaveSetContactModel构造函数 public NewsHaveSetContactModel(string contactName, int newsID) { this._contactName = contactName; this._newsID = newsID; } public NewsHaveSetContactModel() { } #endregion #region 设置已加入联系人的新闻类成员变量 private string _contactName = "VISITOR";//!!!! private int _newsID ; #endregion #region 设置成员变量訪问器 public string ContactName { set { _contactName = value; } get { return _contactName; } } public int NewsID { set { _newsID = value; } get { return _newsID; } } #endregion }}
IDAL层
IDAL层之INewsDAL
using System;using System.Collections.Generic;using System.Linq;using System.Web;using NewsPublish.Model;namespace NewsPublish.IDAL{ public interface INewsDAL { //建立接口,是为了上一层依赖于这个一层的抽象而不是实现(也就是不必知道详细的方法怎么运行) //仅仅须要知道传什么參数返回什么东西就可以..接口无限强大,什么函数都调用(C#语法书没看,摸黑过来,百度后猜測应该是这样) ////// 得到全部新闻列表,主要用于主页显示 /// ///全部新闻列表 ListGetAllNewsList(); /// /// 依据username,得到用户已公布的新闻列表 /// /// username ///用户已公布的新闻列表 ListGetUserNewsListByUserName(string userName); /// /// 公布新闻 /// /// 新闻标题 /// 公布新闻系统时间 /// username(新闻,和username都有这么一个属性) /// 新闻内容 /// 新闻附件ID ///返回插入的新闻ID,这对兴许处理非常有帮助,good int PublishNews(string title, DateTime date, string userName, string content,int fileID); ////// 由新闻Id得到详细新闻内容 /// /// 新闻ID ///详细新闻 NewsModel GetDetailNewsByNewsID(int newsID); }}
IDAL层之IUserDAL
using System;using System.Collections.Generic;using System.Linq;using System.Web;using NewsPublish.Model;namespace NewsPublish.IDAL{ public interface IUserDAL { ////// 登录 /// /// username /// 密码 ///登录是否成功 bool Login(string userName, string password); ////// 注冊 /// /// username /// 密码 ///注冊是否成功,仅仅只是用int类型更好的变化://flag=1,2,3 分别为:username已存在/注冊成功/注冊失败 int Register(string userName, string password); }}
IDAL层之IFileServiceDAL
using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Web;namespace NewsPublish.IDAL{ public interface IFileServiceDAL { ////// 上传附件 /// /// 附件标题 /// 附件内容 /// 附件格式 ///返回附件插入数据库的即时ID int UpLoadFile(string fileTitle, byte[] fileContent, string fileType); ////// 通过fileID下载新闻附件 /// /// 附件ID ///通过DataTable类型返回文件 DataTable GetHadUpLoadFileByFileID(int fileID); ////// 通过newsID获取fileID; /// /// 新闻ID ///附件ID int GetFileIDByNewsID(int newsID); }}
IDAL层之IContactDAL
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using NewsPublish.Model;namespace NewsPublish.IDAL{ public interface IContactDAL { ////// 通过username获取联系人 /// /// username ///联系人列表 ListGetMyContactByUserName(string userName); /// /// 依据username和联系人加入联系人 /// /// username ///加入是否成功 bool AddContact(string userName, string newContact); }}
IDAL层之INewsHaveSetContactDAL
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using NewsPublish.Model;namespace NewsPublish.IDAL{ public interface INewsHaveSetContactDAL { ////// 得到将我增加联系人的新闻的新闻ID列表--------未登录主页+个人中心 /// ///将我增加联系人的新闻的新闻ID列表 ListGetNewsIDOfNewsHaveAddMeIntoContact(string contactName); /// /// 依据对应逻辑获取到的新闻ID列表(依据联系人来分) 然后得到对应逻辑的新闻列表 /// ///将我增加联系人的新闻列表 ListGetNewsListOfNewsHaveAddMeIntoContact(List newsIDList); /// /// 得到将游客和我作为接收人的新闻的新闻ID列表---------登录主页 /// 事实上一開始我还忘了我本人公布的新闻也放在登录主页呢..囧.. /// /// 作为联系人的名字---"我" ///得到将游客,我作为接收人,以及我公布的新闻的ID列表 ListGetNewsIDOfVisitorAndMe(string contactName); /// /// 从联系人增加新闻接收人到详细新闻 /// /// 新闻ID /// 联系人名字 ///增加是否成功 bool AddNewsReceiver(int newsID, string contactName); }}
DAL层
DAL层之SQLHelper(这个是作为数据库操作写的辅助类,对降低冗余代码起了不少作用,只是是从老师课题笔记那摘过来的,网上也有一大堆下载)
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data;using System.Data.SqlClient;using System.Configuration;namespace NewsPublish.DAL{ public class SQLHelper { string connectionString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString; ////// 构造函数 /// public SQLHelper() { } ////// ExecuteNonQuery操作,对数据库进行 增、删、改 操作((1) /// /// 要运行的SQL语句 ///public int ExecuteNonQuery(string sql) { return ExecuteNonQuery(sql, CommandType.Text, null); } /// /// ExecuteNonQuery操作,对数据库进行 增、删、改 操作(2) /// /// 要运行的SQL语句 /// 要运行的查询类型(存储过程、SQL文本) ///public int ExecuteNonQuery(string sql, CommandType commandType) { return ExecuteNonQuery(sql, commandType, null); } /// /// ExecuteNonQuery操作,对数据库进行 增、删、改 操作(3) /// /// 要运行的SQL语句 /// 要运行的查询类型(存储过程、SQL文本) /// 參数数组 ///public int ExecuteNonQuery(string sql, CommandType commandType, SqlParameter[] parameters) { //影响的记录数量 int count = 0; //创建连接 using (SqlConnection connection = new SqlConnection(connectionString)) { //创建命令对象 using (SqlCommand command = new SqlCommand(sql, connection)) { //设置命令类型 command.CommandType = commandType; //是否有參数 if (parameters != null) { //向命令对象加入參数 foreach (SqlParameter parameter in parameters) { command.Parameters.Add(parameter); } } //打开连接 connection.Open(); //返回操作影响的记录数量 count = command.ExecuteNonQuery(); } } return count; } /// /// SqlDataAdapter的Fill方法运行一个查询,并返回一个DataSet类型结果(1) /// /// 要运行的SQL语句 ///public DataSet ExecuteDataSet(string sql) { return ExecuteDataSet(sql, CommandType.Text, null); } /// /// SqlDataAdapter的Fill方法运行一个查询,并返回一个DataSet类型结果(2) /// /// 要运行的SQL语句 /// 要运行的查询类型(存储过程、SQL文本) ///public DataSet ExecuteDataSet(string sql, CommandType commandType) { return ExecuteDataSet(sql, commandType, null); } /// /// SqlDataAdapter的Fill方法运行一个查询,并返回一个DataSet类型结果(3) /// /// 要运行的SQL语句 /// 要运行的查询类型(存储过程、SQL文本) /// 參数数组 ///public DataSet ExecuteDataSet(string sql, CommandType commandType, SqlParameter[] parameters) { DataSet ds = new DataSet(); using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand command = new SqlCommand(sql, connection)) { command.CommandType = commandType; if (parameters != null) { foreach (SqlParameter parameter in parameters) { command.Parameters.Add(parameter); } } SqlDataAdapter adapter = new SqlDataAdapter(command); adapter.Fill(ds); } } return ds; } /// /// SqlDataAdapter的Fill方法运行一个查询,并返回一个DataTable类型结果(1) /// /// 要运行的SQL语句 ///public DataTable ExecuteDataTable(string sql) { return ExecuteDataTable(sql, CommandType.Text, null); } /// /// SqlDataAdapter的Fill方法运行一个查询,并返回一个DataTable类型结果(2) /// /// 要运行的SQL语句 /// 要运行的查询类型(存储过程、SQL文本) ///public DataTable ExecuteDataTable(string sql, CommandType commandType) { return ExecuteDataTable(sql, commandType, null); } /// /// SqlDataAdapter的Fill方法运行一个查询,并返回一个DataTable类型结果(3) /// /// 要运行的SQL语句 /// 要运行的查询类型(存储过程、SQL文本) /// 參数数组 ///public DataTable ExecuteDataTable(string sql, CommandType commandType, SqlParameter[] parameters) { DataTable data = new DataTable(); using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand command = new SqlCommand(sql, connection)) { command.CommandType = commandType; if (parameters != null) { foreach (SqlParameter parameter in parameters) { command.Parameters.Add(parameter); } } SqlDataAdapter adapter = new SqlDataAdapter(command); adapter.Fill(data); } } return data; } /// /// ExecuteReader运行一查询,返回一SqlDataReader对象实例(1) /// /// 要运行的SQL语句 ///public SqlDataReader ExecuteReader(string sql) { return ExecuteReader(sql, CommandType.Text, null); } /// /// ExecuteReader运行一查询,返回一SqlDataReader对象实例(2) /// /// 要运行的SQL语句 /// 要运行的查询类型(存储过程、SQL文本) ///public SqlDataReader ExecuteReader(string sql, CommandType commandType) { return ExecuteReader(sql, commandType, null); } /// /// ExecuteReader运行一查询,返回一SqlDataReader对象实例(3) /// /// 要运行的SQL语句 /// 要运行的查询类型(存储过程、SQL文本) /// 參数数组 ///public SqlDataReader ExecuteReader(string sql, CommandType commandType, SqlParameter[] parameters) { SqlConnection connection = new SqlConnection(connectionString); SqlCommand command = new SqlCommand(sql, connection); command.CommandType = commandType; if (parameters != null) { foreach (SqlParameter parameter in parameters) { command.Parameters.Add(parameter); } } connection.Open(); return command.ExecuteReader(CommandBehavior.CloseConnection); } /// /// ExecuteScalar运行一查询,返回查询结果的第一行第一列(1) /// /// 要运行的SQL语句 ///public Object ExecuteScalar(string sql) { return ExecuteScalar(sql, CommandType.Text, null); } /// /// ExecuteScalar运行一查询,返回查询结果的第一行第一列(2) /// /// 要运行的SQL语句 /// 要运行的查询类型(存储过程、SQL文本) ///public Object ExecuteScalar(string sql, CommandType commandType) { return ExecuteScalar(sql, commandType, null); } /// /// ExecuteScalar运行一查询,返回查询结果的第一行第一列(3) /// /// 要运行的SQL语句 /// 要运行的查询类型(存储过程、SQL文本) ///public Object ExecuteScalar(string sql, CommandType commandType, SqlParameter[] parameters) { object result = null; using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand command = new SqlCommand(sql, connection)) { command.CommandType = commandType; if (parameters != null) { foreach (SqlParameter parameter in parameters) { command.Parameters.Add(parameter); } } connection.Open(); result = command.ExecuteScalar(); } } return result; } /// /// 返回当前连接的数据库中全部由用户创建的数据库 /// ///public DataTable GetTables() { DataTable data = null; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); data = connection.GetSchema("Tables"); } return data; } } }
DAL层之NewsDAL
using System;using System.Collections.Generic;using System.Linq;using System.Web;using NewsPublish.IDAL;using NewsPublish.Model;using System.Data.SqlClient;using NewsPublish.DAL;using System.Data;namespace NewsPublish.DAL{ public class NewsDAL : INewsDAL { //这个原本想是利用微软的sqlhelper类来写的网上大量有得下载,只是既然老师的课堂代码刚好有就直接搞过来了 private SQLHelper sqlhelper = null; //构造函数 public NewsDAL() { sqlhelper = new SQLHelper(); } ////// 得到全部新闻列表,,主要用于主页显示(只是由于第二阶段需求之后,此函数已无用) /// ///全部新闻列表 public ListGetAllNewsList() { //创建新闻对象列表 List newsList = new List (); //选择dbo.Table_News中的新闻标题、作者、公布日期列 string sql = "select Title,UserName,Date,NewsID from dbo.Table_News order by Date desc"; //这个SQLHelper类中的ExecuteReader方法中会建立commond对象,返回的是DataReader对象 SqlDataReader res = sqlhelper.ExecuteReader(sql); //通过reader方法自增获取每一行的对应所要的列内容,然后加入到list中建立新闻对象.相似foreach while (res.Read()) { //创建新闻对象,无參数的构造函数的优点体如今这 NewsModel news = new NewsModel(); //给新闻对象属性赋值 //使用SqlDataReader的GetOrdinal()方法(这种方法我不求甚解,自己百度吧),获得列的序号,输入參数为列名 news.Title = res.GetString(res.GetOrdinal("Title")); news.UserName = res.GetString(res.GetOrdinal("UserName")); news.NewsID = res.GetInt32(res.GetOrdinal("NewsID")); news.Date = res.GetDateTime((res.GetOrdinal("Date"))); //将新闻加入到列表中,easy吧..-_- newsList.Add(news); } return newsList; } /// /// 依据username,得到用户已公布的新闻列表 /// /// username ///用户已公布的新闻列表 public ListGetUserNewsListByUserName(string userName) { //创建新闻对象列表 List userNewsList = new List (); //选择dbo.Table_News中该用户已公布新闻的标题、作者、公布日期 string sql = "select NewsID, Title,UserName,Date from dbo.Table_News where UserName=@UserName order by Date desc"; SqlParameter[] param = new SqlParameter[1]{ new SqlParameter("@UserName", userName) }; //含有參数的的传參方法,迷惑的话能够看一下Sqlhelper类(在DAL层中) SqlDataReader res = sqlhelper.ExecuteReader(sql, CommandType.Text, param); //获取新闻对象 while (res.Read()) { //创建新闻对象 NewsModel news = new NewsModel(); //给新闻对象属性赋值 //使用SqlDataReader的GetOrdinal()方法,获得列的序号,输入參数为列名 news.NewsID = res.GetInt32(res.GetOrdinal("NewsID")); news.Title = res.GetString(res.GetOrdinal("Title")); news.UserName = res.GetString(res.GetOrdinal("UserName")); news.Date = res.GetDateTime(res.GetOrdinal("Date")); //将新闻加入到列表中 userNewsList.Add(news); } return userNewsList; } /// /// 公布新闻 /// /// 新闻标题 /// 公布新闻系统时间 /// username(新闻,和username都有这么一个属性) /// 新闻内容 /// 新闻附件ID ///返回插入的新闻ID,这对兴许处理非常有帮助,good public int PublishNews(string title, DateTime Date, string userName, string content, int fileID) { //观察这条sql语句的结尾处是返回插入的即时ID的关键,不懂的话度娘吧.-_- string sql = "insert into dbo.Table_News(Title,UserName,Date,Content,FileID )values (@Title,@UserName,@Date,@Content,@FileID);Select @@IDENTITY"; //话说这种传參方式我真是不习惯.只是好在貌似非常有用.@Parameter充当的是中介的变量 SqlParameter[] param = new SqlParameter[5] { new SqlParameter("@Title", title), new SqlParameter("@UserName", userName), new SqlParameter("@Date",Date), new SqlParameter("@Content", content), new SqlParameter("@FileID",fileID) }; //调用数据库操作 int res = Convert.ToInt32(sqlhelper.ExecuteScalar(sql, CommandType.Text, param).ToString()); if (res != 0) { return res;//直接返回就是操作后的FileID(注意这种使用方法仅限于key值,由于ExecuteScalar仅仅能获取第一行第一列值) } else { return 0; } } ////// 依据新闻ID,得到具体新闻 /// /// 新闻ID ///具体新闻 public NewsModel GetDetailNewsByNewsID(int newsID) { NewsModel news = new NewsModel(); string sql = "Select Title ,Content,Date,UserName,NewsID from dbo.Table_News where NewsID=@NewsID"; SqlParameter[] param = new SqlParameter[1] { new SqlParameter("@NewsID", newsID) }; SqlDataReader res = sqlhelper.ExecuteReader(sql, CommandType.Text, param); while (res.Read()) { //给新闻对象属性赋值 //使用SqlDataReader的GetOrdinal()方法,获得列的序号,输入參数为列名 news.NewsID = res.GetInt32(res.GetOrdinal("NewsID")); news.Title = res.GetString(res.GetOrdinal("Title")); news.UserName = res.GetString(res.GetOrdinal("UserName")); news.Date = res.GetDateTime(res.GetOrdinal("Date")); news.Content = res.GetString(res.GetOrdinal("Content")); } return news; } }}
DAL层之UserDAL
using System;using System.Collections.Generic;using System.Linq;using System.Web;using NewsPublish.IDAL;using System.Data.SqlClient;using NewsPublish.Model;using System.Data;namespace NewsPublish.DAL{ public class UserDAL : IUserDAL { private SQLHelper sqlhelper = null; //构造函数 public UserDAL() { sqlhelper = new SQLHelper(); } ////// 登录 /// /// username /// 密码 ///登录是否成功 public bool Login(string userName, string password) { string sql = "Select UserName,Password from dbo.Table_User where UserName=@UserName and Password=@Password"; //创建參数数组 SqlParameter[] param = new SqlParameter[2] { new SqlParameter("@UserName", userName), new SqlParameter("@Password", password) }; string i = Convert.ToString(sqlhelper.ExecuteScalar(sql, CommandType.Text, param)); if (string.IsNullOrEmpty(i)) { return false; } else { return true; } } ////// 注冊 /// /// username /// 密码 ///注冊是否成功,仅仅只是用int类型更好的变化 public int Register(string userName, string password) { int flag; //flag=1,2,3 分别为:username已存在/注冊成功/注冊失败 string sql = "select COUNT(*)from dbo.Table_User where UserName=@UserName"; //创建參数数组 SqlParameter[] param = new SqlParameter[1] { new SqlParameter("@UserName", userName) }; int i = Convert.ToInt32(sqlhelper.ExecuteScalar(sql, CommandType.Text, param)); if (i == 0) { string sql1 = "insert into dbo.Table_User(UserName,Password) values (@UserName,@Password)"; //创建參数数组 SqlParameter[] param1 = new SqlParameter[2] { new SqlParameter("@UserName", userName), new SqlParameter("@Password", password) }; int res = sqlhelper.ExecuteNonQuery(sql1, CommandType.Text, param1); if (res != 0) { flag= 2; } else { flag= 3; } } else { flag=1; } return flag; } }}
DAL层之FileServiceDAL
using System;using System.Collections.Generic;using System.Data;using System.Data.SqlClient;using System.Linq;using System.Web;using NewsPublish.IDAL;using NewsPublish.Model;namespace NewsPublish.DAL{ public class FileServiceDAL : IFileServiceDAL { private SQLHelper sqlhelper = null; int tmpFileID;//方便以下的获取文件ID public FileServiceDAL() { sqlhelper = new SQLHelper(); } ////// 上传附件 /// /// 附件标题 /// 附件内容 /// 附件格式 ///返回附件插入数据库的即时ID public int UpLoadFile(string fileTitle, byte[] fileContent, string fileType) { //返回ID的sql语句,这个看尾部...Select @@IDENTITY string sql = "insert into Table_UploadFile(FileTitle,FileContent, FileType)values (@FileTitle,@FileContent,@FileType);Select @@IDENTITY"; //给sql语句加入參数 SqlParameter[] param = new SqlParameter[3] { new SqlParameter("@FileTitle", fileTitle), new SqlParameter("@FileContent", fileContent), new SqlParameter("@FileType",fileType) }; //调用数据库操作 //与Select @@IDENTITY配套的ExecuteScalar int res = Convert.ToInt32(sqlhelper.ExecuteScalar(sql, CommandType.Text, param).ToString()); if (res != 0) { return res;//直接返回就是操作后的FileID } else { return 0; } } ////// 通过fileID下载新闻附件 /// /// 附件ID ///通过DataTable类型返回文件 public DataTable GetHadUpLoadFileByFileID(int fileID) { FileServiceModel file = new FileServiceModel(); string sql = "Select FileTitle,FileContent,FileType from dbo.Table_UploadFile where FileID=@FileID"; SqlParameter[] param = new SqlParameter[1] { new SqlParameter("@FileID", fileID) }; //基本数据库操作 SqlDataReader res = sqlhelper.ExecuteReader(sql, CommandType.Text, param); DataTable db = new DataTable(); db.Load(res); return db; } ////// 通过newsID获取fileID; /// /// 新闻ID ///附件ID public int GetFileIDByNewsID(int newsID) { string sql = "Select FileID from dbo.Table_News where NewsID=@NewsID"; SqlParameter[] param = new SqlParameter[1]{ new SqlParameter("@NewsID", newsID) }; SqlDataReader res = sqlhelper.ExecuteReader(sql, CommandType.Text, param); while (res.Read()) { //res.GetInt32(res.GetOrdinal("FileID"));//原本是在这样,可是报错了 //由于FileID或许为空,所以就要先推断是否为空,由于这个GetOrdinal()非常矫情,受不了 if (!res.IsDBNull(res.GetOrdinal("FileID"))) tmpFileID = res.GetInt32(res.GetOrdinal("FileID")); } return tmpFileID; } }}
DAL层之ContactDAL
using System;using System.Collections.Generic;using System.Linq;using System.Web;using NewsPublish.IDAL;using System.Data.SqlClient;using NewsPublish.Model;using System.Data;using System.Text.RegularExpressions;namespace NewsPublish.DAL{ public class ContactDAL : IContactDAL { private SQLHelper sqlhelper = null; //构造函数 public ContactDAL() { sqlhelper = new SQLHelper(); } ////// 通过username获取联系人 /// /// username ///联系人列表 public ListGetMyContactByUserName(string userName) { string myContact = null; List contactList = new List (); //选择dbo.Table_Contact中该用户的联系人 string sql = "select ContactName from dbo.Table_Contact where UserName=@UserName "; SqlParameter[] param = new SqlParameter[1] { new SqlParameter("@UserName", userName) }; SqlDataReader res = sqlhelper.ExecuteReader(sql, CommandType.Text, param); while (res.Read()) { //myContact = res.GetString(res.GetOrdinal("ContactName")); //由于FileID或许为空,所以就要先推断是否为空 if (!res.IsDBNull(res.GetOrdinal("ContactName"))) { myContact = res.GetString(res.GetOrdinal("ContactName")); } } if (myContact != null) { //这个函数是依据逗号把字符串的联系人切割开来,放进列表中 String[] array = Regex.Split(myContact, ",", RegexOptions.IgnoreCase); for (int i = 0; i < array.Length; i++) { //c#的创建对象真是都人性化,这样子把对应的属性就赋值了,nice ContactModel contact = new ContactModel() { ContactName = array[i] }; contactList.Add(contact); } } return contactList; } /// /// 依据username和联系人加入联系人 /// /// username ///加入是否成功 public bool AddContact(string userName,string newContact) { //由于考虑到第一次由空联系人加入联系人会有些麻烦,所以每次加入的时候都推断一下联系人是否为空 //而且在前端要有提示:没输入一个联系人就要用逗号隔开 string contactName = string.Empty; //选择dbo.Table_Contact中该用户的联系人 string sql = "select ContactName from dbo.Table_Contact where UserName=@UserName "; SqlParameter[] param = new SqlParameter[1] { new SqlParameter("@UserName", userName) }; SqlDataReader res = sqlhelper.ExecuteReader(sql, CommandType.Text, param); while (res.Read()) { if (!res.IsDBNull(res.GetOrdinal("ContactName"))) contactName = res.GetString(res.GetOrdinal("ContactName")); } //注意了哟:假设一開始的时候,数据为空的话,就插入,否则则是更新..由于一開始没数据的话更新个屌阿-_- if (contactName.Length == 0) { contactName = newContact; sql = "insert into dbo.Table_Contact(UserName,ContactName) values (@UserName,@ContactName)"; SqlParameter[] parm1 = new SqlParameter[2] { new SqlParameter("@UserName",userName), new SqlParameter("@ContactName",contactName) }; int tmp1 = sqlhelper.ExecuteNonQuery(sql,CommandType.Text,parm1); if (tmp1 != 0) { return true; } else { return false; } } else { contactName = contactName + newContact; sql = "update dbo.Table_Contact set ContactName=@ContactName where UserName=@UserName "; //创建參数数组 SqlParameter[] param1 = new SqlParameter[2] { new SqlParameter("@UserName", userName), new SqlParameter("@ContactName", contactName) }; int tmp2 = sqlhelper.ExecuteNonQuery(sql, CommandType.Text, param1); if (tmp2 != 0) { return true; } else { return false; } } } }}
DAL层之NewsHaveSetContactDAL
using System;using System.Collections.Generic;using System.Linq;using System.Web;using NewsPublish.IDAL;using System.Data.SqlClient;using NewsPublish.Model;using System.Data;namespace NewsPublish.DAL{ public class NewsHaveSetContactDAL : INewsHaveSetContactDAL,IComparer{ private SQLHelper sqlhelper = null; public NewsHaveSetContactDAL() { sqlhelper = new SQLHelper(); } /// /// 哈哈,这个是查字典写的快排的Compare方法,下文中依据ID列表一条一条的去新闻的话,这样子是不会 /// 自己主动排序的嘛,所以自己就随便调用一下系统的快排队时间进行排序嘛. /// /// /// ///public int Compare(NewsModel x, NewsModel y) { if(x.Date>y.Date) return -1; if (x.Date < y.Date) return 1; return 0; } /// /// 得到将我加入联系人的新闻的新闻ID列表--------邮箱新闻或者未登录主页(传參"游客") /// ///将我加入联系人的新闻的新闻ID列表 public ListGetNewsIDOfNewsHaveAddMeIntoContact(string contactName) { //事实上这种方法是有漏洞的呢,由于like的不严谨.也是C语言的字符串匹配内容,待会再查查KMP吧,必须严谨才行 //创建新闻ID列表 List newsIDOfTheNewsHaveContactList = new List (); //选择dbo.Table_Contact中该用户的联系人 //注意标点符号写法. string sql = "select NewsID from dbo.Table_NewsHaveSetContact where ContactName like '%'+@ContactName+'%'"; //“Select * From table Where field like ’%‘+@field+’%‘”;//必须感谢 SqlParameter[] param = new SqlParameter[1] { new SqlParameter("@ContactName", contactName) }; SqlDataReader res = sqlhelper.ExecuteReader(sql, CommandType.Text, param); //获取新闻ID while (res.Read()) { NewsHaveSetContactModel newsList = new NewsHaveSetContactModel(); //给新闻ID变量赋值 //使用SqlDataReader的GetOrdinal()方法,获得列的序号,输入參数为列名 newsList.NewsID = res.GetInt32(res.GetOrdinal("NewsID")); newsIDOfTheNewsHaveContactList.Add(newsList); } return newsIDOfTheNewsHaveContactList; } /// /// 依据对应逻辑获取到的新闻ID列表(依据联系人来分) 然后得到对应逻辑的新闻列表 /// /// 对应逻辑获取到的新闻ID列表 ///对应逻辑的新闻列表 public ListGetNewsListOfNewsHaveAddMeIntoContact(List newsIDList) { //创建新闻对象列表 List newsList = new List (); for (int i = 0; i < newsIDList.Count; i++) { int newsID = newsIDList[i].NewsID; //选择dbo.Table_News中该用户已公布新闻的标题、作者、公布日期 string sql = "select top 10 NewsID, Title,UserName,Date from dbo.Table_News where NewsID=@NewsID order by Date desc"; SqlParameter[] param = new SqlParameter[1] { new SqlParameter("@NewsID", newsID) }; SqlDataReader res = sqlhelper.ExecuteReader(sql,CommandType.Text,param); //获取新闻对象 while (res.Read()) { //创建新闻对象 NewsModel news = new NewsModel(); //给新闻对象属性赋值 //使用SqlDataReader的GetOrdinal()方法,获得列的序号,输入參数为列名 news.NewsID = res.GetInt32(res.GetOrdinal("NewsID")); news.Title = res.GetString(res.GetOrdinal("Title")); news.UserName = res.GetString(res.GetOrdinal("UserName")); news.Date = res.GetDateTime(res.GetOrdinal("Date")); //将新闻加入到列表中 newsList.Add(news); } } //给获取到的新闻列表排序 NewsHaveSetContactDAL dc = new NewsHaveSetContactDAL(); newsList.Sort(dc); return newsList; } /// /// 得到将游客和我作为接收人的新闻的新闻ID列表---------登录主页 /// 事实上一開始我还忘了我本人公布的新闻也放在登录主页呢..囧.. /// /// 作为联系人的名字---"我" ///得到将游客,我作为接收人,以及我公布的新闻的ID列表 public ListGetNewsIDOfVisitorAndMe(string contactName) { //创建新闻ID列表 List newsIDOfTheNewsHaveContactList = new List (); //选择dbo.Table_Contact中该用户的联系人 //------------(1)选择本人作为新闻联系人的newsID string sql = "select NewsID from dbo.Table_NewsHaveSetContact where ContactName like '%'+@ContactName+'%'"; //“Select * From table Where field like ’%‘+@field+’%‘”;//必须感谢 SqlParameter[] param = new SqlParameter[1] { new SqlParameter("@ContactName", contactName) }; SqlDataReader res = sqlhelper.ExecuteReader(sql, CommandType.Text, param); //获取新闻ID while (res.Read()) { NewsHaveSetContactModel newsList = new NewsHaveSetContactModel(); //给新闻ID变量赋值 //使用SqlDataReader的GetOrdinal()方法,获得列的序号,输入參数为列名 newsList.NewsID = res.GetInt32(res.GetOrdinal("NewsID")); newsIDOfTheNewsHaveContactList.Add(newsList); } //-------------(2)获取面向游客新闻的newsID string sql1 = "select NewsID from dbo.Table_NewsHaveSetContact where ContactName like '游客'"; //“Select * From table Where field like ’%‘+@field+’%‘”;//必须感谢 SqlDataReader res1 = sqlhelper.ExecuteReader(sql1); //获取新闻ID while (res1.Read()) { NewsHaveSetContactModel newsList = new NewsHaveSetContactModel(); //给新闻ID变量赋值 //使用SqlDataReader的GetOrdinal()方法,获得列的序号,输入參数为列名 newsList.NewsID = res1.GetInt32(res1.GetOrdinal("NewsID")); newsIDOfTheNewsHaveContactList.Add(newsList); } //------------(3)获取本人公布的新闻 //选择dbo.Table_News中该用户已公布新闻的标题、作者、公布日期 string sql2 = "select NewsID from dbo.Table_News where UserName=@ContactName order by Date desc"; SqlParameter[] param2 = new SqlParameter[1]{ new SqlParameter("@ContactName", contactName) }; //含有參数的的传參方法,迷惑的话能够看一下Sqlhelper类(在DAL层中) SqlDataReader res2 = sqlhelper.ExecuteReader(sql2, CommandType.Text, param2); //获取新闻对象 while (res2.Read()) { NewsHaveSetContactModel newsList = new NewsHaveSetContactModel(); //给新闻ID变量赋值 //使用SqlDataReader的GetOrdinal()方法,获得列的序号,输入參数为列名 newsList.NewsID = res2.GetInt32(res1.GetOrdinal("NewsID")); //将新闻加入到列表中 newsIDOfTheNewsHaveContactList.Add(newsList); } //------------ return newsIDOfTheNewsHaveContactList; } /// /// 从联系人加入新闻接收人到详细新闻 /// /// 新闻ID /// 联系人名字 ///加入是否成功 public bool AddNewsReceiver(int newsID, string contactName) { string sql = "insert into dbo.Table_NewsHaveSetContact(NewsID,ContactName )values (@NewsID,@ContactName)"; SqlParameter[] param = new SqlParameter[2] { new SqlParameter("@NewsID", newsID), new SqlParameter("@ContactName", contactName) }; //调用数据库操作 int res = sqlhelper.ExecuteNonQuery(sql, CommandType.Text, param); if (res != 0) { return true; } else { return false; } } }}
IBLL层
IBLL层之INewsBLL
using System;using System.Collections.Generic;using System.Linq;using System.Web;using NewsPublish.Model;namespace NewsPublish.IBLL{ public interface INewsBLL { ////// 得到全部新闻列表,主要用于主页显示 /// ///全部新闻列表 ListGetAllNewsList(); /// /// 依据username,得到用户已公布的新闻列表 /// /// username ///用户已公布的新闻列表 ListGetUserNewsListByUserName(string userName); /// /// 公布新闻 /// /// 新闻标题 /// 公布新闻系统时间 /// username(新闻,和username都有这么一个属性) /// 新闻内容 /// 新闻附件ID ///返回插入的新闻ID,这对兴许处理非常有帮助,good int PublishNews(string title, DateTime date, string userName, string content, int fileID); ////// 依据新闻ID,得到具体新闻 /// /// 新闻ID ///具体新闻 NewsModel GetDetailNewsByNewsID(int newsID); }}
IBLL层之IUserBLL
using System;using System.Collections.Generic;using System.Linq;using System.Web;using NewsPublish.Model;namespace NewsPublish.IBLL{ public interface IUserBLL { ////// 登录 /// /// username /// 密码 ///登录是否成功 bool Login(string userName, string password); ////// 注冊 /// /// username /// 密码 ///注冊是否成功,仅仅只是用int类型更好的变化://flag=1,2,3 分别为:username已存在/注冊成功/注冊失败 int Register(string userName, string password); }}
IBLL层之IFileServiceBLL
using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Web;namespace NewsPublish.IBLL{ public interface IFileServiceBLL { ////// 上传附件 /// /// 附件标题 /// 附件内容 /// 附件格式 ///返回附件插入数据库的即时ID int UpLoadFile(string fileTitle, byte[] fileContent, string fileType); ////// 通过fileID下载新闻附件 /// /// 附件ID ///通过DataTable类型返回文件 DataTable GetHadUpLoadFileByFileID(int fileID); ////// 通过newsID获取fileID; /// /// 新闻ID ///附件ID int GetFileIDByNewsID(int newsID); }}
IBLL层之IContactBLL
using System;using System.Collections.Generic;using System.Linq;using System.Web;using NewsPublish.Model;namespace NewsPublish.IBLL{ public interface IContactBLL { ////// 通过username获取联系人 /// /// username ///联系人列表 ListGetMyContactByUserName(string userName); /// /// 依据username和联系人加入联系人 /// /// username ///加入是否成功 bool AddContact(string userName, string newContact); }}
IBLL层之INewsHaveSetContactBLL
using System;using System.Collections.Generic;using System.Linq;using System.Web;using NewsPublish.Model;namespace NewsPublish.IBLL{ public interface INewsHaveSetContactBLL { ////// 得到将我增加联系人的新闻的新闻ID列表--------未登录主页+个人中心 /// ///将我增加联系人的新闻的新闻ID列表 ListGetNewsIDOfNewsHaveAddMeIntoContact(string contactName); /// /// 依据对应逻辑获取到的新闻ID列表(依据联系人来分) 然后得到对应逻辑的新闻列表 /// ///将我增加联系人的新闻列表 ListGetNewsListOfNewsHaveAddMeIntoContact(List newsIDList); /// /// 得到将游客和我作为接收人的新闻的新闻ID列表---------登录主页 /// 事实上一開始我还忘了我本人公布的新闻也放在登录主页呢..囧.. /// /// 作为联系人的名字---"我" ///得到将游客,我作为接收人,以及我公布的新闻的ID列表 ListGetNewsIDOfVisitorAndMe(string contactName); /// /// 从联系人增加新闻接收人到详细新闻 /// /// 新闻ID /// 联系人名字 ///增加是否成功 bool AddNewsReceiver(int newsID, string contactName); }}
BLL层
BLL层之NewsBLL
using System;using System.Collections.Generic;using System.Linq;using System.Web;using NewsPublish.IBLL;using NewsPublish.IDAL;using NewsPublish.Model;namespace NewsPublish.BLL{ public class NewsBLL : INewsBLL { INewsDAL _newsDAL = Factory.DALFactory.CreateNewsDAL(); ////// 得到全部新闻列表,主要用于主页显示 /// ///全部新闻列表 public ListGetAllNewsList() { return _newsDAL.GetAllNewsList(); } /// /// 依据username,得到用户已公布的新闻列表 /// /// username ///用户已公布的新闻列表 public ListGetUserNewsListByUserName(string userName) { return _newsDAL.GetUserNewsListByUserName(userName); } /// /// 公布新闻 /// /// 新闻标题 /// 公布新闻系统时间 /// username(新闻,和username都有这么一个属性) /// 新闻内容 /// 新闻附件ID ///返回插入的新闻ID,这对兴许处理非常有帮助,good public int PublishNews(string title, DateTime date, string userName, string content, int fileID) { return _newsDAL.PublishNews(title, date, userName, content,fileID); } ////// 依据新闻ID,得到具体新闻 /// /// 新闻ID ///具体新闻 public NewsModel GetDetailNewsByNewsID(int newsID) { return _newsDAL.GetDetailNewsByNewsID(newsID); } }}
BLL层之UserBLL
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data;using NewsPublish.Model;using NewsPublish.IDAL;using NewsPublish.IBLL;namespace NewsPublish.BLL{ public class UserBLL:IUserBLL { IUserDAL _userDAL = Factory.DALFactory.CreateUserDAL(); ////// 登录 /// /// username /// 密码 ///登录是否成功 public bool Login(string userName, string password) { return _userDAL.Login(userName, password); } ////// 注冊 /// /// username /// 密码 ///注冊是否成功,仅仅只是用int类型更好的变化://flag=1,2,3 分别为:username已存在/注冊成功/注冊失败 public int Register(string userName, string password) { return _userDAL.Register(userName, password); } }}
BLL层之FileServiceBLL
using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Web;using NewsPublish.IBLL;using NewsPublish.IDAL;namespace NewsPublish.BLL{ public class FileServiceBLL : IFileServiceBLL { IFileServiceDAL _fileServiceDAL = Factory.DALFactory.CreateFileServiceDAL(); ////// 上传附件 /// /// 附件标题 /// 附件内容 /// 附件格式 ///返回附件插入数据库的即时ID public int UpLoadFile(string fileTitle, byte[] fileContent, string fileType) { return _fileServiceDAL.UpLoadFile(fileTitle, fileContent, fileType); } ////// 通过fileID下载新闻附件 /// /// 附件ID ///通过DataTable类型返回文件 public DataTable GetHadUpLoadFileByFileID(int fileID) { return _fileServiceDAL.GetHadUpLoadFileByFileID(fileID); } ////// 通过newsID获取fileID; /// /// 新闻ID ///附件ID public int GetFileIDByNewsID(int newsID) { return _fileServiceDAL.GetFileIDByNewsID(newsID); } }}
BLL层之ContactBLL
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data;using NewsPublish.Model;using NewsPublish.IDAL;using NewsPublish.IBLL;namespace NewsPublish.BLL{ public class ContactBLL :IContactBLL { IContactDAL _contactDAL = Factory.DALFactory.CreateContactDAL(); ////// 通过username获取联系人 /// /// username ///联系人列表 public ListGetMyContactByUserName(string userName) { return _contactDAL.GetMyContactByUserName(userName); } /// /// 依据username和联系人加入联系人 /// /// username ///加入是否成功 public bool AddContact(string userName, string newContact) { return _contactDAL.AddContact(userName, newContact); } }}
BLL层之NewsHaveSetContactBLL
using System;using System.Collections.Generic;using System.Linq;using System.Web;using NewsPublish.IBLL;using NewsPublish.IDAL;using NewsPublish.Model;namespace NewsPublish.BLL{ public class NewsHaveSetContactBLL :INewsHaveSetContactBLL { INewsHaveSetContactDAL _newsHaveSetContactDAL = Factory.DALFactory.CreateNewsHaveSetContactDAL(); ////// 得到将我加入联系人的新闻的新闻ID列表--------未登录主页+个人中心 /// ///将我加入联系人的新闻的新闻ID列表 public ListGetNewsIDOfNewsHaveAddMeIntoContact(string contactName) { return _newsHaveSetContactDAL.GetNewsIDOfNewsHaveAddMeIntoContact(contactName); } /// /// 依据对应逻辑获取到的新闻ID列表(依据联系人来分) 然后得到对应逻辑的新闻列表 /// /// 对应逻辑获取到的新闻ID列表 ///对应逻辑的新闻列表 public ListGetNewsListOfNewsHaveAddMeIntoContact(List newsIDList) { return _newsHaveSetContactDAL.GetNewsListOfNewsHaveAddMeIntoContact(newsIDList); } /// /// 得到将游客和我作为接收人的新闻的新闻ID列表---------登录主页 /// 事实上一開始我还忘了我本人公布的新闻也放在登录主页呢..囧.. /// /// 作为联系人的名字---"我" ///得到将游客,我作为接收人,以及我公布的新闻的ID列表 public ListGetNewsIDOfVisitorAndMe(string contactName) { return _newsHaveSetContactDAL.GetNewsIDOfVisitorAndMe(contactName); } /// /// 从联系人加入新闻接收人到详细新闻 /// /// 新闻ID /// 联系人名字 ///加入是否成功 public bool AddNewsReceiver(int newsID, string contactName) { return _newsHaveSetContactDAL.AddNewsReceiver(newsID, contactName); } }}
Factory层
Factory层之DALFactory
using System;using System.Collections.Generic;using System.Linq;using System.Web;using NewsPublish.IDAL;using NewsPublish.DAL;namespace NewsPublish.Factory{ public static class DALFactory { ////// 建立工厂是为了使得建立对象的时候不用了解还有一层中类中的细节减少耦合,仅仅需知道工厂会帮你生产出来须要用的对象就可以 /// ///public static IUserDAL CreateUserDAL() { return new UserDAL(); } public static INewsDAL CreateNewsDAL() { return new NewsDAL(); } public static IContactDAL CreateContactDAL() { return new ContactDAL(); } public static INewsHaveSetContactDAL CreateNewsHaveSetContactDAL() { return new NewsHaveSetContactDAL(); } public static IFileServiceDAL CreateFileServiceDAL() { return new FileServiceDAL(); } }}
Factory层之BLLFactory
using System;using System.Collections.Generic;using System.Linq;using System.Web;using NewsPublish.IBLL;using NewsPublish.BLL;namespace NewsPublish.Factory{ public class BLLFactory { public static IUserBLL CreatUserBLL() { return new UserBLL(); } public static INewsBLL CreateNewsBLL() { return new NewsBLL(); } public static IContactBLL CreateContactBLL() { return new ContactBLL(); } public static INewsHaveSetContactBLL CreateNewsHaveSetContactBLL() { return new NewsHaveSetContactBLL(); } public static IFileServiceBLL CreateFileServiceBLL() { return new FileServiceBLL(); } }}
界面层
界面层之homepage(游客主页)
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using NewsPublish.IBLL;using NewsPublish.Factory;using NewsPublish.Model;namespace 新闻公布系统{ public partial class WebForm2 : System.Web.UI.Page { //依赖于下一层的接口 INewsHaveSetContactBLL _iBLL; protected void Page_Load(object sender, EventArgs e) { //假设是游客的话则会显示游客,而且把游客能看的新闻显示出来,否则已经用不上了.由于我又一次设计了一个登录后的主页. if (Session["userName"] == null) { LabelName.Text = "游客";//获取当前client的username _iBLL = BLLFactory.CreateNewsHaveSetContactBLL(); Listlist = _iBLL.GetNewsIDOfNewsHaveAddMeIntoContact(LabelName.Text); //DataGrid绑定数据 GdvhomePage.DataSource = _iBLL.GetNewsListOfNewsHaveAddMeIntoContact(list); GdvhomePage.DataBind(); } else { LabelName.Text = Session["userName"].ToString(); } } }}
界面层之newscontent(游客查看详细新闻页)
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using NewsPublish.IBLL;using NewsPublish.Factory;using System.Data;using NewsPublish.Model;namespace 新闻公布系统{ public partial class newscontent : System.Web.UI.Page { //依赖于下一层的接口 INewsBLL _ibll = BLLFactory.CreateNewsBLL(); IFileServiceBLL _ifile = BLLFactory.CreateFileServiceBLL(); protected void Page_Load(object sender, EventArgs e) { //还有一种接受client參数的形式 ViewState["NewsID"] = Request.QueryString[0]; int id = Convert.ToInt32(ViewState["NewsID"]); //依据NewsID找到详细的新闻内容返回 NewsModel newsModel = new NewsModel(); newsModel=_ibll.GetDetailNewsByNewsID(id); TxtTitle.Text = newsModel.Title; TxtDate.Text = newsModel.Date.ToString(); TxtUserName.Text = newsModel.UserName; TxtContent.Text = newsModel.Content; //绑定下载数据源文件标题 int fileID = _ifile.GetFileIDByNewsID(id); if (fileID != 0)//假设某一条新闻没有fileID的话,就要做对应的措施,否则会报错 { DataTable dt = new DataTable(); dt = _ifile.GetHadUpLoadFileByFileID(fileID); LabelDisPlayFileTitle.Text = dt.Rows[0]["FileTitle"].ToString(); } else { LabelDisPlayFileTitle.Text = "没有下载文件"; BtnDownLoadFile.Visible = false; } } ////// 下载附件 /// /// /// protected void BtnDownLoadFile_Click(object sender, EventArgs e) { ViewState["NewsID"] = Request.QueryString[0]; int id = Convert.ToInt32(ViewState["NewsID"]); int fileID = _ifile.GetFileIDByNewsID(id); //拿到要下载的文件 DataTable dt = new DataTable(); dt = _ifile.GetHadUpLoadFileByFileID(fileID); //事实上仅仅要拿到Data Table之类的数据表就可以,接下来的一堆Response都是下载,我也看不大懂.-_- Response.Buffer = true; Response.Clear(); //注意了:字节流(字节数组)成功的转为string类型 Response.ContentType = dt.Rows[0]["FileType"].ToString(); Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(dt.Rows[0]["FileTitle"].ToString())); Response.BinaryWrite((Byte[])dt.Rows[0]["FileContent"]); Response.Flush(); Response.End(); } ////// 返回主页 /// /// /// protected void BtnBackHome_Click(object sender, EventArgs e) { if (Session["userName"] == null) { Response.Redirect("homepage.aspx"); } else { Response.Redirect("userhomepage.aspx"); } } }}
界面层之login(登录页面)
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using NewsPublish.IBLL;namespace 新闻公布系统{ public partial class WebForm4 : System.Web.UI.Page { //依赖于下一层的接口 IUserBLL _userBLL = NewsPublish.Factory.BLLFactory.CreatUserBLL(); protected void Page_Load(object sender, EventArgs e) { } protected void BtnLogin_Click(object sender, EventArgs e) { string userName = TextUserName.Text.ToString(); string password = TextPassWord.Text.ToString(); //对登录账号进行验证 if (_userBLL.Login(userName, password)) { Response.Write(""); Session["userName"] = userName; //Response.Redirect("personnel.aspx"); //注意了哦,假设用Response.Write弹出窗体的话,是使用不了那个Response.Redirect()进行跳转的 Response.Write(""); } else { Response.Write(""); } } }}
界面层之register(注冊页面)
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using NewsPublish.IBLL;namespace 新闻公布系统{ public partial class 測试 : System.Web.UI.Page { //依赖于下一层的接口 IUserBLL _userBLL = NewsPublish.Factory.BLLFactory.CreatUserBLL(); protected void Page_Load(object sender, EventArgs e) { } ////// 提交注冊button /// /// /// protected void BtnSubmit_Click(object sender, EventArgs e) { string userName = this.TbxUserName.Text.Trim().ToString(); string pwd = this.TbxPsw.Text.Trim().ToString(); string ConPwd = this.TbxConfPsw.Text.Trim().ToString(); //实在悲剧,一開始注冊用的是验证性控件,后来測试的时候不知道为什么会和Register函数起冲突, //即,Register函数呗忽视而不会运行..仅仅好改为人工码农丑陋代码进行推断了.-_- if(userName.Length==0) { Response.Write(""); } else if(pwd.Length==0||ConPwd.Length==0) { Response.Write(""); } else if(pwd.Equals(ConPwd)==false) { Response.Write(""); } else { int tmp = _userBLL.Register(userName, pwd); //tmp=1,2,3 分别为:username已存在/注冊成功/注冊失败 if (tmp == 2) { Response.Write(""); Response.Write(""); } else if (tmp == 1) { Response.Write(""); } else if (tmp == 3) { Response.Write(""); //Response.Write(""); } } } }}
界面层之personnel(个人中心)
界面层之usernewscontent(登录后查看详细新闻)
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using NewsPublish.IBLL;using NewsPublish.Factory;using System.Data;using NewsPublish.Model;namespace 新闻公布系统{ public partial class newscontent_user : System.Web.UI.Page { //依赖于下一层的接口 INewsBLL _ibll = BLLFactory.CreateNewsBLL(); IFileServiceBLL _ifile = BLLFactory.CreateFileServiceBLL(); protected void Page_Load(object sender, EventArgs e) { //还有一种接受client參数的形式 ViewState["NewsID"] = Request.QueryString[0]; int id = Convert.ToInt32(ViewState["NewsID"]); //依据NewsID找到详细的新闻内容返回 NewsModel newsModel = new NewsModel(); newsModel = _ibll.GetDetailNewsByNewsID(id); TxtTitle.Text = newsModel.Title; TxtDate.Text = newsModel.Date.ToString(); TxtUserName.Text = newsModel.UserName; TxtContent.Text = newsModel.Content; /* TxtTitle.Text = _ibll.GetDetailNewsByNewsID(id).Title; TxtDate.Text = _ibll.GetDetailNewsByNewsID(id).Date.ToString(); TxtUserName.Text = _ibll.GetDetailNewsByNewsID(id).UserName; TxtContent.Text = _ibll.GetDetailNewsByNewsID(id).Content; */ //绑定下载数据源文件标题 int fileID = _ifile.GetFileIDByNewsID(id); if (fileID != 0)//假设某一条新闻没有fileID的话,就要做对应的措施 { DataTable dt = new DataTable(); dt = _ifile.GetHadUpLoadFileByFileID(fileID); LabelDisPlayFileTitle.Text = dt.Rows[0]["FileTitle"].ToString(); } else { LabelDisPlayFileTitle.Text = "没有下载文件"; BtnDownLoadFile.Visible = false; } } ////// 下载附件 /// /// /// protected void BtnDownLoadFile_Click(object sender, EventArgs e) { ViewState["NewsID"] = Request.QueryString[0]; int id = Convert.ToInt32(ViewState["NewsID"]); int fileID = _ifile.GetFileIDByNewsID(id); //拿到要下载的文件 DataTable dt = new DataTable(); dt = _ifile.GetHadUpLoadFileByFileID(fileID); //事实上仅仅要拿到Data Table之类的数据表就可以,接下来的一堆Response都是下载,我也看不大懂.-_- Response.Buffer = true; Response.Clear(); //注意了:字节流(字节数组)成功的转为string类型 Response.ContentType = dt.Rows[0]["FileType"].ToString(); Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(dt.Rows[0]["FileTitle"].ToString())); Response.BinaryWrite((Byte[])dt.Rows[0]["FileContent"]); Response.Flush(); Response.End(); } }}
界面层之userhomepage(登录后首页)
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using NewsPublish.Factory;using NewsPublish.IBLL;using NewsPublish.Model;namespace 新闻公布系统{ public partial class userhomepage : System.Web.UI.Page { //依赖于下一层的接口 INewsHaveSetContactBLL _iBLL; protected void Page_Load(object sender, EventArgs e) { //获取当前client的username string contactName = Session["userName"].ToString(); _iBLL = BLLFactory.CreateNewsHaveSetContactBLL(); //获取我公布的+游客能看得+@我的 这些新闻 Listlist = _iBLL.GetNewsIDOfVisitorAndMe(contactName); //DataGrid绑定数据 GdvUsHoPage.DataSource = _iBLL.GetNewsListOfNewsHaveAddMeIntoContact(list); GdvUsHoPage.DataBind(); } }}
界面层之publish(公布新闻页面)
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using NewsPublish.IBLL;using NewsPublish.Model;using System.IO;namespace 新闻公布系统{ public partial class publish_new_ : System.Web.UI.Page { //依赖于下一层的接口 INewsBLL _newsBLL = NewsPublish.Factory.BLLFactory.CreateNewsBLL(); IFileServiceBLL _fileServiceBLL = NewsPublish.Factory.BLLFactory.CreateFileServiceBLL(); IContactBLL _contactBLL = NewsPublish.Factory.BLLFactory.CreateContactBLL(); INewsHaveSetContactBLL _newsHaveSetContactBLL = NewsPublish.Factory.BLLFactory.CreateNewsHaveSetContactBLL(); int fileID;//用于下方加入一条新闻的时候的绑定 protected void Page_Load(object sender, EventArgs e) { string publisher = Session["userName"].ToString(); LabelUserName.Text = publisher; //新闻接收人控件绑定数据,首先得取出联系人列表 Listtmp = new List (); tmp = _contactBLL.GetMyContactByUserName(publisher); if (tmp != null && tmp.Count > 0) { LabelDisplayMyContacts2.Text = "您的联系人:"; //由于之前那个页面也有这个控件,所以用2来区分识别 this.BulletedListNewsContactChoose.DataSource = _contactBLL.GetMyContactByUserName(publisher); this.BulletedListNewsContactChoose.DataTextField = "ContactName"; //this.BulletedList.DataValueField = "FileID"; this.BulletedListNewsContactChoose.DataBind(); } else { LabelDisplayMyContacts2.Text = "您如今还没有联系人"; } } /// /// 事实上在这里我是硬性规定上传附件和新闻一定是绑定在一个button一起提交的, /// 这个举措是为了不让用户随便上传附件,这个会给数据库带来一定的负担 /// 但我这个是牺牲在对上传附件的是否成功验证的前提下取得的... /// /// /// protected void BulletedListNewsContactChoose_Click(object sender, BulletedListEventArgs e) { //每个名字取出来了 TextBoxDisplayChoise.Focus();//文本框获取焦点 //这个和BulletedList的功能有紧密关系了,获取点击事件,然后依据此获得其选项内容 string contactName = ((BulletedList)sender).Items[e.Index].Value.ToString(); //把加入的新闻联系人逐个显示在textbox控件上面,就像是EMAIL一样,仅仅只是我的控件界面其丑无比, //只是话说回来,基本上没怎么碰过界面的(当然也包含本人过于菜,全部控件都还木有熟悉) TextBoxDisplayChoise.Text += contactName; TextBoxDisplayChoise.Text += ","; } ////// 提交附件+公布新闻button /// /// /// protected void BtnPublish_Click(object sender, EventArgs e) { string title = Txt_Title.Text.ToString(); string publisher = Session["userName"].ToString(); string content = Txt_Content.Text.ToString(); //注意用trim方法去掉左右空格 if (title.Trim().Length == 0 || content.Trim().Length == 0) { Response.Write(" "); } else { //---// //获取上传的文件全部文件 HttpFileCollection fileList = Request.Files; for (int i = 0; i < fileList.Count; i++)//事实上这个count仅仅是1个.哈哈.我測试过了,仅仅只是我把模板拷贝过来的时候懒得改动了 { HttpPostedFile postedFile = fileList[i]; //文件名称 string fileName = postedFile.FileName; //文件类型 string fileType = postedFile.ContentType; //文件转换为二进制流 int fileLength = postedFile.ContentLength; byte[] fileContent = new byte[fileLength]; Stream fileStream = postedFile.InputStream; fileStream.Read(fileContent, 0, fileLength); //存储文件 //之前写的文件服务类 if (fileName.Trim().Length == 0) fileID = 0; else { fileID = _fileServiceBLL.UpLoadFile(fileName, fileContent, fileType);//返回来的是FileID if (fileID != 0) { Response.Write(" "); } else { Response.Write(" "); } } } //---// DateTime date = DateTime.Now; int tmp = _newsBLL.PublishNews(title, date, publisher, content, fileID);//fileID是会为0的 if (tmp != 0) //返回来的是NewsID { //我把联系人当成字符串进行保存了,假设为空的话是会自己主动默认面向游客开放的新闻,即全部人 string contactNameSet = TextBoxDisplayChoise.Text.Trim(); if (contactNameSet.Length == 0) { contactNameSet = "游客"; } _newsHaveSetContactBLL.AddNewsReceiver(tmp, contactNameSet); Response.Write(" "); } else { Response.Write(" "); } Session["userName"] = publisher; Response.Write(" "); } } }}
界面层之personnelContacts(联系人页面)
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using NewsPublish.IBLL;using NewsPublish.Model;namespace 新闻公布系统{ public partial class personnelContacts : System.Web.UI.Page { //依赖于下一层的接口 IContactBLL _contactBLL = NewsPublish.Factory.BLLFactory.CreateContactBLL(); protected void Page_Load(object sender, EventArgs e) { //获取当前client的username string userName = Session["userName"].ToString(); Listtmp = new List (); //获取我的联系人 tmp = _contactBLL.GetMyContactByUserName(userName); if (tmp != null && tmp.Count > 0) { LabelDisplayMyContacts.Text = "您的联系人:"; this.BulletedList.DataSource = _contactBLL.GetMyContactByUserName(userName); //BulletedList绑定数据,这个绑定数据相似下拉框.. this.BulletedList.DataTextField = "ContactName";//事实上就是给选项框选中一个数据库字段 //this.BulletedList.DataValueField = "FileID";//这个就是选项的序号,假设没有给出,则会以字段的值作为序号 this.BulletedList.DataBind(); } else { LabelDisplayMyContacts.Text = "您如今还没有联系人"; } } }}
界面层之email-news(@我新闻页面)
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using NewsPublish.IBLL;using NewsPublish.Factory;using NewsPublish.Model;namespace 新闻公布系统{ public partial class WebForm5 : System.Web.UI.Page { //依赖于下一层的接口 INewsHaveSetContactBLL _iBLL; protected void Page_Load(object sender, EventArgs e) { //获取当前client的username string contactName = Session["userName"].ToString(); _iBLL = BLLFactory.CreateNewsHaveSetContactBLL(); //把登陆后"我"作为联系人contactName进行传參,返回的是@我的新闻,在这里独立抽取出来了 Listlist = _iBLL.GetNewsIDOfNewsHaveAddMeIntoContact(contactName); //DataGrid绑定数据 GdvEmNews.DataSource = _iBLL.GetNewsListOfNewsHaveAddMeIntoContact(list); GdvEmNews.DataBind(); } }}
界面层之Web.sitemap(导航截图,在母版页中存在)
尾记
小小总结,just do it........