Table of Contents
最近参与了一个ASP项目,而且这也是自己第一次进行web方面的编程;作为3P之一的ASP应该是很老的技术了,但这并妨碍自己的积累和学习,尤其是web编程方面。在这里我想跟大家分享下在ASP服务端如何通过ADO访问ACCESS和SQL SERVER:
ADO是一个访问数据库中数据的编程接口,是微软的一个Active-x组件,会随IIS被自动安装;在做ASP编程的时候,安装IIS(即Internet informations service)是必不可少的;
首先确保OS上已正确安装了ACCESS和SQL SERVER,由于家里电脑上没有装SQL SERVER,下面以在xp平台上访问ACCESS2003数据库为例来测试下自己写的几个数据库访问函数
1.通过.udl文件获取数据库连接字符串
2.通过连接字符串创建相关数据库操作函数
3、对创建的数据库函数进行测试,这里以GXY_DB1数据库下的表Test_table为例,该表的各字段及全部记录如图-4和图-5所示:
4.总结:
Home Database Mysql Tutorial ASP中通过ADO访问ACCESS和SQL SERVER

ASP中通过ADO访问ACCESS和SQL SERVER

Jun 07, 2016 pm 03:39 PM
access asp sql access pass

最近参与了一个ASP项目,而且这也是自己第一次进行web方面的编程;作为3P之一的ASP应该是很老的技术了,但这并妨碍自己的积累和学习,尤其是web编程方面。在这里我想跟大家分享下在ASP服务端如何 通过 ADO 访问 ACCESS和SQL SERVER: ADO是一个 访问 数据库中

最近参与了一个ASP项目,而且这也是自己第一次进行web方面的编程;作为3P之一的ASP应该是很老的技术了,但这并妨碍自己的积累和学习,尤其是web编程方面。在这里我想跟大家分享下在ASP服务端如何通过ADO访问ACCESS和SQL SERVER:

ADO是一个访问数据库中数据的编程接口,是微软的一个Active-x组件,会随IIS被自动安装;在做ASP编程的时候,安装IIS(即Internet informations service)是必不可少的;

首先确保OS上已正确安装了ACCESS和SQL SERVER,由于家里电脑上没有装SQL SERVER,下面以在xp平台上访问ACCESS2003数据库为例来测试下自己写的几个数据库访问函数

1.通过.udl文件获取数据库连接字符串

1)新建一个txt文件,并修改后缀名为.udl;直接双击打开该udl文件后我们就可以根据需要获取特定数据库的连接字符串了;打开udl后的界面如图-1所示:

ASP中通过ADO访问ACCESS和SQL SERVER

图-1

2)在这里我们选择Microsoft.Jet.OLEDB.4.0数据库提供程序来访问access,点击下一步后界面如图-2所示:

ASP中通过ADO访问ACCESS和SQL SERVER

图-2

3)输入或选择数据库所在路径后,其它选项保持默认,点击测试连接后,没有问题的情况下会弹出提示【测试连接成功】的消息框;

这时,我们点击确定后用UE或者其它文本编辑工具打开该udl文件后,即可获取连接该access数据库的连接字符串;

如图-3所示:这里的连接字符串即为:Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\IIS\webapps\My_Test\GXY_DB1.mdb;Persist Security Info=False

ASP中通过ADO访问ACCESS和SQL SERVER

图-3

2.通过连接字符串创建相关数据库操作函数

1.)新建ProDatBase.asp文件,并插入空的ASP代码段;在该代码段中声明三个全局变量;分别用来存储ADO记录集对象、ADO连接对象、以及连接字符串;

将上面获取的连接字符串赋值给g_ConStr

<p><span>Dim g_Rs,g_Con,g_ConStr</span></p><p><span>g_ConStr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\IIS\webapps\My_Test\GXY_DB1.mdb;Persist Security Info=False"</span></p>
Copy after login
       2.)创建数据连接和断开连接函数

'连接数据库
function ConnectDataBase()
	set g_Con=server.CreateObject("ADODB.connection")
	on error resume next
	g_Con.mode=3           '将连接模式设为可读取写
	g_Con.open g_ConStr
	if err0 then
	ConnectDataBase=false
	response.Write(err.Description)
	else 
	ConnectDataBase=true
	end if
end function

'断开数据库连接
function DisconnectDataBase()
	on error resume next
	g_Con.close
	set g_Con=nothing
	if err0 then
	DisconnectDataBase=false
	response.Write(err.Description)
	else 
	DisconnectDataBase=true
	end if
end function
Copy after login
3.)创建插入记录函数
'插入记录
function InsertRecord(table,sqlFields,sqlValues)
if ConnectDataBase() then
	sql="insert into ["+table+"] ("+sqlFields+") values ("+sqlValues+") "
	on error resume next
	g_Con.Execute sql
	if err 0 then
	response.Write(err.Description)
	end if
	DisconnectDataBase()
end if
end function
Copy after login

4.)创建更新记录函数

'更新记录
function UpdateRecord(table,sqlFields,sqlValues,strCondition)
if ConnectDataBase() then
	sql="update ["+table+"] set "
	sql_fd=split(sqlFields,",")
	sql_fv=split(sqlValues,",")
	for i=0 to ubound(sql_fd)
	if iubound(sql_fd) then
	sql=sql & ""&sql_fd(i)&"="&sql_fv(i)&","
	else
	sql=sql & ""&sql_fd(i)&"="&sql_fv(i)&""
	end if
	next
	sql=sql&" where "&strCondition&""
	on error resume next
	g_Con.Execute sql
	if err 0 then
	response.Write(err.Description)
	end if
	DisconnectDataBase()
end if
end function
Copy after login

5.)创建删除记录函数

'根据条件删除记录
function DeleteRecord(table,strCondition)
if ConnectDataBase() then
	sql="delete from "+table+" where "&strCondition&" "
	on error resume next
	g_Con.Execute sql
	if err 0 then
	response.Write(err.description)
	end if
	DisconnectDataBase()
end if
end function
Copy after login

6.)创建记录获取函数

'根据条件获取记录
function GetRecords(table,strCondition)
if ConnectDataBase() then
	sql="select * from ["+table+"]"
	if strCondition "" then
	sql=sql&" where "&strCondition&""
	end if
	on error resume next
	set g_Rs=Server.CreateObject("ADODB.recordset")
	g_Rs.Open sql,g_Con
	if err 0 then
	response.Write(err.description)
	end if
end if
end function
Copy after login

7.)创建资源释放函数

function ReleaseResource()
on error resume next
g_Rs.close
set g_Rs=nothing
DisconnectDataBase()
if err0 then
response.Write(err.description)
end if
end function
Copy after login

3、对创建的数据库函数进行测试,这里以GXY_DB1数据库下的表Test_table为例,该表的各字段及全部记录如图-4和图-5所示:

ASP中通过ADO访问ACCESS和SQL SERVERASP中通过ADO访问ACCESS和SQL SERVER

-4(id自动增长)                                                                     图-5(没有记录)


准备:新建ProDataBase_Test.asp文件,添加包含ProDataBase.asp文件的语句,插入空的asp代码块

       1.)插入记录函数测试:插入10条记录,name和age依次从name0和15岁到name9和24岁

测试代码:

Copy after login

测试结果如图-6所示:

ASP中通过ADO访问ACCESS和SQL SERVER

-6

      2.)获取记录函数测试:获取age在18到23之间的记录并显示在网页上

测试代码:

<div align="center">
Test_Table
<table border="2" bgcolor="#99FFFF">
")
for each x in g_Rs.fields
   response.Write("<th>")
   response.write(x.name)
    response.Write("</th>")
next 
response.Write("")
do until g_Rs.eof
response.Write("<tr>")
for each x in g_Rs.fields
   response.Write("<td width="90">")
   response.write(x.value)
   response.Write("</td>")
next 
   response.Write("</tr>")
   g_Rs.movenext
loop
ReleaseResource()
%>
</table>
</div>
Copy after login
Copy after login

测试结果如图-7所示:

ASP中通过ADO访问ACCESS和SQL SERVER

-7

3.)更新记录函数测试:将name=name7的记录的name改为newname,age改为99岁,并将获取所有记录显示在网页上

测试代码:

<div align="center">
Test_Table
<table border="2" bgcolor="#99FFFF">
")
for each x in g_Rs.fields
   response.Write("<th>")
   response.write(x.name)
    response.Write("</th>")
next 
response.Write("")
do until g_Rs.eof
response.Write("<tr>")
for each x in g_Rs.fields
   response.Write("<td width="90">")
   response.write(x.value)
   response.Write("</td>")
next 
   response.Write("</tr>")
   g_Rs.movenext
loop
ReleaseResource()
%>
</table>
</div>
Copy after login
Copy after login

测试结果如图-8所示:

ASP中通过ADO访问ACCESS和SQL SERVER

图-8

4.)删除记录函数测试:删除age大于18岁的记录,并将获取所有记录显示在网页上

测试代码:

<div align="center">
Test_Table
<table border="2" bgcolor="#99FFFF">
18"
GetRecords 	 "Test_Table",""

response.Write("<tr>")
for each x in g_Rs.fields
   response.Write("<th>")
   response.write(x.name)
    response.Write("</th>")
next 
response.Write("</tr>")
do until g_Rs.eof
response.Write("<tr>")
for each x in g_Rs.fields
   response.Write("<td width="90">")
   response.write(x.value)
   response.Write("</td>")
next 
   response.Write("</tr>")
   g_Rs.movenext
loop
ReleaseResource()
%>
</table>
</div>
Copy after login

测试结果如图-9所示:

ASP中通过ADO访问ACCESS和SQL SERVER

图-9


4.总结:

上述所有函数在win7和xp平台以及access2003和sql server2008 express版(vs2010自带的)上均测试过,可以正常运行;下面提供一个连接sql server2008的连接字符串供参考:(也是通过udl文件创建的,数据提供程序选择sql server native client 10.0)

g_ConStr="Provider=SQLNCLI10.1;Persist Security Info=False;User ID=gxy;Password=54321;Initial Catalog=GXY_DB1;Data Source=(local);
Copy after login
      第一次发博客,文笔又不行,vbscript刚接触,函数写的不够高效及简洁。。。等等。。。。。如有不当及纰漏之处,请各路大牛多多指教
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
How to configure zend for apache How to configure zend for apache Apr 13, 2025 pm 12:57 PM

How to configure Zend in Apache? The steps to configure Zend Framework in an Apache Web Server are as follows: Install Zend Framework and extract it into the Web Server directory. Create a .htaccess file. Create the Zend application directory and add the index.php file. Configure the Zend application (application.ini). Restart the Apache Web server.

Using Dicr/Yii2-Google to integrate Google API in YII2 Using Dicr/Yii2-Google to integrate Google API in YII2 Apr 18, 2025 am 11:54 AM

VprocesserazrabotkiveB-enclosed, Мнепришлостольностьсясзадачейтерациигооглапидляпапакробоглесхетсigootrive. LEAVALLYSUMBALLANCEFRIABLANCEFAUMDOPTOMATIFICATION, ČtookazaLovnetakProsto, Kakaožidal.Posenesko

How to identify malicious access in Debian Apache logs How to identify malicious access in Debian Apache logs Apr 13, 2025 am 07:30 AM

Effective monitoring and defense against malicious website access is crucial to the Apache server on the Debian system. Apache access logs are the key source of information to identify such threats. This article will guide you on how to analyze logs and take defensive measures. The Apache access log that identifies malicious access behaviors Debian systems is usually located in /var/log/apache2/access.log. You can analyze the logs in a variety of ways: Log file location confirmation: First, please confirm the exact location of your Apache access log, which may vary slightly depending on the system configuration. Command line tool analysis: Use grep command to search for specific patterns, such as grep "404"

What is apache server? What is apache server for? What is apache server? What is apache server for? Apr 13, 2025 am 11:57 AM

Apache server is a powerful web server software that acts as a bridge between browsers and website servers. 1. It handles HTTP requests and returns web page content based on requests; 2. Modular design allows extended functions, such as support for SSL encryption and dynamic web pages; 3. Configuration files (such as virtual host configurations) need to be carefully set to avoid security vulnerabilities, and optimize performance parameters, such as thread count and timeout time, in order to build high-performance and secure web applications.

How to solve nginx current limit How to solve nginx current limit Apr 14, 2025 pm 12:06 PM

The Nginx current limit problem can be solved by: use ngx_http_limit_req_module to limit the number of requests; use ngx_http_limit_conn_module to limit the number of connections; use third-party modules (ngx_http_limit_connections_module, ngx_http_limit_rate_module, ngx_http_access_module) to implement more current limit policies; use cloud services (Cloudflare, Google Cloud Rate Limiting, AWS WAF) to DD

How to use Debian Apache logs to improve website performance How to use Debian Apache logs to improve website performance Apr 12, 2025 pm 11:36 PM

This article will explain how to improve website performance by analyzing Apache logs under the Debian system. 1. Log Analysis Basics Apache log records the detailed information of all HTTP requests, including IP address, timestamp, request URL, HTTP method and response code. In Debian systems, these logs are usually located in the /var/log/apache2/access.log and /var/log/apache2/error.log directories. Understanding the log structure is the first step in effective analysis. 2. Log analysis tool You can use a variety of tools to analyze Apache logs: Command line tools: grep, awk, sed and other command line tools.

Nginx performance monitoring and troubleshooting tools Nginx performance monitoring and troubleshooting tools Apr 13, 2025 pm 10:00 PM

Nginx performance monitoring and troubleshooting are mainly carried out through the following steps: 1. Use nginx-V to view version information, and enable the stub_status module to monitor the number of active connections, requests and cache hit rate; 2. Use top command to monitor system resource occupation, iostat and vmstat monitor disk I/O and memory usage respectively; 3. Use tcpdump to capture packets to analyze network traffic and troubleshoot network connection problems; 4. Properly configure the number of worker processes to avoid insufficient concurrent processing capabilities or excessive process context switching overhead; 5. Correctly configure Nginx cache to avoid improper cache size settings; 6. By analyzing Nginx logs, such as using awk and grep commands or ELK

The Purpose of SQL: Interacting with MySQL Databases The Purpose of SQL: Interacting with MySQL Databases Apr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

See all articles