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 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.