⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 messagesdb.cs

📁 This is not a very mean things
💻 CS
字号:
//***********************************************************************
//
// WebExpert.NET 1.0 
//
// (c) 2003, www.AspCool.com. All rights reserved.
// ASP酷技术网 版权所有
//
// 该源码下载自:http://www.51aspx.com
// 邮箱:tim@aspcool.com
//
// 版权声明:本程序仅供学习使用,你也可以修改后在网站上使用,但使用时必
// 须保留ASP酷技术网(www.AspCool.com)的版权信息和链接。本程序随《ASP.NET
// 网站建设专家》一书赠送,未经作者同意,不得随意修改、传播。
//
// 描述:
//   此文件包含下面的类:
//       MessagesDB
//
// 作者:     王保健
// 时间:     2004-12-04
//
//************************************************************************


using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


namespace AspCool.WebExpert
{
	public class MessagesDB
	{
		

		# region 显示Message内容
		
		//*********************************************************************
		//
		// GetMessages 方法
		//
		// GetMessages 方法返回所有包含所有Message信息的DataSet。
		//
		//*********************************************************************
		public SqlDataReader GetMessages(int ThreadId)
		{
			// 创建一个数据库连接实例和命令对象。
			SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
			SqlCommand myCommand = new SqlCommand("GetMessages", myConnection);

			// 把命令对象的命令类型设置为存储过程。
			myCommand.CommandType = CommandType.StoredProcedure;


			// 向存储过程中传递参数。
			SqlParameter parameterThreadId = new SqlParameter("@ThreadId", SqlDbType.Int, 4);
			parameterThreadId.Value = ThreadId;
			myCommand.Parameters.Add(parameterThreadId);

			// 执行命令。
			myConnection.Open();
			SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
          
			// 返回一个DataSet。
			return result;
		}

		//*********************************************************************
		//
		// GetMessagesRecords 方法
		//
		// GetMessagesRecords 方法是用来计算Messages的总数。
		//
		//
		//*********************************************************************
		public int GetMessagesRecords()
		{
			// 创建一个数据库连接实例和命令对象。
			SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
			SqlCommand myCommand = new SqlCommand("GetMessagesRecords", myConnection);

			// 把命令对象的命令类型设置为存储过程
			myCommand.CommandType = CommandType.StoredProcedure;

			
			// 执行命令。
			myConnection.Open();
			SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
			return (int)result["co"];
            

		}

		//*********************************************************************
		//
		// CurrentPageMessages 方法
		//
		// CurrentPageMessages 方法是用来从一个包含所有Message的DataSet中返回一个只包含
		// 当前页面的DataSet。
		//
		// 其中currentPage是指当前的页号,pageSize是指每页有多少条Message,totalPage
		// 用来返回总共有多少页Message。
		//
		//*********************************************************************
		public DataSet CurrentPageMessages(int currentPage,int pageSize,out int totalPage,out int records)
		{
			// 创建一个数据库连接实例和命令对象。
			SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
			SqlDataAdapter myCommand = new SqlDataAdapter("GetMessages", myConnection);

			// 把命令对象的命令类型设置为存储过程。
			myCommand.SelectCommand.CommandType = CommandType.StoredProcedure;

		
			// 建立并填充一个DataSet。
			DataSet allMessages = new DataSet();
			myCommand.Fill(allMessages);

			// 创建一个新的DataSet。
			DataSet dsCurrentMessages = new DataSet();

			// 计算所有Message的条数。
			records = allMessages.Tables[0].Rows.Count;

			// 计算当前页第一条Message的位置。
			int startIndex = (currentPage-1)*pageSize;

			// 计算当前页最后一条Message的位置。
			int endIndex = startIndex + pageSize;

			// 计算Message的页数。
			totalPage = records/pageSize;

			if (totalPage*pageSize<records)
			{
				totalPage++;
			}

			if (endIndex>records)
				endIndex = records;
			
			// 建立一个与allMessages数据结构完全一样的DataSet.
			dsCurrentMessages = allMessages.Clone();

			// 取得当前页的所有记录,并保存到新的DataSet中去。
			for (int i=startIndex; i<endIndex; i++)
			{
				DataRow row = dsCurrentMessages.Tables[0].NewRow();
				row.ItemArray = allMessages.Tables[0].Rows[i].ItemArray;

				dsCurrentMessages.Tables[0].Rows.Add(row);
			}
			
			// 返回当前页的DataSet。
			return dsCurrentMessages;
		}

		//*********************************************************************
		//
		// ClickMessages 方法
		//
		// ClickMessages 是把Messages数据表中被访问的Message的点击次数增加一。
		//
		//*********************************************************************

		public void ClickMessages(int itemID) 
		{

			// 创建一个数据库连接实例和命令对象。
			SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
			SqlCommand myCommand = new SqlCommand("ClickMessages", myConnection);

			// 把命令对象的命令类型设置为存储过程。
			myCommand.CommandType = CommandType.StoredProcedure;

			// 向存储过程中传递参数。
			SqlParameter parameterItemID = new SqlParameter("@ItemID", SqlDbType.Int, 4);
			parameterItemID.Value = itemID;
			myCommand.Parameters.Add(parameterItemID);

			// 打开数据库连接并执行命令。
			myConnection.Open();
			myCommand.ExecuteNonQuery();
			myConnection.Close();
		}

		//*********************************************************************
		//
		// GetSingleMessage 方法
		//
		// GetSingleMessage 方法是用来返回一条包含某条Message记录的所有信息的SqlDataReader.
		// itemId为这条Message记录的编号。
		//
		//*********************************************************************

		public SqlDataReader GetSingleMessage(int itemId) 
		{

			// 创建一个数据库连接实例和命令对象。
			SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
			SqlCommand myCommand = new SqlCommand("GetSingleMessage", myConnection);

			// 把命令对象的命令类型设置为存储过程
			myCommand.CommandType = CommandType.StoredProcedure;

			// 向存储过程中传递参数。
			SqlParameter parameterItemId = new SqlParameter("@ItemId", SqlDbType.Int, 4);
			parameterItemId.Value = itemId;
			myCommand.Parameters.Add(parameterItemId);

			// 执行命令。
			myConnection.Open();
			SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            
			// 返回一个datareader。
			return result;
		}
		# endregion

		# region 增加、修改、删除Message


		

		//*********************************************************************
		//
		// AddMessage 方法
		//
		// AddMessage 方法在Messages表中增加一条新的Message记录,此方法返回新增加的Message的 
		// Message编号。
		//
		//*********************************************************************
		public void AddMessage(System.Int32 threadID,System.String author,System.String content,System.String subject)
		{		

			// 创建一个数据库连接实例和命令对象。
			SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
			SqlCommand myCommand = new SqlCommand("AddMessage", myConnection);

			// 把命令对象的命令类型设置为存储过程。
			myCommand.CommandType = CommandType.StoredProcedure;

			// 向存储过程中传递参数。			

			SqlParameter parameterThreadID = new SqlParameter("@threadID", SqlDbType.Int ,4);
			parameterThreadID.Value = threadID;
			myCommand.Parameters.Add(parameterThreadID);

			SqlParameter parameterAuthor = new SqlParameter("@author", SqlDbType.NVarChar,50);
			parameterAuthor.Value = author;
			myCommand.Parameters.Add(parameterAuthor);

			SqlParameter parameterSubject = new SqlParameter("@subject", SqlDbType.NVarChar,50);
			parameterSubject.Value = subject;
			myCommand.Parameters.Add(parameterSubject);

			SqlParameter parameterContent = new SqlParameter("@content", SqlDbType.NText ,16);
			parameterContent.Value = content;
			myCommand.Parameters.Add(parameterContent);


			// 打开数据库连接并执行命令。
			myConnection.Open();
			myCommand.ExecuteNonQuery();
			myConnection.Close();
		}

		//*********************************************************************
		//
		// UpdateMessages 方法
		//
		// UpdateMessages 是用来根据Message编号来更新Messages表中的一条Message记录。
		//
		//*********************************************************************

		public void UpdateMessage(System.Int32 messageID,System.DateTime postTime,System.Int32 threadID,System.String author,System.String content,System.String subject)
		{

			
			// 创建一个数据库连接实例和命令对象。
			SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
			SqlCommand myCommand = new SqlCommand("UpdateMessage", myConnection);

			// 向存储过程中传递参数。
			myCommand.CommandType = CommandType.StoredProcedure;

			// 向存储过程中传递参数。			
			SqlParameter parameterMessageID = new SqlParameter("@messageID", SqlDbType.Int,4);
			parameterMessageID.Value = messageID;
			myCommand.Parameters.Add(parameterMessageID);

			SqlParameter parameterThreadID = new SqlParameter("@threadID", SqlDbType.Int ,4);
			parameterThreadID.Value = threadID;
			myCommand.Parameters.Add(parameterThreadID);

			SqlParameter parameterAuthor = new SqlParameter("@author", SqlDbType.NVarChar,50);
			parameterAuthor.Value = author;
			myCommand.Parameters.Add(parameterAuthor);

			SqlParameter parameterSubject = new SqlParameter("@subject", SqlDbType.NVarChar,50);
			parameterSubject.Value = subject;
			myCommand.Parameters.Add(parameterSubject);

			SqlParameter parameterContent = new SqlParameter("@content", SqlDbType.NText ,16);
			parameterContent.Value = content;
			myCommand.Parameters.Add(parameterContent);

			SqlParameter parameterPostTime = new SqlParameter("@postTime", SqlDbType.DateTime ,8);
			parameterPostTime.Value = postTime;
			myCommand.Parameters.Add(parameterPostTime);

			// 打开数据库连接并执行命令。
			myConnection.Open();
			myCommand.ExecuteNonQuery();
			myConnection.Close();
		
		
		}

		//*********************************************************************
		//
		// DeleteMessage 方法
		//
		// DeleteMessage 方法是根据Message编号删除一条Message记录。
		// itemID为要删除的Message的编号。
		//
		//*********************************************************************

		public void DeleteMessage(int itemID) 
		{

			// 创建一个数据库连接实例和命令对象。
			SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
			SqlCommand myCommand = new SqlCommand("DeleteMessage", myConnection);

			// 把命令对象的命令类型设置为存储过程。
			myCommand.CommandType = CommandType.StoredProcedure;

			// 向存储过程中传递参数。
			SqlParameter parameterItemID = new SqlParameter("@ItemID", SqlDbType.Int, 4);
			parameterItemID.Value = itemID;
			myCommand.Parameters.Add(parameterItemID);

			// 打开数据库连接并执行命令。
			myConnection.Open();
			myCommand.ExecuteNonQuery();
			myConnection.Close();
		}

		# endregion
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -