利用Asp.net和Sql Server实现留言板功能

本教程设及到:使用SQL Server查询分析器建立数据库;SQL查询语句经常使用的一些属性值;触发器建立和使用;存储过程的建立,ASP使用存储过程。javascript

正文:java

1、建立数据库:sql

建立一个feedback数据库,该数据库的主数据文件的逻辑名称是feedback,操做系统文件是feedback.mdf数据库

 

Create Database feedback --建立数据库feedbackide

 

On {语法错误?}函数

 

Primary (oop

Name=feedback,post

Filename='d:\feedback.mdf',   --数据库操做系统文件的目录和名称spa

Size=15MB,操作系统

Maxsize=30MB,

Filegrowth=20%)

 

Log On

 

(Name=feedback_log,

Filename='d:\feedback.ldf',

Size=3MB,

Maxsize=10MB,

FileGrowth=1MB)

 

USE feedback   --打开数据库

2、建立两个表,一个用来做留言,一个做留言的回复!

 

一、建立第一个表:Feedback存放留言的记录!

 

Drop Table Feedback  --若是已经有此表将其删除,第一次建立,不用这句!

 

GO

 

Create 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  --留言内容,类型文本字段,不能为空

 subFeedback_count  int default 0 --字段subFeedback_count 回复的条数!默认值0

)

 

二、插入一条新记录,并显示出来

 

Insert into Feedback

(Title,Content)

values

('here is Title','This is a test')

 

GO

 

select * from Feedback

 

 

三、建立第二表:subFeedback存放留言的回复

 

Create Table subFeedback

(

 subFeedback_ID int Primary Key identity(1,1) Not Null,

 Feedback_ID int Foreign key references Feedback(Feedback_ID),

 --定义外键关联到表Feedback的主键Feedback_ID

 Content text Not Null

)

 3、建立两个触发器

 

一、第一个触发器(级联删除触发器):

当删除Feedback表中的记录时,自动删除subFeedback中外键对应相同的全部记录 Create Trigger Trigger_delete_Feedback

ON Feedback

--在表feedback上建触发器Trigger_delete_Feedback

Instead OF  Delete        

--INSTEAD OF 触发器表示并不执行其所定义的

操做(INSERT、 UPDATE、 DELETE),而仅是执行触发器自己

--或者说发生Delete事件时执行,该触发器AS后语名会替换过delete语句的执行

 

AS

Delete From subFeedback where Feedback_ID in(select Feedback_ID from deleted)

--删除表subFeedback外键与删除feedback主键相同的值

Delete From Feedback where Feedback_ID in(select Feedback_ID from deleted)

 

 

 

二、第二个触发器:

当subFeedback有新增记录时,Feedback.subFeedback_count字段记数增长! Create Trigger Trigger_update_subFeedback

ON subFeedback

For insert   

--注间和Instead OF的区别,For是当insert语句执行完后再执行解发器AS后的语句

 

AS

update Feedback set subFeedback_count=subFeedback_count+1 where Feedback_ID in(select Feedback_ID from inserted)

 

 

    另外:若是考虑的较周全点,当subFeedback中的记录删除时,Feedback_subFeedback_count字段还要减1,触发器的写法和上面一类似,为减短教程,就不在增长!

 

4、创建两个存储过程用来保存增长的Feedback和subFeedback记录 

 

Create Procedure proc_insert_Feedback --建立存储过程proc_insert_Feedback

@Title nvarChar(256),@Content text  --定义参数变量

AS

Insert into Feedback (Title,Content) values(@Title,@Content) --执行语句

 

GO

 

Create Procedure proc_insert_subFeedback

@Feedback_ID int,@Content text

AS

Insert into subFeedback (Feedback_ID,Content) values(@Feedback_ID,@Content)

 

 

5、创建asp文件,完成留言板制做!

 

一、建立conn.asp文件,与数据库链接。

 <%

dim conn

set conn=Server.createobject("ADODB.CONNECTION")      '建立链接对象

 

conn.open="Provider=SQLOLEDB; Data Source=127.0.0.1;" & _

"Initial Catalog=Feedback; User ID=sa; password=sa;"      

'打开链接。换成你的server-IP(若是也是本机不用修改),数据库用户名,密码!

%>

 

二、建立List.asp显示留言,内容。

这里我把增长的 Form 也加到了文件底部,减小文件的个数。 <!--#include file="conn.asp"--><!--用include file包含数据库链接文件。-->

 

<%

SQL="select * from Feedback"

Set rs=Server.CreateObject("ADODB.Recordset")      '建立数据集rs

rs.open SQL,conn,1,3   '打开

 

if not rs.eof then

 

 output=""   '定义字符串变量output,输出

 do while not rs.eof           '外循环开始

 

 output=output&rs("title")

 output=output&"--<a href=Feedback.asp?feedback_ID="&rs("feedback_ID")&"&title="&rs("title")&">回复该留言</a>["&cstr(rs("subFeedback_count"))&"]<hr>"

'创建回复留言的连接,并把要回复的留言的记录Feedback_ID和Title传给Feedback.asp

'Feedback用来标志是回复了哪条记录,增长数据库用!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.open 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("content")

    output=output&"</span><br>"

    j=j+1

    rsSub.movenext

    loop

   end if

 output=output&"<br>"

 rs.movenext

 loop

response.write output

else

response.write "无记录!"

end if

rs.close

set rs=nothing

%>

 

 

<script>

 

 

function chkform(){

//这个函数用来判断输入是否为空

//固然这里的判断还远远不够,比仿说还要判断字符的多少,是否有非法字符等

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="javascript: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">

<!--上面是一个隐藏域,传递一个名为table,值为Feedback变量,让add.asp知道是编辑的Feedback表-->

<input type="submit" name=submit value="   提   交   ">

</form>

 

    经过上面的list.asp文件,这时若是数据库有有数据,那么网页中就能够显示数据了,若是没有内容网页显示“无记录”,下边显示增长表单。

 

三、建立Feedback.asp文件,用来填写留言的回复!

回复:<%=request("title")%>

<form name="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" name="Feedback_ID" value='<%=request.QueryString("Feedback_ID")%>'>

<input type="submit" name=submit value="   提   交   ">

</form>

 

 

四、建立add.asp文件,用来分别保存时Feedback,subFeedback的两个表的增长记录!

这里请注意ASP调用SQL SERVER的存储过程的方法,会让程序变的很简洁! <!--#include file="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<>"" then

                     Conn.Execute "proc_insert_Feedback '"&title&"','"&content&"'"

             else

                     response.write "<script>alert('所需数据为空,请填写')</script>"

                     response.write"<script>history.go(-1)</script>"

                     response.end

             end if

elseif 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 "&Feedback_ID&",'"&content&"'"

             else

                     response.write "<script>alert('所需数据为空,请填写')</script>"

                     response.write"<script>history.go(-1)</script>"

           end if

end if

response.redirect("List.asp")

%>

 

下载这四个ASP文件。

 

 

转载于:baisichen

https://me.csdn.net/baisichen

相关文章
相关标签/搜索