发信人: dongbao()
整理人: dongbao(2000-11-11 10:16:42), 站内信件
|
需要三个文件guestbook.htm guestbook.aspx display.aspx
guestbook.htm:
<html>
<head>
<title>用C#写的留言簿</title>
<meta name = "dongbao" content = "text/html;charset=gb2312" http-e quiv = "Content-Type">
<script language = "javascript"></script>
</head>
<body>
<form action = "guestbook.aspx" method = "post">
Name : <input name = "name"/><br/>
Sex : <input name = "sex" type = "radio" value = "boy"/> Boy
<input checked = "checked" name = "sex" type = "radio" val ue = "girl"/> Girl<br/>
Subject : <input name = "subject"/><br/>
Memo : <textarea name = "memo"></textarea><br/>
<input type = "submit" value = "Send"/> <input type = "reset" va lue = "Reset"/>
</form>
</body>
</html>
guestbook.aspx:
<%
/*
测试环境:
NGWS sdk + Windows 2000 ADS + IE 5.5 + Access 2000
日期:
2000 年 11 月 10 日
*/
%>
<%@ Page Language = "C#"%>
<%@ Import Namespace = "System.Data"%>
<%@ Import Namespace = "System.Data.ADO"%>
<%
String strName,strSex,strSubject;
StringBuilder strMemo;
/*收集数据*/
strName = Request.Form["name"];
strSex = Request.Form["sex"];
strSubject = Request.Form["subject"];
strMemo = new StringBuilder (Request.Form["memo"]);
strMemo = strMemo.Replace( "'" , "''" , 0 , strMemo.Length ); //替换 调\',避免出错
String strConn = "dsn=guestbook"; //建立连接
/*设定sql语句用于向数据库插入数据*/
String strSql = "insert into guestbook ( name , sex , subject , memo ) values ( '";
strSql += strName + "' , '" + strSex + "' , '" + strSubject + "' , ' " + strMemo + "' )";
ADOConnection Conn = new ADOConnection(); //创建连接实例(方法一)
Conn.ConnectionString = strConn; //设定连接的参数
ADOCommand Cmd = new ADOCommand( strSql , Conn ); //创建ADOCommand实 例
try
{
Conn.Open(); //打开连接
Cmd.Execute(); //执行ADOCommand
Conn.Close(); //关闭连接
Page.Navigate("display.aspx"); //页面转向
}
catch ( Exception e )
{
Response.Write( e.ToString() ); //输出错误信息
}
%>
display.aspx:
<%@ Page Language = "C#"%>
<%@ Import Namespace = "System.Data"%>
<%@ Import Namespace = "System.Data.ADO"%>
<%
String strSql = "select * from guestbook order by id desc"; //设定sq l语句来读取表的内容
String strConn = "dsn=guestbook"; //设定连接语句
ADOConnection Conn = new ADOConnection( strConn ); //建立连接
ADODataSetCommand Cmd = new ADODataSetCommand( strSql , Conn ); //创 建ADODataSetCommand实例
DataSet Ds = new DataSet(); //创建DataSet实例
try
{
Conn.Open(); //打开连接
Cmd.FillDataSet( Ds , "guestbook" ); //设定数据关联
}
catch ( Exception e )
{
Response.Write( e.ToString() ); //输出错误信息
}
%>
<html>
<head>
<meta author = "dongbao-C#" charset = "gb2312"/>
<script language = "C#" runat = "server"></script>
<title>显示留言内容</title>
</head>
<body>
<table align = "center" border = "1" width = "75%">
<tr align = "center"><td>ID</td><td>Name</td><td>Sex</td><td>Subjec t</td><td>Memo</td></tr>
<%
for ( int i = 0 ; i != Ds.Tables["guestbook"].Rows.Count ; i ++ ) / /循环显示全部数据
{
%>
<tr align = "center">
<td><%=Ds.Tables["guestbook"].Rows[i]["id"].ToString()%></td>
<td><%=Ds.Tables["guestbook"].Rows[i]["name"].ToString()%></td >
<td><%=Ds.Tables["guestbook"].Rows[i]["sex"].ToString()%></td>
<td><%=Ds.Tables["guestbook"].Rows[i]["subject"].ToString()%>< /td>
<td><%=Ds.Tables["guestbook"].Rows[i]["memo"].ToString()%></td >
</tr>
<%
}
Conn.Close();
%>
</table>
</body>
</html>
-- ICQ:43395237 OICQ:126132
我自豪我用正版,我骄傲我用盗版!!!
※ 来源:.月光程序代码网 http://www.moon-soft.com.[FROM: 202.108.2.77]
|
|