源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
查看: 429|回复: 28

[ASP编程] javascript asp教程创建数据库连接

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2007-3-2 00:00:00 | 显示全部楼层 |阅读模式

While this section is devoted to ASP database utilization, it very important to remember that this web site is not intended to be a thorough ASP resource. Remember, the focus of this site is strictly limited to how to use JavaScript as your primary scripting language for ASP.

You'll see how to construct connection strings in JavaScript, use JavaScript loops to manipulate recordsets, convert the JavaScript Date Object into a format that databases can accept, and to some extent you'll see how to make SQL statements in JavaScript.

You can find a lot of good resources on database utilization. Those resources, coupled with the next four lessons, will be everything you need to write ASP JavaScript database applications.

The Connection Object:

The connection object is the link between the database and your ASP script. Remember, it's a created or instanciated object, so we can have two or more instances of Connection on one page. Connection has eight (8) methods, eleven (11) properties, nine (9) events, and finally it has two (2) properties. We will discuss three of the methods, one of the properties, and then we will forego the rest.

There are four common connections. 1) MDL 2) DSN 3) ODBC 4) OLE-DB. MDL stands for Microsoft Data Link. Don't use it. Also, please don't use a DSN; it's slow and outdated and nobody recommends it. ODBC is better, but it's not the best. The recommended connection type is OLE-DB. That's the type of connection you'll see demonstrated below.

Get Started:

Below is the script for Lesson 16. Don't try to understand it yet. We will slowly pick this thing apart down below.

<%@LANGUAGE="JavaScript"%>
<!-- METADATA TYPE="typelib" 
FILE="C:\Program Files\Common Files\System\ado\msado15.dll" -->
<HTML>
<BODY>
<%
var myConnect = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="; 
myConnect += Server.MapPath("\\");
myConnect += "\\GlobalScripts\\htmlColor.mdb;";

var ConnectObj = Server.CreateObject("ADODB.Connection");
var RS = Server.CreateObject("ADODB.Recordset");
var sql="SELECT ID, colorName, hexValue FROM colorChart;";

ConnectObj.Open (myConnect);
RS.Open(sql,ConnectObj,adOpenForwardOnly,adLockReadOnly,adCmdText);

Response.Write("<TABLE BORDER=\"1\" CELLSPACING=\"0\">\r");
Response.Write("<TR><TH>ID</TH><TH>colorName</TH>");
Response.Write("<TH>hexValue</TH></TR>\r");
while (!RS.EOF)
	{
	Response.Write("<TR><TD>" +RS("ID")+ "</TD><TD BGCOLOR=\"#");
	Response.Write( RS("hexValue")+ "\">" + RS("colorName") );
	Response.Write("</TD><TD>" +RS("hexValue")+ "</TD></TR>\r");
	RS.MoveNext();
	}
Response.Write("</TABLE>\r");

RS.Close();
ConnectObj.Close();
RS = null;
ConnectObj = null;
%>
</BODY>
</HTML>

Click Here to run the script in a new window.

Connection String:

This is by no means the most sophisticated database application ever built, but it will demonstrate everything we need to do. Let's start by looking at the connection string reprinted below.

var myConnect = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="; 
myConnect += Server.MapPath("\\ASP") 
myConnect += "\\GlobalScripts\\htmlColor.mdb;";

That does look different than a VBScript connection string. As a matter of fact, let's compare.

Dim myVBconnect;
myVBconnect = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=";
myVBconnect += Server.MapPath("\ASP") 
myVBconnect += "\GlobalScripts\htmlColor.mdb;";

We already talked about escape characters in lesson 02. We won't revisit them here. Down below you'll see that we use myConnect as an argument in the Open() method.

Managing the Connection:

I want you to pay attention to the next four lines of code that I reprinted below. First we instanciate a Connection Object.

var ConnectObj = Server.CreateObject("ADODB.Connection");

Then we open the Connection.

ConnectObj.Open (myConnect);

Then the Connection Object becomes the second argument in the Recordset Open() method.

RS.Open(sql,ConnectObj,adOpenForwardOnly,adLockReadOnly,adCmdText);

And lastly, when we are finished with the Connection, we close it.

ConnectObj.Close();

Next Up:

There is a lot of code left unexplained in this example. We'll repeat the same script in lesson 17 and go over most of what we left out the first time through.

回复

使用道具 举报

2

主题

2万

回帖

347

积分

中级会员

Rank: 3Rank: 3

积分
347
发表于 2022-10-8 00:02:28 | 显示全部楼层
而快乐你们快乐马年快乐
回复 支持 反对

使用道具 举报

4

主题

2万

回帖

58

积分

注册会员

Rank: 2

积分
58
发表于 2022-11-20 19:18:10 | 显示全部楼层
啊,数码撒飒飒飒飒
回复 支持 反对

使用道具 举报

7

主题

2万

回帖

288

积分

中级会员

Rank: 3Rank: 3

积分
288
发表于 2022-12-20 03:29:46 | 显示全部楼层
快更新啊,我擦
回复 支持 反对

使用道具 举报

13

主题

2万

回帖

85

积分

注册会员

Rank: 2

积分
85
发表于 2023-2-6 23:09:23 | 显示全部楼层
先把创新班才能下班才能下班
回复 支持 反对

使用道具 举报

6

主题

1万

回帖

174

积分

注册会员

Rank: 2

积分
174
发表于 2023-4-28 00:12:13 | 显示全部楼层
你们谁看了弄洒了可能
回复 支持 反对

使用道具 举报

6

主题

1万

回帖

174

积分

注册会员

Rank: 2

积分
174
发表于 2023-7-1 03:04:39 | 显示全部楼层
很不错的样子
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-7-10 07:54:24 | 显示全部楼层
很不错的源码论坛
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-9-17 04:14:36 | 显示全部楼层
刷屏刷屏刷屏
回复 支持 反对

使用道具 举报

2

主题

2万

回帖

499

积分

中级会员

Rank: 3Rank: 3

积分
499
发表于 2023-9-17 14:33:29 | 显示全部楼层
天天源码论坛
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2024-11-22 16:17 , Processed in 0.084694 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表