ASP数据库语法总结

上一篇 / 下一篇  2007-06-09 08:44:31 / 个人分类:网页知识

<一>。数据库的连接方法:

1.Access数据库的DSN-less连接方法:

set adocon=Server.Createobject("adodb.connection")
wf^"hf7BK65703adoconn.Open"Driver={Microsoft Access Driver(*.mdb)};DBQ="& _51Testing软件测试网;ev6|3p#{]x
Server.MapPath("数据库所在路径")

2.Access OLE DB连接方法:

set adocon=Server.Createobject("adodb.connection")51Testing软件测试网'V&M9z-X1e,q8^ Z
adocon.open"Provider=Microsoft.Jet.OLEDB.4.0;"& _
~7ZYXa65703"Data Source=" & Server.MapPath("数据库所在路径")

3.SQL server连接方法:

set adocon=server.createobject("adodb.recordset")
hn$\8rwd"H:_^/y65703adocon.Open"Driver={SQL Server};Server=(Local);UID=***;PWD=***;"& _
r0m(tgH#\q65703"database=数据库名;"

4.SQL server OLE DB连接方法:

set adocon=Server.Createobject("adodb.connection")
o[caC*}65703adocon.open"provider=SQLOLEDB.1;Data Source=RITANT4;"& _51Testing软件测试网9C"O1Z1d kUJg F6n$y
"user ID=***;Password=***;"& _51Testing软件测试网2XIlF6m5h(M#w6R$F'N
"inital Catalog=数据库名"

5.Oracle 连接方法:

set adocon=Server.Createobject("adodb.connection")
.k,I-alLL#D2G65703adocon.open"Driver={microsoft odbc for oracle};server=oraclesever.world;uid=admin;pwd=pass;"

6.Oracle OLE DB 连接方法:

set adocon=Server.Createobject("adodb.connection")
Qp/z}4L4u9o4m65703adocon.open"Provider=OraOLEDB.Oracle;data source=dbname;user id=admin;password=pass;"

7.dBase 连接方法:

set adocon=Server.Createobject("adodb.connection")
K5B \7M&ex s9z6K65703adocon.open"Driver={microsoft dbase driver(*.dbf)};driverid=277;dbq=------------;"

8.mySQL 连接方法:

set adocon=Server.Createobject("adodb.connection")51Testing软件测试网6q Qt S zR&r1F g'ix
adocon.open"Driver={mysql};database=yourdatabase;uid=username;pwd=yourpassword;option=16386;"

9.Visual Foxpro 连接方法:

set adocon=Server.Createobject("adodb.connection")
:fc$w1nDq[n65703adocon.open"Driver={microsoft Visual Foxpro driver};sourcetype=DBC;sourceDB=*.dbc;Exclusive=No;"

10.MS text 连接方法:

set adocon=Server.Createobject("adodb.connection")
.K[C9@6^|65703adocon.open"Driver={microsoft text driver(*.txt; *.csv)};dbq=-----;"&_
@rv8E2y65703"extensions=asc,csv,tab,txt;Persist SecurityInfo=false;"

11.MS text OLE DB 连接方法:

set adocon=Server.Createobject("adodb.connection")
8x}1m5oF_ft2P3U|65703adocon.open"Provider=microsof.jet.oledb.4.0;data source=your_path;"&_
Wzi;n'N/J1d7K/v65703"Extended Properties'text;FMT=Delimited'"

<二>。常用的四种SQL命令:

1.查询数据记录(Select)
5bD%o$c2a65703语法:Select 字段串行 From table Where 字段=内容
+t&g Fa4^4J65703例子:想从book表中找出作者为"cancer"的所有记录,SQL语句便如下:
y+` DQ+vt65703select * from book where author=’cancer’
$t6D4N3NC65703"*"是取出book表所有的字段,如查询的字段值为数字,则其后的"内容"便无须加上单引号,

如是日期,则在Access中用(#)包括,而在SQL server中则用(’)包括,
du|m7C5Y65703如:

select * from book where id=1
"Y'EkmRX5R6bIu E65703select * from book where pub_date=#2002-1-7# (Access)51Testing软件测试网:~V6T6hl0Z7\
select * from book where pub_date=’2002-1-7’ (SQL Server)

提示:51Testing软件测试网1w(t/f1G3V#d"t)Yp
日期函数to_date不是标准sql文,不是所有的数据库适用,所以大家在使用的时候要参考数据库具体语法

另外如果是查询传入的变量,则如下:

strau=request.form("author")
*~M*Vd1gycK0C,m65703strsql="select * from book where author=’"&strau&"’"

如果查询的是数字,则:

intID=request.form("id")51Testing软件测试网/?G/w{;H8LV3LA
strsql="select * from book where id="&intID

在很多数据库中,如:oracle,上面的语句是可以写成:
y/o&G8Mt2S gFK.xt9|65703strsql="select * from book where id='"&intID&"'"的。51Testing软件测试网U,tV%{(uIk$Q2l8aw
但是字符型一定不能按照数字格式写,需要注意。

2.添加记录(Insert)
!Rt'Q9a.D9r.UPp?pD65703语法:Insert into table(field1,field2,....) Values (value1,value2,....)51Testing软件测试网#W(F bk$C,fp
例子:添加一作者是"cancer"的记录入book表:
"L fj7PRX8P@M65703insert into book (bookno,author,bookname) values (’CF001’,’cancer’,’Cancer无组件上传程序’)51Testing软件测试网tp [(T/s3J m[
同样,如果用到变量就如下:

strno=request.form("bookno")
ps7Pj2i/Wy4}3s65703strau=request.form("author")
nx~N)m\Ha9V65703strname=request.form("bookname")
W#mmpXSc65703strsql="insert into book (bookno,author,bookname) values (’"&strno&"’,’"&strau&"’,’"&strname&"’)"

3.用Recordset对象的Addnew插入数据的方法:51Testing软件测试网1y(xv Y9lgf)c*A?
语法:

rs.addnew
3yC)j#F NUFt65703rs("field1").value=value151Testing软件测试网U.?W ~-h8p4i1b
rs("field2").value=value251Testing软件测试网2}nNY*CjY
...
:}t:U"go)P,^u Q65703rs.update

4.修改数据记录(Update)51Testing软件测试网(N]is j+l8A2U
语法:update table set field1=value1,field2=value2,...where fieldx=valuex51Testing软件测试网|#Po+r"Xq"ADf+c%Q
例子:update book set author=’babycrazy’ where bookno=’CF001’
N i [UZz1ntM Q'S65703如果用到变量就如下:

strno=request.form("bookno")51Testing软件测试网%G2\e Hb Q/G
strau=request.form("author")51Testing软件测试网`0@g _ ^B ~d
strsql="update book set author=’"&strau&"’ where bookno=’"&strno"’"

5.Recordset对象的Update方法:51Testing软件测试网f+`M;Hc(K|f R-V nu
语法:

rs("field1").value=value1
tP.mH zM|/l65703rs("field2").value=value2
$`l3ma$E65703...
-\*L}s#U#W+zPZ,w65703rs.update

注意:使用语法3和语法5的时候,一定要注意字段的类型(尤其是日期型)一致,否则出错的几率非常的高。

51Testing软件测试网k9r7u\1`#[ zHL
例子:

strno=request.form("bookno")
c8Aez/c U65703strau=request.form("author")
4jVi ?p hl65703set adocon=server.createobject("adodb.connection")
+~p;B2B f3n65703adocon.open "Driver={Microsoft Access Driver(*.mdb)};DBQ=" & _51Testing软件测试网 w0FOb\5P}R
Server.Mappath=("/cancer/cancer.mdb")51Testing软件测试网;U b(wGb|GC(r
strsql="select * from book where bookno=’"&strno&"’"51Testing软件测试网G CNp3~
set rs=server.createobject("adodb.recordset")
DpyqEx([j65703rs.open strsql,adconn,1,351Testing软件测试网N*?~5g/j[
if not rs.eof then ’如果有此记录的话
9h2~+v%]P/P65703rs("author").value=strau51Testing软件测试网.v9PQP@4W/z)K v
rs.update
*B1g4L'z)V65703end if
2]7X x&IF%Ykbj65703rs.close51Testing软件测试网o Eq| DbX*|&s*T
set rs=nothing51Testing软件测试网$Rb/kO[ g
adocon.close51Testing软件测试网6MQS"A0RK
set adocon=nothing

6.删除一条记录(Delete)
,Bg-QMP65703语法:Delete table where field=value
j Q7{U2O1wY65703例子:删除book表中作者是cancer的记录

delete book where author=’cancer’

(注意:如果book表中author字段的值为cancer的记录有多条,将会删除所有author为cancer的记录)

 


TAG:

 

评分:0

我来说两句

Open Toolbar