首页 > 编程 > .NET > 正文

实例编程:用ASP.Net和Access编写留言本

2024-07-10 13:07:44
字体:
来源:转载
供稿:网友

 1、用access建造一个数据库data.mdb,建立表guest,结构如下:

  id 自动编号
  标题 字符串
  署名 字符串
  正文 备注。

  2、guest.aspx


 <%@ import namespace="system.data" %>
  <%@ import namespace="system.data.oledb" %>
  <html><head>
  <style>
  <!--
  body{font-size:9pt}
  p{font-size:9pt}
  td{font-size:9pt}
  //-->
  </style>
  <title>留言本</title></head>
  <body>
  <p align=right><font size=6>留言本</font></p>
  <script language=vb runat=server>
  sub page_load(source as object, e as eventargs)
   dim myconn as new oledbconnection("provider=microsoft.jet.oledb.4.0 ;data source=" & server.mappath("data.mdb"))
   dim myadapter as new oledbdataadapter
   dim myoledbcommandbuilder as oledbcommandbuilder = new oledbcommandbuilder(myadapter)
   dim ds as dataset = new dataset
   myadapter.selectcommand = new oledbcommand("select id, 标题, 署名 from guest order by id desc", myconn)
   myconn.open()
   myadapter.fill(ds, "guest")
   ml.datasource=ds.tables("guest").defaultview
   ml.databind()
  end sub
  </script>
   <asp:datagrid id="ml" runat="server"
   width="100%"
   bordercolor="black"
   showfooter="false"
   cellpadding=3
   cellspacing="0"
   font-name="宋体"
   font-size="9pt"
   headerstyle-backcolor="#ddaacc"
   enableviewstate="false"
   >
  <columns>
   <asp:hyperlinkcolumn
   datanavigateurlfield="id"
   datanavigateurlformatstring="see.asp?id={0}"
   text="see!"
   target="_new"
   />
  </columns>
  
  </asp:datagrid>
  <br><br><font size=5>我要留言</font><br>
  <form method=post action=send.aspx>
  标题:<input type=text name=title><br>
  署名:<input type=text name=name><br>
  内容:<textarea name=body></textarea><br>
  <input type=submit>
  </form> 

    3、send.aspx

  <%@ import namespace="system.data" %>
  <%@ import namespace="system.data.oledb" %>
  <script language=vb runat=server>
  sub page_load(source as object, e as eventargs)
   dim myconn as new oledbconnection("provider=microsoft.jet.oledb.4.0 ;data source=" & server.mappath("data.mdb"))
   dim myinsertquery as string = "insert into guest (标题, 署名, 内容) values (@#" & request("title") & "@#, @#" & request("name") & "@#, @#" & request("body") & "@#)"
   dim myoledbcommand as new oledbcommand(myinsertquery)
   myoledbcommand.connection = myconn
   myconn.open()
   myoledbcommand.executenonquery()
   myoledbcommand.connection.close()
   response.redirect("guest.aspx")
  end sub
  </script>  

  4、see.asp(显示单个数据不如用asp)

  <html><head>
  <style>
  <!--
  body{font-size:9pt}
  p{font-size:9pt}
  td{font-size:9pt}
  //-->
  </style>
  <title>察看留言</title></head>
  <body>
  <p align=right><font size=6>察看留言</font></p>
  <%
  id=request("id")
  set cn=server.createobject("adodb.connection")
  cn.open "dbq=" + server.mappath("data.mdb") + ";defaultdir=; driver={microsoft access driver (*.mdb)};"
  set rs=server.createobject("adodb.recordset")
  rs.open "select * from guest where id=" & id , cn
  %>
  id:<% = id %><br>
  标题:<% = rs("标题") %><br>
  署名:<% = rs("署名") %><br>
  内容:<br><% = rs("内容") %><br>
  <a href="guest.aspx">返回</a>


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表