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> /// 事实上建立这个构造函数是为了弥补这个缺陷的:没有绑定username; /// 只是话说是每个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 } }
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">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 } }
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 } }
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; //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 } }
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 } }
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> /// 依据username,得到用户已公布的新闻列表 /// </summary> /// <param name="userName">username</param> /// <returns>用户已公布的新闻列表</returns> List<NewsModel> GetUserNewsListByUserName(string userName); /// <summary> /// 公布新闻 /// </summary> /// <param name="title">新闻标题</param> /// <param name="date">公布新闻系统时间</param> /// <param name="userName">username(新闻,和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); } }
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">username</param> /// <param name="password">密码</param> /// <returns>登录是否成功</returns> bool Login(string userName, string password); /// <summary> /// 注冊 /// </summary> /// <param name="userName">username</param> /// <param name="password">密码</param> /// <returns>注冊是否成功,仅仅只是用int类型更好的变化://flag=1,2,3 分别为:username已存在/注冊成功/注冊失败</returns> int Register(string userName, string password); } }
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); } }
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> /// 通过username获取联系人 /// </summary> /// <param name="userName">username</param> /// <returns>联系人列表</returns> List<ContactModel> GetMyContactByUserName(string userName); /// <summary> /// 依据username和联系人加入联系人 /// </summary> /// <param name="newContact">username</param> /// <returns>加入是否成功</returns> bool AddContact(string userName, string newContact); } }
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); } }
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; } } }
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> /// 依据username,得到用户已公布的新闻列表 /// </summary> /// <param name="userName">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">username(新闻,和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; } } }
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">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">username</param> /// <param name="password">密码</param> /// <returns>注冊是否成功,仅仅只是用int类型更好的变化</returns> 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; } } }
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; } } }
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> /// 通过username获取联系人 /// </summary> /// <param name="userName">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> /// 依据username和联系人加入联系人 /// </summary> /// <param name="newContact">username</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; } } } } }
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; } } } }
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> /// 依据username,得到用户已公布的新闻列表 /// </summary> /// <param name="userName">username</param> /// <returns>用户已公布的新闻列表</returns> List<NewsModel> GetUserNewsListByUserName(string userName); /// <summary> /// 公布新闻 /// </summary> /// <param name="title">新闻标题</param> /// <param name="date">公布新闻系统时间</param> /// <param name="userName">username(新闻,和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); } }
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">username</param> /// <param name="password">密码</param> /// <returns>登录是否成功</returns> bool Login(string userName, string password); /// <summary> /// 注冊 /// </summary> /// <param name="userName">username</param> /// <param name="password">密码</param> /// <returns>注冊是否成功,仅仅只是用int类型更好的变化://flag=1,2,3 分别为:username已存在/注冊成功/注冊失败</returns> int Register(string userName, string password); } }
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); } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using NewsPublish.Model; namespace NewsPublish.IBLL { public interface IContactBLL { /// <summary> /// 通过username获取联系人 /// </summary> /// <param name="userName">username</param> /// <returns>联系人列表</returns> List<ContactModel> GetMyContactByUserName(string userName); /// <summary> /// 依据username和联系人加入联系人 /// </summary> /// <param name="newContact">username</param> /// <returns>加入是否成功</returns> bool AddContact(string userName, string newContact); } }
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); } }
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> /// 依据username,得到用户已公布的新闻列表 /// </summary> /// <param name="userName">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">username(新闻,和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); } } }
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">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">username</param> /// <param name="password">密码</param> /// <returns>注冊是否成功,仅仅只是用int类型更好的变化://flag=1,2,3 分别为:username已存在/注冊成功/注冊失败</returns> public int Register(string userName, string password) { return _userDAL.Register(userName, password); } } }
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); } } }
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> /// 通过username获取联系人 /// </summary> /// <param name="userName">username</param> /// <returns>联系人列表</returns> public List<ContactModel> GetMyContactByUserName(string userName) { return _contactDAL.GetMyContactByUserName(userName); } /// <summary> /// 依据username和联系人加入联系人 /// </summary> /// <param name="newContact">username</param> /// <returns>加入是否成功</returns> public bool AddContact(string userName, string newContact) { return _contactDAL.AddContact(userName, newContact); } } }
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); } } }
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(); } } }
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(); } } }
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(); List<NewsHaveSetContactModel> list = _iBLL.GetNewsIDOfNewsHaveAddMeIntoContact(LabelName.Text); //DataGrid绑定数据 GdvhomePage.DataSource = _iBLL.GetNewsListOfNewsHaveAddMeIntoContact(list); GdvhomePage.DataBind(); } else { LabelName.Text = Session["userName"].ToString(); } } } }
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; } } /// <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"); } } } }
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>"); } } } }
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> /// 提交注冊button /// </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(‘username不能为空!‘);</script>"); } else if(pwd.Length==0||ConPwd.Length==0) { Response.Write("<script language=javascript>alert(‘password不能为空!‘);</script>"); } else if(pwd.Equals(ConPwd)==false) { Response.Write("<script language=javascript>alert(‘password不相等,请又一次输入!‘);</script>"); } else { int tmp = _userBLL.Register(userName, pwd); //tmp=1,2,3 分别为:username已存在/注冊成功/注冊失败 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(‘username已经存在!‘);</script>"); } else if (tmp == 3) { Response.Write("<script language=javascript>alert(‘注冊出错,请检查输入的信息是否符合要求‘);</script>"); //Response.Write("<script>window.location=‘register.aspx‘</script>"); } } } } }
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; } } /// <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(); } } }
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(); //获取我公布的+游客能看得+@我的 这些新闻 List<NewsHaveSetContactModel> list = _iBLL.GetNewsIDOfVisitorAndMe(contactName); //DataGrid绑定数据 GdvUsHoPage.DataSource = _iBLL.GetNewsListOfNewsHaveAddMeIntoContact(list); GdvUsHoPage.DataBind(); } } }
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> /// 事实上在这里我是硬性规定上传附件和新闻一定是绑定在一个button一起提交的, /// 这个举措是为了不让用户随便上传附件,这个会给数据库带来一定的负担 /// 但我这个是牺牲在对上传附件的是否成功验证的前提下取得的... /// </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> /// 提交附件+公布新闻button /// </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>"); } } } }
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(); 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 = "您如今还没有联系人"; } } } }
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进行传參,返回的是@我的新闻,在这里独立抽取出来了 List<NewsHaveSetContactModel> list = _iBLL.GetNewsIDOfNewsHaveAddMeIntoContact(contactName); //DataGrid绑定数据 GdvEmNews.DataSource = _iBLL.GetNewsListOfNewsHaveAddMeIntoContact(list); GdvEmNews.DataBind(); } } }
<?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>
新闻公布系统 (Asp.net 三层架构 ),布布扣,bubuko.com
原文:http://www.cnblogs.com/mfrbuaa/p/3835087.html