ASP中使用SQL语句操作数据库

发表于:2007-06-30来源:作者:点击数: 标签:
用ASP操作 数据库 时,很多人都使用记录集,但我觉得用 SQL 语句更好些,于是就写了这篇文章,希望对你有帮助。 % @#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#

用ASP操作数据库时,很多人都使用记录集,但我觉得用SQL语句更好些,于是就写了这篇文章,希望对你有帮助。

<%
@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#
@#以下段为连接数据库和断开连接(数据库为Aclearcase/" target="_blank" >ccess格式,采用DSN来连接)
@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#
sub connect()
dsn="FILEDSN=acces.dsn" @#dacces.dsn为mdb文件的dsn,在配置ODBC时建立,如果要上传文件的话,可以把access.dsn一同上传
set cnn=server.createobject("ADODB.Connection") @#建立ADO对象
cnn.open dsn @#打开当前目录下的dsn文件(dsn为access文件的dsn)
response.write "数据库连接成功:" & cnn.state & "<br>"
cnn.close @#关闭连接
response.write"数据库已经关闭" & cnn.state & "<br>"
set cnn=nothing @#清除变量
end sub

@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#
@#以下段为操纵数据库(查询内容)
@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#
sub sel()
dsn="FILEDSN=acces.dsn" @#dacces.dsn为mdb文件的dsn
set cnn=server.createobject("ADODB.Connection") @#建立ADO对象
cnn.open dsn @#打开当前目录下的dsn文件(dsn为access文件的dsn)
@#送出操作,并得到结果
sql="select * from user" @#构造SQL语句
set rs=cnn.Execute(sql) @#送出SQL
@#一个循环把记录集中的记录内容全部输出
while not rs.eof
values=rs("name")
response.write values & "<br>"
rs.movenext
wend
rs.close
cnn.close
set cnn=nothing
end sub

@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#
@#以下段为操纵数据库(修改内容)
@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#
sub modi()
dsn="FILEDSN=acces.dsn" @#dacces.dsn为mdb文件的dsn
set cnn=server.createobject("ADODB.Connection") @#建立ADO对象
cnn.open dsn @#打开当前目录下的dsn文件(dsn为access文件的dsn)
sql="update user set name=@#这是程序@# where name=@#A@#" @#构造修改SQL语句
cnn.Execute sql @#送出SQL
cnn.close
set cnn=nothing
end sub

@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#
@#以下段为操纵数据库(删除内容)
@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#
sub del()
dsn="FILEDSN=acces.dsn" @#dacces.dsn为mdb文件的dsn
set cnn=server.createobject("ADODB.Connection") @#建立ADO对象
cnn.open dsn @#打开当前目录下的dsn文件(dsn为access文件的dsn)
sql="delete from user where name=@#B@#" @#构造删除SQL语句
cnn.Execute sql @#送出SQL
cnn.close
set cnn=nothing
end sub


@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#
@#以下段为操纵数据库(添加内容)
@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#
sub add()
dsn="FILEDSN=acces.dsn" @#dacces.dsn为mdb文件的dsn
set cnn=server.createobject("ADODB.Connection") @#建立ADO对象
cnn.open dsn @#打开当前目录下的dsn文件(dsn为access文件的dsn)
@#sql="insert into user (name,sex,note) values (@#tt@#,@#tt@#,@#tt@#)" @#构造添加SQL语句
@#cnn.Execute sql @#送出SQL
cnn.close
set cnn=nothing
end sub
%>

原文转自:http://www.ltesting.net