熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

實例編程:用ASP.Net和Access編寫留言本

2013-11-13 10:14:15  來源: .NET編程 
用Access建造一個數據庫datamdb建立表Guest結構如下:

  ID 自動編號
  標題 字符串
  署名 字符串
  正文 備注

  guestaspx


    <%@ Import Namespace=SystemData %>
  <%@ Import Namespace=SystemDataOleDb %>
  <html><head>
  <style>
  <!
  body{fontsize:pt}
  p{fontsize:pt}
  td{fontsize:pt}
  //>
  </style>
  <title>留言本</title></head>
  <body>
  <p align=right><font size=>留言本</font></p>
  <script language=vb runat=server>
  Sub Page_Load(Source As Object E As EventArgs)
   Dim myConn As New OleDbConnection(Provider=MicrosoftJetOLEDB ;Data Source= & servermappath(datamdb))
   Dim myAdapter As New OleDbDataAdapter
   Dim myOleDbCommandBuilder As OleDbCommandBuilder = New OleDbCommandBuilder(myAdapter)
   Dim DS As DataSet = New DataSet
   myAdapterSelectCommand = new OleDbCommand(select ID 標題 署名 from Guest order by id desc myconn)
   myconnopen()
   myAdapterFill(ds Guest)
   mlDataSource=dsTables(Guest)DefaultView
   mlDataBind()
  end sub
  </script>
   <ASP:DataGrid id=ml runat=server
   Width=%
   BorderColor=black
   ShowFooter=false
   CellPadding=
   CellSpacing=
   FontName=宋體
   FontSize=pt
   HeaderStyleBackColor=#ddaacc
   EnableViewState=false
   >
  <Columns>
   <asp:HyperLinkColumn
   DataNavigateUrlField=id
   DataNavigateUrlFormatString=seeasp?id={}
   text=See!
   Target=_new
   />
  </Columns>
  
  </asp:DataGrid>
  <br><br><font size=>我要留言</font><br>
  <form method=post action=sendaspx>
  標題:<input type=text name=title><br>
  署名:<input type=text name=name><br>
  內容:<textarea name=body></textarea><br>
  <input type=submit>
  </form> 

  sendaspx

     <%@ Import Namespace=SystemData %>
  <%@ Import Namespace=SystemDataOleDb %>
  <script language=vb runat=server>
  Sub Page_Load(Source As Object E As EventArgs)
   Dim myConn As New OleDbConnection(Provider=MicrosoftJetOLEDB ;Data Source= & servermappath(datamdb))
   Dim myInsertQuery As String = insert into guest (標題 署名 內容) values (@# & request(title) & @# @# & request(name) & @# @# & request(body) & @#)
   Dim myOleDbCommand As New OleDbCommand(myInsertQuery)
   myOleDbCommandConnection = myConn
   myConnOpen()
   myOleDbCommandExecuteNonQuery()
   myOleDbCommandConnectionClose()
   responseredirect(guestaspx)
  end sub
  </script>  

  seeasp(顯示單個數據不如用ASP)

  

  <html><head>
  <style>
  <!
  body{fontsize:pt}
  p{fontsize:pt}
  td{fontsize:pt}
  //>
  </style>
  <title>察看留言</title></head>
  <body>
  <p align=right><font size=>察看留言</font></p>
  <%
  id=request(id)
  set cn=servercreateobject(ADODBCONNECTION)
  cnopen DBQ= + servermappath(datamdb) + ;DefaultDir=; DRIVER={Microsoft Access Driver (*mdb)};
  set rs=servercreateobject(adodbrecordset)
  rsopen select * from guest where id= & id cn
  %>
  ID:<% = id %><BR>
  標題:<% = rs(標題) %><br>
  署名:<% = rs(署名) %><br>
  內容:<Br><% = rs(內容) %><br>
  <a >返回</a>


From:http://tw.wingwit.com/Article/program/net/201311/13000.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.