asp.net留言板简单入门

程序开始前的准备工作: 第一步,创建web.config中的连接字符串,后面读取它。 第二步,创建数据库,demo,用于存储留言数据。 use demo; create table msgboard( nickName nvarchar(20), email nvarchar(30), ipAddr nchar(20), msgTime date, msgTitle nchar(50), msgContent nchar(100) ); 第三步,创建主页面default.aspx 的内容如下: <%@ Page EnableViewStateMac="false" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

留言板

<%--

--%>豆腐技术站亲情奉献 <%--

--%>
您的呢称:
您的联系Email:
您的发言主题:
您的留言内容
察看所有留言

第四步,doLiuyan.aspx页面的后台代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Collections; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.Sql; using System.Data.SqlClient; using System.Configuration; public partial class doLiuyan : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { SqlConnection Conn; //Hashtable Cfg; //Cfg = Context.GetConfig("appsettings"); string connstr = ConfigurationManager.AppSettings[0].ToString(); Conn = new SqlConnection(connstr); string strSQL = ""; string strNickName = ""; string strMail = ""; string strTitle = ""; string strContent = ""; string strIPAddr = ""; strNickName = Request.Form["txtName"].Replace("'", "''").ToString(); strMail = Request.Form["txtMail"].Replace("'", "''").ToString(); strTitle = Request.Form["txtTitle"].Replace("'", "''").ToString(); strContent = Request.Form["txtContent"].Replace("'", "''").ToString(); strIPAddr = Request.ServerVariables["REMOTE_ADDR"].ToString();//用户IP地址 strSQL = "insert into msgBoard(nickname,email,ipAddr,msgTime,msgTitle,msgContent)values("; strSQL = strSQL + "'" + strNickName + "','" + strMail + "','" + strIPAddr + "',getdate(),'" + strTitle + "','" + strContent + "')"; Response.Write(strSQL); //Conn.Open(); SqlCommand Cmd; Cmd = new SqlCommand(strSQL, Conn); Cmd.Connection.Open(); Cmd.ExecuteNonQuery(); Cmd.Connection.Close(); Response.Redirect("showmsg.aspx"); } } 其他的就不用做了,这个就完成了将留言信息插入到数据库。 祝你好运!

posted @ 2011-11-18 15:05  CooMark  阅读(1271)  评论(0编辑  收藏  举报