Windows脚本编程核心技术精解Chapter23.pdf
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《Windows脚本编程核心技术精解Chapter23.pdf》由会员分享,可在线阅读,更多相关《Windows脚本编程核心技术精解Chapter23.pdf(24页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Chapter 23Using DatabasesIn This Chapter?Learn about ADO and MDAC,the standard interfaces to any database?Check your ADO and MDAC versions,and update your system with the helpof free downloads?Access the sample database using a DSN-free connection?Read and write to any database using scripts and SQL
2、?Deal with recordsets,count records,delete records,and retrieve informationabout table fields and variable types?Eliminate the“single quote”problem and work around reserved SQLcharacters?Create completely new database tables and delete old ones?Learn all about the Index Server,and use Boolean operat
3、ors to quickly findthe documents you are looking for?Do full text searches and queries in natural language?Limit search scope to specific folders using the hidden ixsso.UtilobjectMicrosoft has made database access easy.By using MDAC(MicrosoftDatabase Access Components),your scripts can connect thems
4、elves to any kind of database and read information using SQL statements.Yourscripts can even store information and change database structures.In thischapter,you learn how to add powerful database capabilities to your scripts.Accessing DatabasesImagine what you could do if your scripts were able to“t
5、alk”to databases.You could create your own little address book,retrieve invoice information,or store user names.Fortunately,Microsoft has created a standard interface to access any kind ofdatabase,including simple text databases as well as full-scale SQL servers orOracle database servers.4684-8 ch23
6、.f.qc 3/3/00 9:45 AM Page 645Checking ADO versionThis interface is called ActiveX Data Objects(ADO).Many products silentlyinstall ADO on your system,so lets check whether ADO is working on yoursystem and whether its up-to-date(see Figure 23-1).23-1.VBSset fs=CreateObject(“Scripting.FileSystemObject”
7、)set wshshell=CreateObject(“WScript.Shell”)msg=“ADO status:”&vbCr&vbCr check whether Connection is accessibleon error resume nextset dummy=CreateObject(“ADODB.Connection”)if not err.number=0 thenMsgBox msg&“ADODB.Connection isnt working!”WScript.Quitelsemsg=msg&“ADODB.Connection is operational.”&vbC
8、rend ifon error goto 0 retrieve fileson error resume nextclsid=wshshell.RegRead(“HKCRADODB.ConnectionCLSID”)exec=wshshell.RegRead(“HKCRCLSID”&clsid&“InProcServer32”)path=Left(exec,InstrRev(exec,“”)-1)path=Left(path,InstrRev(path,“”)-1)&“Ole DB”if not err.number=0 thenMsgBox msg&“Error retrieving fil
9、e version information!”WScript.Quitend ifon error goto 0 check for ADO componentsfilename=“msdadc.dll”if fs.FileExists(path&filename)thenfilever=fs.GetFileVersion(path&filename)msg=msg&filename&“exists:Version“&filever&vbCrelsemsg=msg&filename&“is missing.”&vbCrend iffilename=“oledb32.dll”if fs.File
10、Exists(path&filename)thenfilever=fs.GetFileVersion(path&filename)msg=msg&filename&“exists:Version“&filever&vbCrelsemsg=msg&filename&“is missing.”&vbCr646Part V:Accessing Hidden ComponentsII4684-8 ch23.f.qc 3/3/00 9:45 AM Page 646end ifMsgBox msg,vbInformationThis script makes some assumptions.If you
11、r ADODB.Connection isoperational,but neither file is found,you should search for the files manually.Figure 23-1:Check whether ADO is operational and which MDAC version you have.Use Table 23-1 to calculate the interface version your system currently uses.Table 23-1Information to Determine ADO Version
12、 InstalledADODB.ConnectionMSDADC.DLLOLEDB32.DLLVersionnot operationalmissingmissingno MDAC(MicrosoftData AccessComponents)installedoperational1.50.3506.0missingMDAC 1.5coperational2.0.3002.42.0.1706.0MDAC 2.0operational2.0.3002.232.0.1706.0MDAC 2.0 SP1/SP2operational2.10.3513.02.10.3513.0MDAC 2.1.0.
13、3513.2(SQL)operational2.10.3711.22.10.3711.2MDAC 2.1.1.3711.6(Internet Explorer 5)operational2.10.3711.22.10.3711.9MDAC 2.1.1.3711.11operational2.50.4403.02.50.4403.3MDAC 2.5(Windows2000)Do I need to upgrade or install anything?Heres the good news.If your script reports ADODB.Connectionoperational,y
14、ou are ready for action.Out of curiosity,you could check your MDACversion,but even if its MDAC 1.5,you are fine.Chapter 23:Using Databases647II4684-8 ch23.f.qc 3/3/00 9:45 AM Page 647And heres more good news.If MDAC is missing on your system,you caninstall it for free.Microsoft offers the complete s
15、oftware kit for downloading.But,you guessed it,theres some not-so-good news,too.Download addresseschange frequently,and Microsoft download addresses change even morefrequently.Start your search here: are also available at for MDAC version 2.1 or later.The download is huge,though;more than 6MB are wa
16、iting for you.Never touch a running system.If your system supports ADODB.Connection,then think twice before deciding to upgrade.There are minor inconsistenciesbetween the MDAC versions,and installing a higher version on a system withapplications that use a lower version might very well crash those a
17、pplications.So,heres an easy ruleinstall MDAC only if its not yet installed or if youspecifically need a new feature only available in the update.Remember:Onceyou have upgraded to a higher MDAC version,you cant go back.You canteven uninstall MDAC.Its a one-way street.Do install the MDAC update if an
18、y of the following example scripts raise a“Cant create Object”error.Its a rather common problem:On some systems,MDAC was installed at some time but parts of it became damaged or wereuninstalled.Those systems can access ADODB.Connection,but they fail themoment you try to open a database.Getting Acces
19、s to a DatabaseFirst of all,you need a database to access.ADO is able to store data as plaintext files in case you have no database at hand.On the companion CD,youfind a sample MS Access database.You should use this database for all ofyour experiments if you dont have a database of your own.As it tu
20、rns out,your scripts can change and even add tables to the sample database.Opening and querying a databaseTo use a database,your script needs to open it first.There are many ways to open a database.Most often you will see the DSN approach:Using theControl Panel and its 32-bit ODBC Data Source module
21、,you define a SystemDSN and store all the details about your database in the DSN.Then,yourscripts just refer to the DSN name tag,and ADO reads the connection detailsfrom your DSN definition(see Figure 23-2).However,the DSN approach has some major drawbacks.It requires you to setup the DSN first.You
22、need to go through many dialog boxes.Not so good.Amuch easier way bypasses the DSN.Here,the script provides the connectiondetails manually.Using this approach,you can distribute your database fileand your scripts,and your users can access the data base information rightaway.No further setup is requi
23、red.648Part V:Accessing Hidden ComponentsII4684-8 ch23.f.qc 3/3/00 9:45 AM Page 648Figure 23-2:Dont use fixed DSNs,or you will lose a lot of flexibility.Try it out.Along with the scripts of this chapter,you will find a sampledatabase(sample.mdb).Copy the entire folder onto your hard drive and getrid
24、 of the write-protection attributes.You can then immediately start workingwith this database and even create your very own table design.All files on a CD have a write-protection attribute.If you copy CD-ROM files toyour local hard drive,they keep this attribute,and you cant change the files.Therefor
25、e,whenever you copy data from a CD ROM,you must remove theattribute:Right-click the files and choose Properties.Then,clear the write-protection attribute and click OK.Now,without any further hassle,launch the script 23-2.VBS.It will accessthe database and retrieve the sample information:my name and
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Windows 脚本 编程 核心技术 Chapter23
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内