读书人

ACESS数据库平添留言板(单层数据库操作

发布时间: 2012-06-27 14:20:09 作者: rapoo

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);}}}

?

读书人网 >其他数据库

热点推荐