打印

[asp.net教程] 浅谈“三层结构”原理与用意 3

浅谈“三层结构”原理与用意 3

postlword.aspx页面文件(发送留言到数据库)
   
   
   
  #001 <%@ page language="c#" codebehind="postlword.aspx.cs" autoeventwireup="false"
   
  inherits="tracelword1.postlword" %>
   
  #002 <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
   
  #003
   
  #004 <html>
   
  #005 <head>
   
  #006 <title>postlword</title>
   
  #007 <meta name="generator" content="microsoft visual studio .net 7.1">
   
  #008 <meta name="code_language" content="c#">
   
  #009 <meta name=vs_defaultclientscript content="javascript">
   
  #010 <meta name=vs_targetschema content="http://schemas.microsoft.com/intellisense/ie5">
   
  #011 </head>
   
  #012 <body ms_positioning="gridlayout">
   
  #013
   
  #014 <form id="__aspnetform" method="post" runat="server">
   
  #015
   
  #016 <textarea id="m_txtcontent" runat="server" rows=8 cols=48></textarea>
   
  #017 <input type="button" id="m_btnpost" runat="server" value="发送留言" />
   
  #018
   
  #019 </form>
   
  #020
   
  #021 </body>
   
  #022 </html>
   
   
   
  postlword.aspx后台程序文件postlword.aspx.cs
   
   
   
  #001 using system;
   
  #002 using system.collections;
   
  #003 using system.componentmodel;
   
  #004 using system.data;
   
  #005 using system.data.oledb; // 需要操作 access 数据库
   
  #006 using system.drawing;
   
  #007 using system.web;
   
  #008 using system.web.sessionstate;
   
  #009 using system.web.ui;
   
  #010 using system.web.ui.webcontrols;
   
  #011 using system.web.ui.htmlcontrols;
   
  #012
   
  #013 namespace tracelword1
   
  #014 {
   

  #015 /// <summary>
   
  #016 /// postlword 发送留言到数据库
   
  #017 /// </summary>
   
  #018 public class postlword : system.web.ui.page
   
  #019 {
   
  #020 // 留言内容编辑框
   
  #021 protected system.web.ui.htmlcontrols.htmltextarea m_txtcontent;
   
  #022 // 提交按钮
   
  #023 protected system.web.ui.htmlcontrols.htmlinputbutton m_btnpost;
   
  #024
   
  #025 /// <summary>
   
  #026 /// postlword.aspx 页面加载函数
   
  #027 /// </summary>
   
  #028 private void page_load(object sender, system.eventargs e)
   
  #029 {
   
  #030 }
   
  #031
   
  #032 #region web 窗体设计器生成的代码
   
  #033 override protected void oninit(eventargs e)
   
  #034 {
   
  #035 initializecomponent();
   
  #036 base.oninit(e);
   
  #037 }
   
  #038
   
  #039 private void initializecomponent()
   
  #040 {
   
  #041 this.load+=new system.eventhandler(this.page_load);
   
  #042 this.m_btnpost.serverclick+=new eventhandler(post_serverclick);
   
  #043 }
   
  #044 #endregion
   
  #046 /// <summary>
   
  #047 /// 发送留言信息到数据库
   
  #048 /// </summary>
   
  #049 private void post_serverclick(object sender, eventargs e)
   
  #050 {
   
  #051 // 获取留言内容
   
  #052 string textcontent=this.m_txtcontent.value;
   
  #053
   
  #054 // 留言内容不能为空
   
  #055 if(textcontent=="")
   
  #056 throw new exception("留言内容为空");
   
  #057
   
  #058 string mdbconn=@"provider=microsoft.jet.oledb.4.0; data source=c:\dbfs\tracelworddb.mdb";
   
  #059 string cmdtext="insert into [lword]([textcontent]) values(@textcontent)";
   
  #060
   
  #061 oledbconnection dbconn=new oledbconnection(mdbconn);
   
  #062 oledbcommand dbcmd=new oledbcommand(cmdtext, dbconn);
   
  #063
   
  #064 // 设置留言内容
   
  #065 dbcmd.parameters.add(new oledbparameter("@textcontent",
   
  oledbtype.longvarwchar));
   
  #066 dbcmd.parameters["@textcontent"].value=textcontent;
   
  #067
   
  #068 try
   
  #069 {
   
  #070 dbconn.open();
   
  #071 dbcmd.executenonquery();
   
  #072 }
   
  #073 catch
   
  #074 {
   
  #075 throw;
   
  #076 }
   
  #077 finally
   
  #078 {
   
  #079 dbconn.close();
   
  #080 }
   
  #081
   
  #082 // 跳转到留言显示页面
   
  #083 response.redirect("listlword.aspx", true);
   
  #084 }
   
  #085 }
   
  #086 }
   
   
   
  仅仅通过两个页面,就完成了一个基于access数据库的留言功能。
   
  程序并不算复杂,非常简单清楚。但是随后你会意识到其存在着不灵活性!

TOP

返回顶部
AYBlue

Processed in 0.052865 second(s), 7 queries.

当前时区 GMT+8, 现在时间是 2009-1-10 10:58 京ICP备06054220号

清除 Cookies - 联系我们 - 163K.com - Archiver - WAP