javascriptasp教程创建数据库连接
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.
相关文章
- 安装oracle11g oci.exe,oracle 11g安装图解|安装oracle数据库软件详细教程[通俗易懂]
- MySQL【三】---数据库查询详细教程{分页、连接查询、自关联、子查询、数据库设计规范}
- windows如何使用sqlpus连接oracle 数据库教程
- Oracle 配置连接远程数据库的教程
- 教你Navicat Premium连接mongodb数据库详细教程
- SQL和Oracle教程:掌握计算机数据库的基础知识(sql教程oracle)
- Oracle数据库中的序列使用教程(oracle中的序列)
- MySQL数据库优化:提高性能的新技术(mysql数据库优化技术)
- 使用Oracle触发器提高数据库效率(oracle触发器类型)
- 深入探索Oracle数据库中的触发器类型(oracle触发器类型)
- Oracle 数据库直连技术实践(oracle直连)
- Oracle教程:如何删除进程,加速数据库性能提升(oracle删除进程)
- 快速上手Oracle 12c安装教程,轻松搭建数据库环境(oracle12c安装)
- MySQL数据库开发指南(mysql数据库开发教程)
- 使用Yii框架连接Oracle数据库简易教程(yiioracle)
- PHP 存入 MySQL 数据库教程(php 存入mysql)
- 程序Go语言调用Oracle数据库驱动程序指南(go oracle驱动)
- 为何要用Redis作为数据库(为啥用redis)
- Oracle优势多多的数据库应用程序(Oracle什么功能)
- 语言连接教程 MySQL的下载及C语言连接教程,帮助初学者快速掌握使用MySQL数据库,并学会用C语言连接MySQL数据库
- PHP面向对象的使用教程简单数据库连接
- ThinkPHP连接Oracle数据库的详细教程[全]
- Window系统下自动备份MySql数据库方法
- DBA在Linux下安装OracleDatabase11g数据库图文教程