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


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

    正文:

    一、创建数据库:

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

    Create Database feedback --创建数据库feedback

    On {语法错误?}

    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、创建第一个表: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

    )

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

    Insert into Feedback

    (Title,Content)

    values

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

    GO

    select * from Feedback

    3、创建第二表: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

    )

     三、创建两个触发器

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

    当删除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)

    2、第二个触发器:

    当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,触发器的写法和上面一相似,为减短教程,就不在增加!

    四、建立两个存储过程用来保存增加的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)

    五、建立asp文件,完成留言板制作!

    1、创建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(如果也是本机不用修改),数据库用户名,密码!

    %>

    2、创建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文件,这时如果数据库有有数据,那么网页中就可以显示数据了,如果没有内容网页显示“无记录”,下边显示增加表单。

    3、创建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>

    4、创建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

  • 相关阅读:
    shell的一本书
    linux设置网络三种方法
    BIOS讲解
    对于ssh和hadoop联系讲解和ssh的基本内容
    Httphandler
    ASP.NET配置文件
    Httpmoudle
    ASP.NET页面生命周期
    ASP.NET页面跳转方法的集合
    OutputCache的使用
  • 原文地址:https://www.cnblogs.com/apf20010807/p/12882022.html
Copyright © 2020-2023  润新知