程序开始前的准备工作: 第一步,创建web.config中的连接字符串,后面读取它。
<%--
--%>豆腐技术站亲情奉献 <%--
第四步,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"); } } 其他的就不用做了,这个就完成了将留言信息插入到数据库。 祝你好运!