zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

javascriptasp教程创建数据库连接

数据库教程连接 创建 javascriptasp
2023-06-13 09:13:53 时间

WhilethissectionisdevotedtoASPdatabaseutilization,itveryimportanttorememberthatthiswebsiteisnotintendedtobeathoroughASPresource.Remember,thefocusofthissiteisstrictlylimitedtohowtouseJavaScriptasyourprimaryscriptinglanguageforASP.

You"llseehowtoconstructconnectionstringsinJavaScript,useJavaScriptloopstomanipulaterecordsets,converttheJavaScriptDateObjectintoaformatthatdatabasescanaccept,andtosomeextentyou"llseehowtomakeSQLstatementsinJavaScript.

Youcanfindalotofgoodresourcesondatabaseutilization.Thoseresources,coupledwiththenextfourlessons,willbeeverythingyouneedtowriteASPJavaScriptdatabaseapplications.

TheConnectionObject:

TheconnectionobjectisthelinkbetweenthedatabaseandyourASPscript.Remember,it"sacreatedorinstanciatedobject,sowecanhavetwoormoreinstancesofConnectionononepage.Connectionhaseight(8)methods,eleven(11)properties,nine(9)events,andfinallyithastwo(2)properties.Wewilldiscussthreeofthemethods,oneoftheproperties,andthenwewillforegotherest.

Therearefourcommonconnections.1)MDL2)DSN3)ODBC4)OLE-DB.MDLstandsforMicrosoftDataLink.Don"tuseit.Also,pleasedon"tuseaDSN;it"sslowandoutdatedandnobodyrecommendsit.ODBCisbetter,butit"snotthebest.TherecommendedconnectiontypeisOLE-DB.That"sthetypeofconnectionyou"llseedemonstratedbelow.

GetStarted:

BelowisthescriptforLesson16.Don"ttrytounderstandityet.Wewillslowlypickthisthingapartdownbelow.

<%@LANGUAGE="JavaScript"%>
<!--METADATATYPE="typelib"
FILE="C:\ProgramFiles\CommonFiles\System\ado\msado15.dll"-->
<HTML>
<BODY>
<%
varmyConnect="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=";
myConnect+=Server.MapPath("\\");
myConnect+="\\GlobalScripts\\htmlColor.mdb;";

varConnectObj=Server.CreateObject("ADODB.Connection");
varRS=Server.CreateObject("ADODB.Recordset");
varsql="SELECTID,colorName,hexValueFROMcolorChart;";

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

Response.Write("<TABLEBORDER=\"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><TDBGCOLOR=\"#");
	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>

ClickHeretorunthescriptinanewwindow.

ConnectionString:

Thisisbynomeansthemostsophisticateddatabaseapplicationeverbuilt,butitwilldemonstrateeverythingweneedtodo.Let"sstartbylookingattheconnectionstringreprintedbelow.

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

ThatdoeslookdifferentthanaVBScriptconnectionstring.Asamatteroffact,let"scompare.

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

Wealreadytalkedaboutescapecharactersinlesson02.Wewon"trevisitthemhere.Downbelowyou"llseethatweusemyConnectasanargumentintheOpen()method.

ManagingtheConnection:

IwantyoutopayattentiontothenextfourlinesofcodethatIreprintedbelow.FirstweinstanciateaConnectionObject.

varConnectObj=Server.CreateObject("ADODB.Connection");

ThenweopentheConnection.

ConnectObj.Open(myConnect);

ThentheConnectionObjectbecomesthesecondargumentintheRecordsetOpen()method.

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

Andlastly,whenwearefinishedwiththeConnection,wecloseit.

ConnectObj.Close();

NextUp:

Thereisalotofcodeleftunexplainedinthisexample.We"llrepeatthesamescriptinlesson17andgoovermostofwhatweleftoutthefirsttimethrough.