2012年度课程设计---新闻发布系统(小结)
-----Presented By muximuxi@Achilles
Tips:因本课程设计大部分代码皆有本人短时间仓促码成,界面恶心,代码丑陋.唯一优点便是: 全部代码都已贴上,并且全部都已注释.另外与Asp.net教程结合恰当,通俗易懂,容易上手.
需求
新闻发布系统需求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
{
/// <summary>
/// 封装News的字段和属性
/// </summary>
public class NewsModel
{
#region NewsModel构造函数
/// <summary>
/// NewsModel构造函数
/// </summary>
/// <param name="title">新闻标题</param>
/// <param name="date">新闻发布日期</param>
/// <param name="content">新闻内容</param>
/// <param name="newsID">新闻ID(key)</param>
/// <param name="fileID">新闻所含的上传附件ID(or not)</param>
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;
}
/// <summary>
/// 其实建立这个构造函数是为了弥补这个缺陷的:没有绑定用户名;
/// 不过话说是每一个model只是含有这个样例的
/// </summary>
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
{
/// <summary>
/// 封装User的字段和属性
/// </summary>
public class UserModel
{
#region UserModel构造函数
/// <summary>
/// UserModel构造函数
/// </summary>
/// <param name="userID">用户ID</param>
/// <param name="userName">用户名</param>
/// <param name="password">用户密码</param>
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构造函数
/// <summary>
/// NewsModel构造函数
/// </summary>
/// <param name="fileID">该新闻上传的附件的ID</param>
/// <param name="fileTitle">附件标题</param>
/// <param name="fileContent">附件内容</param>
/// <param name="fileType">附件格式,这是和附件以二进制流的形式存在数据库的内容紧密相关的,不懂度娘去(附件上传数据库)</param>
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构造函数
/// <summary>
/// ContactModel的构造函数
/// </summary>
/// <param name="contactName">联系人</param>
/// <param name="userName">用户</param>
public ContactModel(string contactName,string userName)
{
this._contactName = contactName;
this._userName = userName;
}
public ContactModel()
{
}
#endregion
#region 设置新闻类的属性
//用户的联系人
private string _contactName = null;
//用户名
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#语法书没看,摸黑过来,百度后推测应该是这样)
/// <summary>
/// 得到所有新闻列表,主要用于主页显示
/// </summary>
/// <returns>所有新闻列表</returns>
List<NewsModel> GetAllNewsList();
/// <summary>
/// 根据用户名,得到用户已发布的新闻列表
/// </summary>
/// <param name="userName">用户名</param>
/// <returns>用户已发布的新闻列表</returns>
List<NewsModel> GetUserNewsListByUserName(string userName);
/// <summary>
/// 发布新闻
/// </summary>
/// <param name="title">新闻标题</param>
/// <param name="date">发布新闻系统时间</param>
/// <param name="userName">用户名(新闻,和用户名都有这么一个属性)</param>
/// <param name="content">新闻内容</param>
/// <param name="fileID">新闻附件ID</param>
/// <returns>返回插入的新闻ID,这对后续处理很有帮助,good</returns>
int PublishNews(string title, DateTime date, string userName, string content,int fileID);
/// <summary>
/// 由新闻Id得到具体新闻内容
/// </summary>
/// <param name="newsID">新闻ID</param>
/// <returns>详细新闻</returns>
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
{
/// <summary>
/// 登录
/// </summary>
/// <param name="userName">用户名</param>
/// <param name="password">密码</param>
/// <returns>登录是否成功</returns>
bool Login(string userName, string password);
/// <summary>
/// 注册
/// </summary>
/// <param name="userName">用户名</param>
/// <param name="password">密码</param>
/// <returns>注册是否成功,只不过用int类型更好的变化://flag=1,2,3 分别为:用户名已存在/注册成功/注册失败</returns>
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
{
/// <summary>
/// 上传附件
/// </summary>
/// <param name="fileTitle">附件标题</param>
/// <param name="fileContent">附件内容</param>
/// <param name="fileType">附件格式</param>
/// <returns>返回附件插入数据库的即时ID</returns>
int UpLoadFile(string fileTitle, byte[] fileContent, string fileType);
/// <summary>
/// 通过fileID下载新闻附件
/// </summary>
/// <param name="fileID">附件ID</param>
/// <returns>通过DataTable类型返回文件</returns>
DataTable GetHadUpLoadFileByFileID(int fileID);
/// <summary>
/// 通过newsID获取fileID;
/// </summary>
/// <param name="newsID">新闻ID</param>
/// <returns>附件ID</returns>
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
{
/// <summary>
/// 通过用户名获取联系人
/// </summary>
/// <param name="userName">用户名</param>
/// <returns>联系人列表</returns>
List<ContactModel> GetMyContactByUserName(string userName);
/// <summary>
/// 根据用户名和联系人添加联系人
/// </summary>
/// <param name="newContact">用户名</param>
/// <returns>添加是否成功</returns>
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
{
/// <summary>
/// 得到将我加入联系人的新闻的新闻ID列表--------未登录主页+个人中心
/// </summary>
/// <returns>将我加入联系人的新闻的新闻ID列表</returns>
List<NewsHaveSetContactModel> GetNewsIDOfNewsHaveAddMeIntoContact(string contactName);
/// <summary>
/// 根据相应逻辑获取到的新闻ID列表(根据联系人来分) 然后得到相应逻辑的新闻列表
/// </summary>
/// <returns>将我加入联系人的新闻列表</returns>
List<NewsModel> GetNewsListOfNewsHaveAddMeIntoContact(List<NewsHaveSetContactModel> newsIDList);
/// <summary>
/// 得到将游客和我作为接收人的新闻的新闻ID列表---------登录主页
/// 其实一开始我还忘了我本人发布的新闻也放在登录主页呢..囧..
/// </summary>
/// <param name="contactName">作为联系人的名字---"我"</param>
/// <returns>得到将游客,我作为接收人,以及我发布的新闻的ID列表</returns>
List<NewsHaveSetContactModel> GetNewsIDOfVisitorAndMe(string contactName);
/// <summary>
/// 从联系人添加新闻接收人到具体新闻
/// </summary>
/// <param name="newsID">新闻ID</param>
/// <param name="contactName">联系人名字</param>
/// <returns>添加是否成功</returns>
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;
/// <summary>
/// 构造函数
/// </summary>
public SQLHelper()
{
}
/// <summary>
/// ExecuteNonQuery操作,对数据库进行 增、删、改 操作((1)
/// </summary>
/// <param name="sql">要执行的SQL语句 </param>
/// <returns> </returns>
public int ExecuteNonQuery(string sql)
{
return ExecuteNonQuery(sql, CommandType.Text, null);
}
/// <summary>
/// ExecuteNonQuery操作,对数据库进行 增、删、改 操作(2)
/// </summary>
/// <param name="sql">要执行的SQL语句 </param>
/// <param name="commandType">要执行的查询类型(存储过程、SQL文本) </param>
/// <returns> </returns>
public int ExecuteNonQuery(string sql, CommandType commandType)
{
return ExecuteNonQuery(sql, commandType, null);
}
/// <summary>
/// ExecuteNonQuery操作,对数据库进行 增、删、改 操作(3)
/// </summary>
/// <param name="sql">要执行的SQL语句 </param>
/// <param name="commandType">要执行的查询类型(存储过程、SQL文本) </param>
/// <param name="parameters">参数数组 </param>
/// <returns> </returns>
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;
}
/// <summary>
/// SqlDataAdapter的Fill方法执行一个查询,并返回一个DataSet类型结果(1)
/// </summary>
/// <param name="sql">要执行的SQL语句 </param>
/// <returns> </returns>
public DataSet ExecuteDataSet(string sql)
{
return ExecuteDataSet(sql, CommandType.Text, null);
}
/// <summary>
/// SqlDataAdapter的Fill方法执行一个查询,并返回一个DataSet类型结果(2)
/// </summary>
/// <param name="sql">要执行的SQL语句 </param>
/// <param name="commandType">要执行的查询类型(存储过程、SQL文本) </param>
/// <returns> </returns>
public DataSet ExecuteDataSet(string sql, CommandType commandType)
{
return ExecuteDataSet(sql, commandType, null);
}
/// <summary>
/// SqlDataAdapter的Fill方法执行一个查询,并返回一个DataSet类型结果(3)
/// </summary>
/// <param name="sql">要执行的SQL语句 </param>
/// <param name="commandType">要执行的查询类型(存储过程、SQL文本) </param>
/// <param name="parameters">参数数组 </param>
/// <returns> </returns>
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;
}
/// <summary>
/// SqlDataAdapter的Fill方法执行一个查询,并返回一个DataTable类型结果(1)
/// </summary>
/// <param name="sql">要执行的SQL语句 </param>
/// <returns> </returns>
public DataTable ExecuteDataTable(string sql)
{
return ExecuteDataTable(sql, CommandType.Text, null);
}
/// <summary>
/// SqlDataAdapter的Fill方法执行一个查询,并返回一个DataTable类型结果(2)
/// </summary>
/// <param name="sql">要执行的SQL语句 </param>
/// <param name="commandType">要执行的查询类型(存储过程、SQL文本) </param>
/// <returns> </returns>
public DataTable ExecuteDataTable(string sql, CommandType commandType)
{
return ExecuteDataTable(sql, commandType, null);
}
/// <summary>
/// SqlDataAdapter的Fill方法执行一个查询,并返回一个DataTable类型结果(3)
/// </summary>
/// <param name="sql">要执行的SQL语句 </param>
/// <param name="commandType">要执行的查询类型(存储过程、SQL文本) </param>
/// <param name="parameters">参数数组 </param>
/// <returns> </returns>
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;
}
/// <summary>
/// ExecuteReader执行一查询,返回一SqlDataReader对象实例(1)
/// </summary>
/// <param name="sql">要执行的SQL语句 </param>
/// <returns> </returns>
public SqlDataReader ExecuteReader(string sql)
{
return ExecuteReader(sql, CommandType.Text, null);
}
/// <summary>
/// ExecuteReader执行一查询,返回一SqlDataReader对象实例(2)
/// </summary>
/// <param name="sql">要执行的SQL语句 </param>
/// <param name="commandType">要执行的查询类型(存储过程、SQL文本) </param>
/// <returns> </returns>
public SqlDataReader ExecuteReader(string sql, CommandType commandType)
{
return ExecuteReader(sql, commandType, null);
}
/// <summary>
/// ExecuteReader执行一查询,返回一SqlDataReader对象实例(3)
/// </summary>
/// <param name="sql">要执行的SQL语句 </param>
/// <param name="commandType">要执行的查询类型(存储过程、SQL文本) </param>
/// <param name="parameters">参数数组 </param>
/// <returns> </returns>
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);
}
/// <summary>
/// ExecuteScalar执行一查询,返回查询结果的第一行第一列(1)
/// </summary>
/// <param name="sql">要执行的SQL语句 </param>
/// <returns> </returns>
public Object ExecuteScalar(string sql)
{
return ExecuteScalar(sql, CommandType.Text, null);
}
/// <summary>
/// ExecuteScalar执行一查询,返回查询结果的第一行第一列(2)
/// </summary>
/// <param name="sql">要执行的SQL语句 </param>
/// <param name="commandType">要执行的查询类型(存储过程、SQL文本) </param>
/// <returns> </returns>
public Object ExecuteScalar(string sql, CommandType commandType)
{
return ExecuteScalar(sql, commandType, null);
}
/// <summary>
/// ExecuteScalar执行一查询,返回查询结果的第一行第一列(3)
/// </summary>
/// <param name="sql">要执行的SQL语句 </param>
/// <param name="commandType">要执行的查询类型(存储过程、SQL文本) </param>
/// <returns> </returns>
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;
}
/// <summary>
/// 返回当前连接的数据库中所有由用户创建的数据库
/// </summary>
/// <returns> </returns>
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();
}
/// <summary>
/// 得到所有新闻列表,,主要用于主页显示(不过因为第二阶段需求之后,此函数已无用)
/// </summary>
/// <returns>所有新闻列表</returns>
public List<NewsModel> GetAllNewsList()
{
//创建新闻对象列表
List<NewsModel> newsList = new List<NewsModel>();
//选择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;
}
/// <summary>
/// 根据用户名,得到用户已发布的新闻列表
/// </summary>
/// <param name="userName">用户名</param>
/// <returns>用户已发布的新闻列表</returns>
public List<NewsModel> GetUserNewsListByUserName(string userName)
{
//创建新闻对象列表
List<NewsModel> userNewsList = new List<NewsModel>();
//选择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;
}
/// <summary>
/// 发布新闻
/// </summary>
/// <param name="title">新闻标题</param>
/// <param name="date">发布新闻系统时间</param>
/// <param name="userName">用户名(新闻,和用户名都有这么一个属性)</param>
/// <param name="content">新闻内容</param>
/// <param name="fileID">新闻附件ID</param>
/// <returns>返回插入的新闻ID,这对后续处理很有帮助,good</returns>
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;
}
}
/// <summary>
/// 根据新闻ID,得到详细新闻
/// </summary>
/// <param name="newsID">新闻ID</param>
/// <returns>详细新闻</returns>
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();
}
/// <summary>
/// 登录
/// </summary>
/// <param name="userName">用户名</param>
/// <param name="password">密码</param>
/// <returns>登录是否成功</returns>
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;
}
}
/// <summary>
/// 注册
/// </summary>
/// <param name="userName">用户名</param>
/// <param name="password">密码</param>
/// <returns>注册是否成功,只不过用int类型更好的变化</returns>
public int Register(string userName, string password)
{
int flag; //flag=1,2,3 分别为:用户名已存在/注册成功/注册失败
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();
}
/// <summary>
/// 上传附件
/// </summary>
/// <param name="fileTitle">附件标题</param>
/// <param name="fileContent">附件内容</param>
/// <param name="fileType">附件格式</param>
/// <returns>返回附件插入数据库的即时ID</returns>
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;
}
}
/// <summary>
/// 通过fileID下载新闻附件
/// </summary>
/// <param name="fileID">附件ID</param>
/// <returns>通过DataTable类型返回文件</returns>
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;
}
/// <summary>
/// 通过newsID获取fileID;
/// </summary>
/// <param name="newsID">新闻ID</param>
/// <returns>附件ID</returns>
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();
}
/// <summary>
/// 通过用户名获取联系人
/// </summary>
/// <param name="userName">用户名</param>
/// <returns>联系人列表</returns>
public List<ContactModel> GetMyContactByUserName(string userName)
{
string myContact = null;
List<ContactModel> contactList = new List<ContactModel>();
//选择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;
}
/// <summary>
/// 根据用户名和联系人添加联系人
/// </summary>
/// <param name="newContact">用户名</param>
/// <returns>添加是否成功</returns>
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<NewsModel>
{
private SQLHelper sqlhelper = null;
public NewsHaveSetContactDAL()
{
sqlhelper = new SQLHelper();
}
/// <summary>
/// 哈哈,这个是查字典写的快排的Compare方法,下文中根据ID列表一条一条的去新闻的话,这样子是不会
/// 自动排序的嘛,所以自己就随便调用一下系统的快排队时间进行排序嘛.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public int Compare(NewsModel x, NewsModel y)
{
if(x.Date>y.Date)
return -1;
if (x.Date < y.Date)
return 1;
return 0;
}
/// <summary>
/// 得到将我加入联系人的新闻的新闻ID列表--------邮箱新闻或者未登录主页(传参"游客")
/// </summary>
/// <returns>将我加入联系人的新闻的新闻ID列表</returns>
public List<NewsHaveSetContactModel> GetNewsIDOfNewsHaveAddMeIntoContact(string contactName)
{
//其实这个方法是有漏洞的呢,因为like的不严谨.也是C语言的字符串匹配内容,待会再查查KMP吧,必须严谨才行
//创建新闻ID列表
List<NewsHaveSetContactModel> newsIDOfTheNewsHaveContactList = new List<NewsHaveSetContactModel>();
//选择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;
}
/// <summary>
/// 根据相应逻辑获取到的新闻ID列表(根据联系人来分) 然后得到相应逻辑的新闻列表
/// </summary>
/// <param name="newsIDList">相应逻辑获取到的新闻ID列表</param>
/// <returns>相应逻辑的新闻列表</returns>
public List<NewsModel> GetNewsListOfNewsHaveAddMeIntoContact(List<NewsHaveSetContactModel> newsIDList)
{
//创建新闻对象列表
List<NewsModel> newsList = new List<NewsModel>();
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;
}
/// <summary>
/// 得到将游客和我作为接收人的新闻的新闻ID列表---------登录主页
/// 其实一开始我还忘了我本人发布的新闻也放在登录主页呢..囧..
/// </summary>
/// <param name="contactName">作为联系人的名字---"我"</param>
/// <returns>得到将游客,我作为接收人,以及我发布的新闻的ID列表</returns>
public List<NewsHaveSetContactModel> GetNewsIDOfVisitorAndMe(string contactName)
{
//创建新闻ID列表
List<NewsHaveSetContactModel> newsIDOfTheNewsHaveContactList = new List<NewsHaveSetContactModel>();
//选择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;
}
/// <summary>
/// 从联系人添加新闻接收人到具体新闻
/// </summary>
/// <param name="newsID">新闻ID</param>
/// <param name="contactName">联系人名字</param>
/// <returns>添加是否成功</returns>
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
{
/// <summary>
/// 得到所有新闻列表,主要用于主页显示
/// </summary>
/// <returns>所有新闻列表</returns>
List<NewsModel> GetAllNewsList();
/// <summary>
/// 根据用户名,得到用户已发布的新闻列表
/// </summary>
/// <param name="userName">用户名</param>
/// <returns>用户已发布的新闻列表</returns>
List<NewsModel> GetUserNewsListByUserName(string userName);
/// <summary>
/// 发布新闻
/// </summary>
/// <param name="title">新闻标题</param>
/// <param name="date">发布新闻系统时间</param>
/// <param name="userName">用户名(新闻,和用户名都有这么一个属性)</param>
/// <param name="content">新闻内容</param>
/// <param name="fileID">新闻附件ID</param>
/// <returns>返回插入的新闻ID,这对后续处理很有帮助,good</returns>
int PublishNews(string title, DateTime date, string userName, string content, int fileID);
/// <summary>
/// 根据新闻ID,得到详细新闻
/// </summary>
/// <param name="newsID">新闻ID</param>
/// <returns>详细新闻</returns>
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
{
/// <summary>
/// 登录
/// </summary>
/// <param name="userName">用户名</param>
/// <param name="password">密码</param>
/// <returns>登录是否成功</returns>
bool Login(string userName, string password);
/// <summary>
/// 注册
/// </summary>
/// <param name="userName">用户名</param>
/// <param name="password">密码</param>
/// <returns>注册是否成功,只不过用int类型更好的变化://flag=1,2,3 分别为:用户名已存在/注册成功/注册失败</returns>
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
{
/// <summary>
/// 上传附件
/// </summary>
/// <param name="fileTitle">附件标题</param>
/// <param name="fileContent">附件内容</param>
/// <param name="fileType">附件格式</param>
/// <returns>返回附件插入数据库的即时ID</returns>
int UpLoadFile(string fileTitle, byte[] fileContent, string fileType);
/// <summary>
/// 通过fileID下载新闻附件
/// </summary>
/// <param name="fileID">附件ID</param>
/// <returns>通过DataTable类型返回文件</returns>
DataTable GetHadUpLoadFileByFileID(int fileID);
/// <summary>
/// 通过newsID获取fileID;
/// </summary>
/// <param name="newsID">新闻ID</param>
/// <returns>附件ID</returns>
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
{
/// <summary>
/// 通过用户名获取联系人
/// </summary>
/// <param name="userName">用户名</param>
/// <returns>联系人列表</returns>
List<ContactModel> GetMyContactByUserName(string userName);
/// <summary>
/// 根据用户名和联系人添加联系人
/// </summary>
/// <param name="newContact">用户名</param>
/// <returns>添加是否成功</returns>
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
{
/// <summary>
/// 得到将我加入联系人的新闻的新闻ID列表--------未登录主页+个人中心
/// </summary>
/// <returns>将我加入联系人的新闻的新闻ID列表</returns>
List<NewsHaveSetContactModel> GetNewsIDOfNewsHaveAddMeIntoContact(string contactName);
/// <summary>
/// 根据相应逻辑获取到的新闻ID列表(根据联系人来分) 然后得到相应逻辑的新闻列表
/// </summary>
/// <returns>将我加入联系人的新闻列表</returns>
List<NewsModel> GetNewsListOfNewsHaveAddMeIntoContact(List<NewsHaveSetContactModel> newsIDList);
/// <summary>
/// 得到将游客和我作为接收人的新闻的新闻ID列表---------登录主页
/// 其实一开始我还忘了我本人发布的新闻也放在登录主页呢..囧..
/// </summary>
/// <param name="contactName">作为联系人的名字---"我"</param>
/// <returns>得到将游客,我作为接收人,以及我发布的新闻的ID列表</returns>
List<NewsHaveSetContactModel> GetNewsIDOfVisitorAndMe(string contactName);
/// <summary>
/// 从联系人添加新闻接收人到具体新闻
/// </summary>
/// <param name="newsID">新闻ID</param>
/// <param name="contactName">联系人名字</param>
/// <returns>添加是否成功</returns>
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();
/// <summary>
/// 得到所有新闻列表,主要用于主页显示
/// </summary>
/// <returns>所有新闻列表</returns>
public List<NewsModel> GetAllNewsList()
{
return _newsDAL.GetAllNewsList();
}
/// <summary>
/// 根据用户名,得到用户已发布的新闻列表
/// </summary>
/// <param name="userName">用户名</param>
/// <returns>用户已发布的新闻列表</returns>
public List<NewsModel> GetUserNewsListByUserName(string userName)
{
return _newsDAL.GetUserNewsListByUserName(userName);
}
/// <summary>
/// 发布新闻
/// </summary>
/// <param name="title">新闻标题</param>
/// <param name="date">发布新闻系统时间</param>
/// <param name="userName">用户名(新闻,和用户名都有这么一个属性)</param>
/// <param name="content">新闻内容</param>
/// <param name="fileID">新闻附件ID</param>
/// <returns>返回插入的新闻ID,这对后续处理很有帮助,good</returns>
public int PublishNews(string title, DateTime date, string userName, string content, int fileID)
{
return _newsDAL.PublishNews(title, date, userName, content,fileID);
}
/// <summary>
/// 根据新闻ID,得到详细新闻
/// </summary>
/// <param name="newsID">新闻ID</param>
/// <returns>详细新闻</returns>
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();
/// <summary>
/// 登录
/// </summary>
/// <param name="userName">用户名</param>
/// <param name="password">密码</param>
/// <returns>登录是否成功</returns>
public bool Login(string userName, string password)
{
return _userDAL.Login(userName, password);
}
/// <summary>
/// 注册
/// </summary>
/// <param name="userName">用户名</param>
/// <param name="password">密码</param>
/// <returns>注册是否成功,只不过用int类型更好的变化://flag=1,2,3 分别为:用户名已存在/注册成功/注册失败</returns>
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();
/// <summary>
/// 上传附件
/// </summary>
/// <param name="fileTitle">附件标题</param>
/// <param name="fileContent">附件内容</param>
/// <param name="fileType">附件格式</param>
/// <returns>返回附件插入数据库的即时ID</returns>
public int UpLoadFile(string fileTitle, byte[] fileContent, string fileType)
{
return _fileServiceDAL.UpLoadFile(fileTitle, fileContent, fileType);
}
/// <summary>
/// 通过fileID下载新闻附件
/// </summary>
/// <param name="fileID">附件ID</param>
/// <returns>通过DataTable类型返回文件</returns>
public DataTable GetHadUpLoadFileByFileID(int fileID)
{
return _fileServiceDAL.GetHadUpLoadFileByFileID(fileID);
}
/// <summary>
/// 通过newsID获取fileID;
/// </summary>
/// <param name="newsID">新闻ID</param>
/// <returns>附件ID</returns>
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();
/// <summary>
/// 通过用户名获取联系人
/// </summary>
/// <param name="userName">用户名</param>
/// <returns>联系人列表</returns>
public List<ContactModel> GetMyContactByUserName(string userName)
{
return _contactDAL.GetMyContactByUserName(userName);
}
/// <summary>
/// 根据用户名和联系人添加联系人
/// </summary>
/// <param name="newContact">用户名</param>
/// <returns>添加是否成功</returns>
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();
/// <summary>
/// 得到将我加入联系人的新闻的新闻ID列表--------未登录主页+个人中心
/// </summary>
/// <returns>将我加入联系人的新闻的新闻ID列表</returns>
public List<NewsHaveSetContactModel> GetNewsIDOfNewsHaveAddMeIntoContact(string contactName)
{
return _newsHaveSetContactDAL.GetNewsIDOfNewsHaveAddMeIntoContact(contactName);
}
/// <summary>
/// 根据相应逻辑获取到的新闻ID列表(根据联系人来分) 然后得到相应逻辑的新闻列表
/// </summary>
/// <param name="newsIDList">相应逻辑获取到的新闻ID列表</param>
/// <returns>相应逻辑的新闻列表</returns>
public List<NewsModel> GetNewsListOfNewsHaveAddMeIntoContact(List<NewsHaveSetContactModel> newsIDList)
{
return _newsHaveSetContactDAL.GetNewsListOfNewsHaveAddMeIntoContact(newsIDList);
}
/// <summary>
/// 得到将游客和我作为接收人的新闻的新闻ID列表---------登录主页
/// 其实一开始我还忘了我本人发布的新闻也放在登录主页呢..囧..
/// </summary>
/// <param name="contactName">作为联系人的名字---"我"</param>
/// <returns>得到将游客,我作为接收人,以及我发布的新闻的ID列表</returns>
public List<NewsHaveSetContactModel> GetNewsIDOfVisitorAndMe(string contactName)
{
return _newsHaveSetContactDAL.GetNewsIDOfVisitorAndMe(contactName);
}
/// <summary>
/// 从联系人添加新闻接收人到具体新闻
/// </summary>
/// <param name="newsID">新闻ID</param>
/// <param name="contactName">联系人名字</param>
/// <returns>添加是否成功</returns>
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
{
/// <summary>
/// 建立工厂是为了使得建立对象的时候不用了解另一层中类中的细节降低耦合,只需知道工厂会帮你生产出来需要用的对象即可
/// </summary>
/// <returns></returns>
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 = "游客";//获取当前客户端的用户名
_iBLL = BLLFactory.CreateNewsHaveSetContactBLL();
List<NewsHaveSetContactModel> list = _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)
{
//另一种接受客户端参数的形式
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;
}
}
/// <summary>
/// 下载附件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
/// <summary>
/// 返回主页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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("<script language=javascript>alert('登录成功!');</script>");
Session["userName"] = userName;
//Response.Redirect("personnel.aspx");
//注意了哦,如果用Response.Write弹出窗口的话,是使用不了那个Response.Redirect()进行跳转的
Response.Write("<script> window.location='personnel.aspx'</script>");
}
else
{
Response.Write("<script language=javascript>alert('账号或者密码错误,登录失败!');</script>");
}
}
}
}
界面层之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)
{
}
/// <summary>
/// 提交注册按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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("<script language=javascript>alert('用户名不能为空!');</script>");
}
else if(pwd.Length==0||ConPwd.Length==0)
{
Response.Write("<script language=javascript>alert('密码不能为空!');</script>");
}
else if(pwd.Equals(ConPwd)==false)
{
Response.Write("<script language=javascript>alert('密码不相等,请重新输入!');</script>");
}
else
{
int tmp = _userBLL.Register(userName, pwd); //tmp=1,2,3 分别为:用户名已存在/注册成功/注册失败
if (tmp == 2)
{
Response.Write("<script language=javascript>alert('注册成功');</script>");
Response.Write("<script>window.location='login.aspx'</script>");
}
else if (tmp == 1)
{
Response.Write("<script language=javascript>alert('用户名已经存在!');</script>");
}
else if (tmp == 3)
{
Response.Write("<script language=javascript>alert('注册出错,请检查输入的信息是否符合要求');</script>");
//Response.Write("<script>window.location='register.aspx'</script>");
}
}
}
}
}
界面层之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)
{
//另一种接受客户端参数的形式
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;
}
}
/// <summary>
/// 下载附件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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)
{
//获取当前客户端的用户名
string contactName = Session["userName"].ToString();
_iBLL = BLLFactory.CreateNewsHaveSetContactBLL();
//获取我发布的+游客能看得+@我的 这些新闻
List<NewsHaveSetContactModel> list = _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;
//新闻接收人控件绑定数据,首先得取出联系人列表
List<ContactModel> tmp = new List<ContactModel>();
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 = "您现在还没有联系人";
}
}
/// <summary>
/// 其实在这里我是硬性规定上传附件和新闻一定是绑定在一个按钮一起提交的,
/// 这个举措是为了不让用户随便上传附件,这个会给数据库带来一定的负担
/// 但我这个是牺牲在对上传附件的是否成功验证的前提下取得的...
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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 += ",";
}
/// <summary>
/// 提交附件+发布新闻按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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("<script language=javascript>alert('标题或者内容不能为空!请重新输入!');</script>");
}
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("<script language=javascript>alert('上传附件成功!');</script>");
}
else
{
Response.Write("<script language=javascript>alert('上传附件失败!请重新上传');</script>");
}
}
}
//---//
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("<script language=javascript>alert('发布新闻成功!');</script>");
}
else
{
Response.Write("<script language=javascript>alert('发布新闻失败!');</script>");
}
Session["userName"] = publisher;
Response.Write("<script> window.location='personnel.aspx'</script>");
}
}
}
}
界面层之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)
{
//获取当前客户端的用户名
string userName = Session["userName"].ToString();
List<ContactModel> tmp = new List<ContactModel>();
//获取我的联系人
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)
{
//获取当前客户端的用户名
string contactName = Session["userName"].ToString();
_iBLL = BLLFactory.CreateNewsHaveSetContactBLL();
//把登陆后"我"作为联系人contactName进行传参,返回的是@我的新闻,在这里独立抽取出来了
List<NewsHaveSetContactModel> list = _iBLL.GetNewsIDOfNewsHaveAddMeIntoContact(contactName);
//DataGrid绑定数据
GdvEmNews.DataSource = _iBLL.GetNewsListOfNewsHaveAddMeIntoContact(list);
GdvEmNews.DataBind();
}
}
}
界面层之Web.sitemap(导航截图,在母版页中存在)
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode title="主页" description="Home" url="~/userhomepage.aspx" >
<siteMapNode title="个人中心" description="Services we offer"
url="~/personnel.aspx">
<siteMapNode title="发布新闻" description="Training classes"
url="~/publish.aspx" />
<siteMapNode title="添加联系人" description="Consulting services"
url="~/Addcontact.aspx" />
<siteMapNode title="@我新闻" description="Consulting services"
url="~/email-news.aspx" />
</siteMapNode>
</siteMapNode>
</siteMap>
尾记
小小总结,just do it........