JavaScript ASP Tutorial Create database connection _asp Basics

Source: Internet
Author: User
Tags dsn odbc ole

While the is devoted to ASP database utilization, it very important to remember so this web site isn't not Intende D to be a thorough ASP resource. Remember, the focus of this site are strictly limited to the "to" use JavaScript as your primary scripting language for ASP.

You'll be construct connection strings in JavaScript, use JavaScript loops to manipulate recordsets, convert the J Avascript Date Object into a format which is databases can accept, and to some extent you ' ll-i-i-SQL statements in Javascript.

You can find a lot of good resources on the database utilization. Those, coupled with the next four lessons, 'll be everything your need to write ASP JavaScript database applicat ions.

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 of the Connection on one page. Connection has eight (8) methods, Eleven (one) properties, nine (9) events, and finally it has two (2) properties. We'll discuss three of the methods, one of the properties, and then we'll 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. The ODBC is better, but it's not the best. The recommended connection type is ole-db. That's the type of connection you'll be demonstrated below.

Get started:

Below is the script for Lesson 16. Don ' t try to understand it yet. We'll 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 are by no means the most sophisticated database application ever built it'll but demonstrate we everything 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's 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 be here we use Myconnect as a 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. 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 the code left unexplained in this example. We'll repeat the same script in lesson and go over most of what we left out the the '

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.