利用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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM