日期:2014-05-16  浏览次数:20538 次

ACESS数据库添加留言板(单层数据库操作)
using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Web;
using System.Web.UI.WebControls;

namespace TraceLWord1.WebUI
{
	public partial class PostLWord : System.Web.UI.Page
	{
		// 用户昵称
		protected System.Web.UI.WebControls.TextBox m_aspNickName;
		// 用户密码
		protected System.Web.UI.WebControls.TextBox m_aspPassWord;
		// 留言内容
		protected System.Web.UI.WebControls.TextBox m_aspTextContent;
		// 发送命令按钮
		protected System.Web.UI.WebControls.Button m_aspPostCmd;

		protected void Page_Init(object sender, EventArgs e)
		{
			this.m_aspPostCmd.Click += new EventHandler(PostCmd_Click);
		}

		// 获取注册用户信息
		private const string SQL_GetRegUser = @"SELECT * FROM [RegUser] WHERE [NickName] = @NickName";
		// 注册新用户
		private const string SQL_Register = @"INSERT INTO [RegUser] ( [NickName], [PassWord] ) VALUES ( @NickName, @PassWord )";
		// 发送新留言
		private const string SQL_PostLWord = @"INSERT INTO [LWord] ( [FromUser], [TextContent] ) VALUES ( @FromUser, @TextContent )";

		// 发送留言信息到数据库
		private void PostCmd_Click(object sender, EventArgs e)
		{
			// 获取用户昵称
			string nickName = this.m_aspNickName.Text;
			// 获取用户密码
			string passWord = this.m_aspPassWord.Text;
			// 获取留言内容
			string textContent = this.m_aspTextContent.Text;

			// 用户昵称和密码不能为空
			if (String.IsNullOrEmpty(nickName) || String.IsNullOrEmpty(passWord))
				throw new Exception("用户昵称或密码为空");

			// 留言内容不能为空
			if (String.IsNullOrEmpty(textContent))
				throw new Exception("留言内容为空");

			// 用户是否已注册
			bool isRegistered = false;
			// 存于数据库中的用户密码
			string passWordInDB = null;
            //创建数据库链接
			OleDbConnection dbConn = new OleDbConnection(ConfigurationManager.ConnectionStrings["TraceLWordDB"].ConnectionString);
			//创建数据库命令
            OleDbCommand getRegUserDBCmd = new OleDbCommand(SQL_GetRegUser, dbConn);

			// 设置用户昵称
			getRegUserDBCmd.Parameters.Add("@NickName", OleDbType.VarWChar, 32).Value = nickName;

			try
			{
				dbConn.Open();
				OleDbDataReader dr = getRegUserDBCmd.ExecuteReader();

				if (dr.Read())
				{
					isRegistered = true;

					// 获取存于数据库中的用户密码
					passWordInDB = Convert.ToString(dr["PassWord"]);
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				dbConn.Close();
			}

			if (isRegistered)
			{
				// 如果用户已经注册,则比较用户密码
				if (String.Compare(passWord, passWordInDB) != 0)
					throw new Exception("用户密码错误");

				// 如果密码相同则添加留言信息
				// 
				OleDbCommand postLWordDBCmd = new OleDbCommand(SQL_PostLWord, dbConn);

				// 设置留言发送人昵称
				postLWordDBCmd.Parameters.Add("@FromUser", OleDbType.VarWChar, 32).Value = nickName;
				// 设置留言内容
				postLWordDBCmd.Parameters.Add("@TextContent", OleDbType.LongVarWChar).Value = textContent;

				try
				{
					dbConn.Open();
					postLWordDBCmd.ExecuteNonQuery();
				}
				catch (Exception ex)
				{
					throw ex;
				}
				finally
				{
					dbConn.Close();
				}
			}
			else
			{
				// 如果用户未注册,那么先注册新用户,然后添加留言
				// 
				dbConn.Open();

				try
				{
					OleDbCommand registerDBCmd = new OleDbCommand(SQL_Register, dbConn);

					// 设置用户昵称
					registerDBCmd.Parameters.Add("@NickName", OleDbType.VarWChar, 32).Value = nickName;
					// 设置用户密码
					registerDBCmd.Parameters.Add("@PassWord", OleDbType.VarWChar, 255).Value = passWord;

					registerDBCmd.ExecuteNonQuery();

					OleDbCommand postLWordDBCmd = new OleDbCommand(SQL_PostLWord, dbConn);

					// 设置留言发送人昵称
					postLWordDBCmd.Parameters.Add("@FromUser", OleDbType.VarWChar, 32).Value = nickName;
					// 设置留言内容
					postLWordDBCmd.Parameters.Add("@TextContent", OleDbType.LongVarWChar).Value = textContent;

					postLWordDBCmd.ExecuteNonQuery();
				}
				catch (Exception ex)
				{
					throw ex;
				}
				finally
				{
					dbConn.Close();
				}
			}

			// 跳转到留言显示页面
			Response.Redirect("ListLWord.aspx", true);
		}
	}
}

?