ASP中有關(guān)數(shù)據(jù)庫的操作語法總結(jié)
語法:Insert into table(field1,field2,....) Values (value1,value2,....)
例子:添加一作者是"cancer"的記錄入book表:
insert into book (bookno,author,bookname) values (’CF001’,’cancer’,’Cancer無組件上傳程序’)
同樣,如果用到變量就如下:
strno=request.form("bookno")
strau=request.form("author")
strname=request.form("bookname")
strsql="insert into book (bookno,author,bookname) values (’"&strno&"’,’"&strau&"’,’"&strname&"’)"
3.用Recordset對(duì)象的Addnew插入數(shù)據(jù)的方法:
語法:
rs.addnew
rs("field1").value=value1
rs("field2").value=value2
...
rs.update
4.修改數(shù)據(jù)記錄(Update)
語法:update table set field1=value1,field2=value2,...where fieldx=valuex
例子:update book set author=’babycrazy’ where bookno=’CF001’
如果用到變量就如下:
strno=request.form("bookno")
strau=request.form("author")
strsql="update book set author=’"&strau&"’ where bookno=’"&strno"’"
5.Recordset對(duì)象的Update方法:
語法:
rs("field1").value=value1
rs("field2").value=value2
...
rs.update
注意:使用語法3和語法5的時(shí)候,一定要注意字段的類型(尤其是日期型)一致,否則出錯(cuò)的幾率非常的高。
例子:
strno=request.form("bookno")
strau=request.form("author")
set adocon=server.createobject("adodb.connection")
adocon.open "Driver={Microsoft Access Driver(*.mdb)};DBQ=" & _
Server.Mappath=("/cancer/cancer.mdb")
strsql="select * from book where bookno=’"&strno&"’"
set rs=server.createobject("adodb.recordset")
rs.open strsql,adconn,1,3
if not rs.eof then ’如果有此記錄的話
rs("author").value=strau
rs.update
end if
rs.close
set rs=nothing
adocon.close
set adocon=nothing
6.刪除一條記錄(Delete)
語法:Delete table where field=value
例子:刪除book表中作者是cancer的記錄
delete book where author=’cancer’
(注意:如果book表中author字段的值為cancer的記錄有多條,將會(huì)刪除所有author為cancer的記錄)
好了,學(xué)會(huì)了用這些操作,大家在用asp操作數(shù)據(jù)庫的時(shí)候,該是沒有什么問題了。