惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
O
OpenAI News
Cloudbric
Cloudbric
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tenable Blog
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
P
Proofpoint News Feed
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
L
LangChain Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东

博客园 - czh

基于MDB_ICP协议的纸币识别器与自动售货机通讯的研究 Mobile Network auto-Connection How could you login to ms sql server if you lost system admin password to setup windows auto-login Compile DLLs referenced into exe/dll 族谱软件系统的使用介绍 javascript在vs2003中调试随笔和javascript使用小总结[转载] 世界互联网正处于剧变前夜! 系统分析师 考试 DVD playable asp.net 页面事件:顺序与回传 创业还是读研? C#判断文件名是否合法 Window消息大全 关于STRONG NAME 好东西 一名25岁的董事长给大学生的18条忠告(不想虚度年华的进来) ZT SQL*PLUS命令的使用大全 如何做一个增加系统DSN的过程
ASP连接11种数据库语法总结
czh · 2006-04-04 · via 博客园 - czh

from:http://www.baist.net/Article/ASPtech/200603/Article_1751.shtml
作者:佚名

经常使用到有关数据库的操作。包括连接代码、SQL命令等等,又不曾刻意去记忆它们(我本人是不愿意去记这东东),所以常常在用到的时候又去查书本,翻来翻去。一些比较少用的数据库还不一定能顺利找到,所以现在把它们全归纳到这里,提供大家参考。 <一>数据库的连接方法

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

以下是代码片段:
set adocon=Server.Createobject("adodb.connection")
adoconn.Open"Driver={Microsoft Access Driver(*.mdb)};DBQ="& _
Server.MapPath("数据库所在路径")

  2.Access OLE DB连接方法:

以下是代码片段:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Provider=Microsoft.Jet.OLEDB.4.0;"& _
"Data Source=" & Server.MapPath("数据库所在路径")

  3.SQL server连接方法:

以下是代码片段:
set adocon=server.createobject("adodb.recordset")
adocon.Open"Driver={SQL Server};Server=(Local);UID=***;PWD=***;"& _
"database=数据库名;"

  4.SQL server OLE DB连接方法:
以下是代码片段:
set adocon=Server.Createobject("adodb.connection")
adocon.open"provider=SQLOLEDB.1;Data Source=RITANT4;"& _
"user ID=***;Password=***;"& _
"inital Catalog=数据库名"

5.Oracle 连接方法:

以下是代码片段:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft odbc for oracle};server=oraclesever.world;uid=admin;pwd=pass;"

  6.Oracle OLE DB 连接方法:

以下是代码片段:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Provider=OraOLEDB.Oracle;data source=dbname;user id=admin;password=pass;"

  7.dBase 连接方法:

以下是代码片段:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft dbase driver(*.dbf)};driverid=277;dbq=------------;"

  8.mySQL 连接方法:

以下是代码片段:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={mysql};database=yourdatabase;
uid=username;pwd=yourpassword;option=16386;"

  9.Visual Foxpro 连接方法:

以下是代码片段:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft Visual Foxpro driver};sourcetype=DBC;sourceDB=*.dbc;Exclusive=No;"

  10.MS text 连接方法:

以下是代码片段:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft text driver(*.txt; *.csv)};dbq=-----;"&_
"extensions=asc,csv,tab,txt;Persist SecurityInfo=false;"

  11.MS text OLE DB 连接方法:
以下是代码片段:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Provider=microsof.jet.oledb.4.0;data source=your_path;"&_
"Extended Properties’text;FMT=Delimited’"

<二>常用的四种SQL命令

  1.查询数据记录(Select)
  语法:Select 字段串行 From table Where 字段=内容
  例子:想从book表中找出作者为"cancer"的所有记录,SQL语句便如下:

以下是代码片段:
select * from book where author=’cancer’

  "*"是取出book表所有的字段,如查询的字段值为数字,则其后的"内容"便无须加上单引号,如是日期,则在Access中用(#)包括,而在SQL server中则用(’)包括,
如:

以下是代码片段:
select * from book where id=1
select * from book where pub_date=#2002-1-7# (Access)
select * from book where pub_date=’2002-1-7’ (SQL Server)


  提示


  日期函数to_date不是标准sql文,不是所有的数据库适用,所以大家在使用的时候要参考数据库具体语法

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

以下是代码片段:
strau=request.form("author")
strsql="select * from book where author=’"&strau&"’"

  如果查询的是数字,则:

以下是代码片段:
intID=request.form("id")
strsql="select * from book where id="&intID

  在很多数据库中,如:oracle,上面的语句是可以写成:

以下是代码片段:
strsql="select * from book where id=’"&intID&"’"


  但是字符型一定不能按照数字格式写,需要注意。

2.添加记录(Insert)
  语法:

以下是代码片段:
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对象的Addnew插入数据的方法:
  语法:

以下是代码片段:
rs.addnew
rs("field1").value=value1
rs("field2").value=value2
...
rs.update


  

4.修改数据记录(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对象的Update方法:
  语法:

以下是代码片段:
rs("field1").value=value1
rs("field2").value=value2
...
rs.update

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

  例子:

以下是代码片段:
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的记录有多条,将会删除所有author为cancer的记录)

  好了,学会了用这些操作,大家在用asp操作数据库的时候,该是没有什么问题了。