javascript asp教程创建数据库连接_ASP基础

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.

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索javascript
asp创建数据库、asp.net创建数据库、asp 创建access数据库、asp 创建sql数据库、asp.net 创建数据库表,以便于您获取更多的相关知识。

时间: 2024-09-25 03:36:34

javascript asp教程创建数据库连接_ASP基础的相关文章

javascript asp教程错误处理_ASP基础

The ASPError Object has zero (0) Methods, nine (9) Properties, zero (0) Events, and zero (0) Collections. AspCode AspDescription Category Column Description File Line Number Source The way you access the ASPError Properties is with a Server Method. Y

javascript asp教程 日期相关_ASP基础

JavaScript is loosely typed. Database files are not. If you put text into a Boolean database column or a Boolean value into a date/time column, then you will get an error. For the most part this is not a problem, except for date/time. It does not cor

javascript asp教程Recordset记录_ASP基础

Recordset is another created/instanciated Object. It is a collection of data taken from a database. Recordset has 26 properties, 25 methods, 11 events, and two (2) collections. The vast majority of Recordset is beyond the scope of this web site. Quic

javascript asp教程服务器对象_ASP基础

Overview: The Server Object has seven (7) Methods, one (1) Property, zero (0) Events, and zero (0) Collections. List of Methods: Server Methods CreateObject( ) Server.CreateObject("ADODB.Recordset")Create an instance of an ObjectExecute( ) Serve

javascript asp教程创建数据库连接

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 s

javascript asp教程More About Recordsets_ASP基础

Below we will attempt to access data from a database without knowing the column names. Clearly the best way to utilize data in your database is to keep track of your schema. Schema is the layout of data in your database. The concept is well beyond th

javascript asp教程第九课--cookies_ASP基础

Response Cookies in General: We'll start with the Response Cookies collection. I don't think it could be any easier. You simply put the name of the cookie in the argument. The corresponding value is a string. The only time it gets complicated is when

javascript asp教程添加和修改_ASP基础

The Connection Execute(): If you want to retrieve data from a database then you have no choice but to use a Recordset. However, for the purposes of adding, updating, and deleting data you don't necessarily have to have a Recordset. It's up to you. Fo

ASP函数大全解析_ASP基础

Array() 函数返回一个数组表达式 Array(list)允许数据类型: 字符,数字均可实例: <% Dim myArray() For i = 1 to 7 Redim Preserve myArray(i) myArray(i) = WeekdayName(i) Next %> 返回结果: 建立了一个包含7个元素的数组myArraymyArray("Sunday","Monday", ... ... "Saturday")