利用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
利用Asp.net和Sql Server实现留言板功能的更多相关文章
- 利用反馈字段给帝国cms添加留言板功能(图文教程)
帝国cms的插件中提供信息反馈字段,很多人却不会用.这里谢寒教大家如何来给自己的帝国cms网站添加留言板功能 1.找到添加地址 2.添加字段 3.你可以在字段中添加多种字段类型(有文本域,单行文本框, ...
- ASP.NET 连接 SQL Server 和 Oracle 教程
临近期末,有很多同学都问我怎么关于ASP.NET 连接 SQL Server 和 Oracle 的问题.由于人太多了,我也不能一一去帮忙,就写了这篇博客.希望对大家有用处. 首先,前期准备是要安装数据 ...
- 利用Ring Buffer在SQL Server 2008中进行连接故障排除
原文:利用Ring Buffer在SQL Server 2008中进行连接故障排除 出自:http://blogs.msdn.com/b/apgcdsd/archive/2011/11/21/ring ...
- Python和SQL Server 2017的强大功能
Python和SQL Server 2017的强大功能 摘要: 源:https://www.red-gate.com/simple-talk/sql/sql-development/power-pyt ...
- c#直接调用ssis包实现Sql Server的数据导入功能
调用ssis包实现Sql Server的数据导入功能网上已经有很多人讨论过,自己参考后也动手实现了一下,上一次笔者的项目中还用了一下这个功能.思前想后,决定还是贴一下增强记忆,高手请54. 1.直接调 ...
- Web开发从零单排之二:在自制电子请帖中添加留言板功能,SAE+PHP+MySql
在上一篇博客中介绍怎样在SAE平台搭建一个html5的电子请帖网站,收到很多反馈,也有很多人送上婚礼的祝福,十分感谢! web开发从零学起,记录自己学习过程,各种前端大神们可以绕道不要围观啦 大婚将至 ...
- php实现留言板功能
这个小小的留言板功能适合班级内或者公司内部之间的讨论,对话和留言,非常的方便,更重要的是无需网络,对于公司管理层来说是非常乐于常见的, 下面是这个留言板的写法: 1 首先是登录页面: <form ...
- 使用PHP连接数据库实现留言板功能
PHP实现留言板功能: 1 首先是登录页面: <!DOCTYPE html><html> <head> <meta charset=&qu ...
- jsp中运用application实现共享留言板功能
jsp中application的知识点总结: 1.一个Web应用程序启动后,将会自动创建一个application对象,在整个应用程序的运行过程中只有这一个application对象,即所有访问该网站 ...
随机推荐
- D - Complete Tripartite
三分图染色 链接:https://codeforces.com/contest/1228/problem/D 三分图染色步骤:First 首先找一个点1作为集合A中的点,再找到与1相连的一个点设为2, ...
- Python巩固 - 第N天
一.函数解释: def fact(n, m = 1): s = 1 for j in range(1, n+1): s = s*j return n, m, s//m print(fact(10, 5 ...
- Maven+JSP+SSM+Mysql+C3P0实现的学生管理系统
项目简介 项目来源于:https://gitee.com/wu_yun_long/student_management_system 本系统是基于Maven+JSP+SSM+Mysql+C3P0实现的 ...
- 如何将Python项目发布到PyPI
The Python Package Index (PyPI) is a repository of software for the Python programming language. 如何打 ...
- .NET Core 3 WPF MVVM框架 Prism系列文章索引
.NET Core 3 WPF MVVM框架 Prism系列之数据绑定 .NET Core 3 WPF MVVM框架 Prism系列之命令 .NET Core 3 WPF MVVM框架 Prism系列 ...
- VIM 批量缩进4个空格
vim /etc/vimrc 或 vim ~/.vimrc set smartindent set shiftwidth= 按v选中多行,回车 然后shifit + >
- nginx开启ssl并把http重定向到https的两种方式
1 简介 Nginx是一个非常强大和流行的高性能Web服务器.本文讲解Nginx如何整合https并将http重定向到https. https相关文章如下: (1)Springboot整合https原 ...
- 数据之路 - Python爬虫 - 动态页面
一.Ajax数据爬取 1.Ajax介绍 Ajax,全称为Asynchronous JavaScript and XML,即异步的JavaScript和XML. 它不是一门编程语言,而是利用JavaSc ...
- P1886 滑动窗口 单调队列
题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值. 例如: The array i ...
- Geomesa-Hbase集群部署
本文记录一下Geomesa-Hbase集群部署,在单机部署的基础上 https://www.cnblogs.com/help-silence/p/12817447.html 1.搭建集群 https: ...