ASP-SQL-Server制作留言板的完整小例子.doc

上传人:scccc 文档编号:12711229 上传时间:2021-12-05 格式:DOC 页数:8 大小:31KB
返回 下载 相关 举报
ASP-SQL-Server制作留言板的完整小例子.doc_第1页
第1页 / 共8页
ASP-SQL-Server制作留言板的完整小例子.doc_第2页
第2页 / 共8页
ASP-SQL-Server制作留言板的完整小例子.doc_第3页
第3页 / 共8页
ASP-SQL-Server制作留言板的完整小例子.doc_第4页
第4页 / 共8页
ASP-SQL-Server制作留言板的完整小例子.doc_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《ASP-SQL-Server制作留言板的完整小例子.doc》由会员分享,可在线阅读,更多相关《ASP-SQL-Server制作留言板的完整小例子.doc(8页珍藏版)》请在三一文库上搜索。

1、写写关于SQL Server数据库的简单使用过程的教程,也算一个小总结!记录一些常用的方法,关键字,单词等,供以后查阅用!同时希望对大家的学习有一定帮助!不要忘了就好。 我喜欢小例子带注释的学习方法!所以自己总结起来学习的过程也总是配着例子,边做边记我们用ASP+SQL Server做个简单的留言板为例!当然像这样的例子有些地方实际中并没有必要用到这么“深”的东西,但是我们是为了学习,尽量的使用、体验更多的知识。如果你的SQL SERVER的初学者,完成这个例子我相信一定对你学习SQL SERVER有很大帮助!学习本教程需要:了解SQL语句和基本语法;了解SQL Server查询分析器的作用,

2、会初步使用;熟悉ASP。 本教程设及到:使用SQL Server查询分析器创建数据库;SQL查询语句常用的一些属性值;触发器创建和使用;存储过程的创建,ASP使用存储过程。正文开始:一、创建一个数据库 打开SQL SERVER查询分析器,创建一个feedback数据库,该数据库的主数据文件的逻辑名称是feedback,操作系统文件是feedback.mdf,大小是15MB,最大是30MB,以20%的速度增加;该数据库的日志文件的逻辑名称是feedback_log,操作系统文件是feedback.ldf,大小是3MB,最大是10MB,以1MB的速度增加。Create Database feedb

3、ack -创建数据库feedbackOn 语法错误?Primary (Name=feedback, Filename='d:feedback.mdf', -数据库操作系统文件的目录和名称Size=15MB, Maxsize=30MB, Filegrowth=20%) Log On (Name=feedback_log, Filename='d:feedback.ldf', Size=3MB, Maxsize=10MB, FileGrowth=1MB) USE feedback -打开数据库 推荐精选二、创建两个表,一个用来作留言,一个作留言的回复!1、创建第一个

4、表:Feedback存放留言的记录!Drop Table Feedback -如果已经有此表将其删除,第一次创建,不用这句!GOCreate Table Feedback -创建表FeedBack( Feedback_ID int Primary Key Identity (1, 1) Not Null, -字段Feedback_ID ,主关键字,自动累加,初值为1,自动加1,不能为空-逗号可不加 Title nvarchar(256) Not Null, -字段Title 留言标题,类型nvarchar 大小256,不能为空 Content text Not Null, -字段Content

5、 -留言内容,类型文本字段,不能为空 subFeedback_count int default 0 -字段subFeedback_count 回复的条数!默认值0) 2、插入一条新记录,并显示出来Insert into Feedback(Title,Content)values('here is Title','This is a test')GOselect * from Feedback 3、创建第二表:subFeedback存放留言的回复Create Table subFeedback( subFeedback_ID int Primary Key id

6、entity(1,1) Not Null, Feedback_ID int Foreign key references Feedback(Feedback_ID), -定义外键关联到表Feedback的主键Feedback_ID Content text Not Null) 三、创建两个触发器推荐精选1、第一个触发器(级联删除触发器):当删除Feedback表中的记录时,自动删除subFeedback中外键对应相同的所有记录 Create Trigger Trigger_delete_FeedbackON Feedback-在表feedback上建触发器Trigger_delete_Feed

7、backInstead OF Delete -INSTEAD OF 触发器表示并不执行其所定义的操作(INSERT、 UPDATE、 DELETE),而仅是执行触发器本身-或者说发生Delete事件时执行,该触发器AS后语名会替换过delete语句的执行ASDelete From subFeedback where Feedback_ID in(select Feedback_ID from deleted)-删除表subFeedback外键与删除feedback主键相同的值Delete From Feedback where Feedback_ID in(select Feedback_ID

8、 from deleted) 2、第二个触发器:当subFeedback有新增记录时,Feedback.subFeedback_count字段记数增加! Create Trigger Trigger_update_subFeedbackON subFeedbackFor insert -注间和Instead OF的区别,For是当insert语句执行完后再执行解发器AS后的语句ASupdate Feedback set subFeedback_count=subFeedback_count+1 where Feedback_ID in(select Feedback_ID from inser

9、ted) 另外:如果考虑的较周全点,当subFeedback中的记录删除时,Feedback_subFeedback_count字段还要减1,触发器的写法和上面一相似,为减短教程,就不在增加!四、建立两个存储过程用来保存增加的Feedback和subFeedback记录 Create Procedure proc_insert_Feedback -创建存储过程proc_insert_FeedbackTitle nvarChar(256),Content text -定义参数变量AS Insert into Feedback (Title,Content) values(Title,Conten

10、t) -执行语句GOCreate Procedure proc_insert_subFeedback Feedback_ID int,Content textAS Insert into subFeedback (Feedback_ID,Content) values(Feedback_ID,Content)推荐精选 五、建立asp文件,完成留言板制作!1、创建conn.asp文件,与数据库连接。 <%dim conn set conn=Server.createobject("ADODB.CONNECTION") '创建连接对象conn.open="

11、;Provider=SQLOLEDB; Data Source=127.0.0.1;" & _"Initial Catalog=Feedback; User ID=sa; password=sa;" '打开连接。换成你的server-IP(如果也是本机不用修改),数据库用户名,密码!%>2、创建List.asp显示留言,内容。这里我把增加的 Form 也加到了文件底部,减少文件的个数。 <!-#include file="conn.asp"-><!-用include file包含数据库连接文件。->

12、<%SQL="select * from Feedback"Set rs=Server.CreateObject("ADODB.Recordset") '创建数据集rsrs.open SQL,conn,1,3 '打开if not rs.eof then output="" '定义字符串变量output,输出 do while not rs.eof '外循环开始 output=output&rs("title") output=output&"-<

13、a href=Feedback.asp?feedback_ID="&rs("feedback_ID")&"&title="&rs("title")&">回复该留言</a>"&cstr(rs("subFeedback_count")&"<hr>"'建立回复留言的链接,并把要回复的留言的记录Feedback_ID和Title传给Feedback.asp'Feedback

14、用来标志是回复了哪条记录,增加数据库用!Title用来显示回复的哪条记录,给回复者看 output=output&rs("content") output=output&"<br><br>" sqlsub="select * from subFeedback where Feedback_ID="&rs("Feedback_ID") Set rsSub=Server.CreateObject("ADODB.Recordset") rsSub.ope

15、n sqlSub,conn,1,3 if not rsSub.eof then j=1 '为for语句定义变理 do while not rsSub.eof 推荐精选 for k=1 to j '贴子缩进,贴子越靠后,缩进量越大 output=output&" " next output=output&""&j&"楼<span style='word-wrap: break-word;'>" output=output&rsSub("cont

16、ent") output=output&"</span><br>" j=j+1 rsSub.movenext loop end if output=output&"<br>" rs.movenext loopresponse.write outputelseresponse.write "无记录!"end ifrs.closeset rs=nothing%><script>function chkform()/这个函数用来判断输入是否为空/当然这里的判断还

17、远远不够,比仿说还要判断字符的多少,是否有非法字符等if (document.add.title.value=""| document.add.content.value="")alert("标题或内容不能为空,请输入!");return;document.add.action="add.asp"document.add.submit;</script><form name="add" method="post" action="javascrip

18、t:chkfrom();">标题<input type=text size="50" name=title><br>内容<textarea name="content" cols="50" rows="8"></textarea><br><input type="hidden" value="Feedback" name="table"><!-上面是一个隐藏域,传

19、递一个名为table,值为Feedback变量,让add.asp知道是编辑的Feedback表->推荐精选<input type="submit" name=submit value=" 提 交 "></form> 通过上面的list.asp文件,这时如果数据库有有数据,那么网页中就可以显示数据了,如果没有内容网页显示“无记录”,下边显示增加表单。3、创建Feedback.asp文件,用来填写留言的回复! 回复:<%=request("title")%><form name="

20、add" method="post" action="add.asp">内容<textarea name="content" cols="50" rows="8"></textarea><br><input type="hidden" name="table" value="subFeedback"><input type="hidden" nam

21、e="Feedback_ID" value='<%=request.QueryString("Feedback_ID")%>'><input type="submit" name=submit value=" 提 交 "></form> 4、创建add.asp文件,用来分别保存时Feedback,subFeedback的两个表的增加记录!这里请注意ASP调用SQL SERVER的存储过程的方法,会让程序变的很简洁! <!-#include file=&

22、quot;conn.asp"-><%table=request.form("table") '用来判断是编辑的哪个表if table="Feedback" then title=cstr(trim(request.form("title") content=cstr(trim(request.form("content") 'trim去掉字符串前后的空格,cstr数据类型转为字符型 if title<>"" and content<>

23、"" then Conn.Execute "proc_insert_Feedback '"&title&"','"&content&"'" else response.write "<script>alert('所需数据为空,请填写')</script>" response.write"<script>history.go(-1)</script>"

24、 response.end end ifelseif table="subFeedback" then Feedback_ID=trim(request.form("feedback_ID") content=cstr(trim(request.form("content") if Feedback_ID<>"" and content<>"" then Conn.Execute "proc_insert_subFeedback "&Feedb

25、ack_ID&",'"&content&"'"推荐精选 else response.write "<script>alert('所需数据为空,请填写')</script>" response.write"<script>history.go(-1)</script>" end ifend ifresponse.redirect("List.asp")%> 下载这四个ASP文件。六、总结好了,到这里这个简单的留言板就做完了。当然里面还有很多要改进的地方,比仿说列表页要分页。回复的内容太长的话,回复递进的效果就不明显。没有过滤html代码javascript代码。没有管理后台等等。不过如果你能做出这个留言板,只要再增强一下功能和安全,做出一个像样的留言板那应该是没问题的。 (注:可编辑下载,若有不当之处,请指正,谢谢!) 推荐精选

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1