COMP230 Wk7 Lab Instructions

8
VBScript Database Query Lab Objectives In this lab, students will complete the following objectives !reate a connection to an "ccess database !reate various SQL #ueries to e$tract information from a database %ormat e$tracted data with column headers Lab Diagram During your session you will have access to the following lab con&guration Connecting to Your Lab %or this lab, we will only need to connect to Vlab-PC1 Vlab-PC1 Vlab-PC2  ' o start s imply clic( on th e named )or(station from the device list *located on the left hand side of the screen+ and clic( Power on in the tools bar In some cases the devices may power on automatically During the boot up process an activity indicator will be displayed in the name tab Blac(-owered ./ .range)or(ing on your re#uest 0reen1eady to access If the remote console is not displayed automatically in the main window *or popup+ clic( the Connect icon located in the tools bar to start your session !.2-345 )( 7 Database Lab 8 1evision Date9 8384

description

COMP230 Wk7 Lab Instructions

Transcript of COMP230 Wk7 Lab Instructions

Page 1: COMP230 Wk7 Lab Instructions

7/21/2019 COMP230 Wk7 Lab Instructions

http://slidepdf.com/reader/full/comp230-wk7-lab-instructions 1/8

VBScript Database Query Lab

Objectives

In this lab, students will complete the following objectives

• !reate a connection to an "ccess database

• !reate various SQL #ueries to e$tract information from a database

• %ormat e$tracted data with column headers

Lab DiagramDuring your session you will have access to the following lab con&guration

Connecting to Your Lab%or this lab, we will only need to connect to Vlab-PC1

• Vlab-PC1

• Vlab-PC2

 'o start simply clic( on the named )or(station from the device list *located onthe left hand side of the screen+ and clic( Power on in the tools bar In some

cases the devices may power on automatically

During the boot up process an activity indicator will be displayed in the name

tab

• Blac(-owered ./ 

• .range)or(ing on your re#uest

• 0reen1eady to access

If the remote console is not displayed automatically in the main window *orpopup+ clic( the Connect icon located in the tools bar to start your session

!.2-3456)( 76Database6Lab 8 1evision Date9 8384

Page 2: COMP230 Wk7 Lab Instructions

7/21/2019 COMP230 Wk7 Lab Instructions

http://slidepdf.com/reader/full/comp230-wk7-lab-instructions 2/8

If the remote console does not appear please try the following option

• Switch between the :'2L ; and <ava client versions in the tools bar

In the event this does not resolve your connectivity problems please visit our

:elp=Support pages for additional resolution options

 'as( 89 >nderstanding the !ommands in!omputerDatabasevbs

 'he I' department maintains an "ccess database that is used to inventory the

computers in the various rooms %ields in the database include9 !omputer 'ype,:ostname, 1oom ?umber, !-> 'ype, ?umber of Bits, Speed, ?umber of

-rocessors, .perating System, 2emory, and :ard Drive Si@e )e need to #uery

this database to determine upgrades and replacements for e$isting computers

Below (and on te !ollowing "age# is a listing o! te Com"uters$accdb

database contents$

!.2-3456)( 76Database6Lab 3 1evision Date9 8384

Page 3: COMP230 Wk7 Lab Instructions

7/21/2019 COMP230 Wk7 Lab Instructions

http://slidepdf.com/reader/full/comp230-wk7-lab-instructions 3/8

 'as( 39 >nderstanding theD.DB!onnection and "D.DB1ecordset.bjects

In ?ote-adAA, loo( at the following code lines

!.2-3456)( 76Database6Lab 4 1evision Date9 8384

Page 4: COMP230 Wk7 Lab Instructions

7/21/2019 COMP230 Wk7 Lab Instructions

http://slidepdf.com/reader/full/comp230-wk7-lab-instructions 4/8

Line 11 contains the SQL Query String named s%l&tr 'his is the line you will

have to modify to properly #uery the !omputer database 'he current SQL Query

'&LC) * +,O Com"uters. will select all &elds from the database table

!omputers

Lines 12 and 1/ uses a string named data&ource to specify the 2icrosoft

Driver and the name and location of the local database9 Com"uters$accdb

Line 10 !reates the "D.DB!onnectionC object while line 1 opens the

connection to the database

Line 1 !reates the "D.DB1ecordsetC object while line 13 provides access to

the records using the SQL Query String and the !onnection object Line 14

moves the obj1ecordSet pointer to the &rst record

 'as( 49 Displaying the 1ecord :eaders andDatabase 1ecords

In ?ote-adAA, loo( at the following lines of the !omputerDatabasevbs program

!.2-3456)( 76Database6Lab 1evision Date9 8384

Page 5: COMP230 Wk7 Lab Instructions

7/21/2019 COMP230 Wk7 Lab Instructions

http://slidepdf.com/reader/full/comp230-wk7-lab-instructions 5/8

Lines 25620 display the Database 7eldnames as column eaders  ?ote theuse of 8 to concatenate *add+ string values together and 9 which is the VBScript

line continuation character

Lines 26/3 are a Do >ntil loop that se#uences through the database loo(ing

for records that match the SQL Query String 'he obj,ecord&et$O+ method

chec(s to see if we have reached the last record in the database 'his is re#uired

because reading past the end of a database will cause an error record&tr is a

string variable initially set to C recordStr is used to create a multiEline string

that contains the column headers and records that match the SQL #uery 'he

:&cri"t$co record&tr statement in line 05 displays the column headers andrecords to the console or des(top windows depending on whether cscript or

wscript is used to run the program

Lines /4 and /; close the database connections made by the

"D.DB!onnection and "D.DB1ecordset objects

 'he function "ad(b<Val str)e=t> B<Val len# in lines 0060 are used to format

the &eld values with added spaces so the tab positions will line up correctly

!.2-3456)( 76Database6Lab ; 1evision Date9 8384

Page 6: COMP230 Wk7 Lab Instructions

7/21/2019 COMP230 Wk7 Lab Instructions

http://slidepdf.com/reader/full/comp230-wk7-lab-instructions 6/8

 'as( 9 )rite and 1un!omputer1eplacevbs

In this scenario, we need to #uery the !omputer database to determine whichcomputers need to be replaced .ur decision will be based on the !-> speed,

?umber of -rocessors, and the si@e of the :ard drive

8+ .pen the !omputerDatabasevbs program in ?ote-adAA and &ave ?s the

program with the name Com"uter,e"lace$vbs

3+ 2odify the SQL Query String *s#lStr+ in line 88 to e$tract the following

information from the database

Fields Displayed from Computers Table (specified by the SELECT clause).

Computer

Room_Num 

Speed 

 Num_CPUs

OS_Type

HDD_Size

Replaeme!t Criteria (specified by the "HERE clause).

Any computer with a si!#le CPU

Any computer with a CPU speed less t$a! %&' (Hz

Any Computer with a Hard Dis) Dri*e size less t$a! +,, (-ytes

Sort Criteria (specified by the ORDER -. clause).

Sort the extracted records by the “Room_Num ” field.

4+ 2odify lines 35F3 to display the correct &eld headers for the &elds being

displayed

+ 2odify the Do >ntil loop body to include only the &elds being displayed >se

the "ad( # function as needed to ma(e the header and &eld values line up

;+ 1un your program from a command window using wscri"t

Com"uter,e"lace$vbs to verify correct formatting and #uery results 

!.2-3456)( 76Database6Lab G 1evision Date9 8384

Page 7: COMP230 Wk7 Lab Instructions

7/21/2019 COMP230 Wk7 Lab Instructions

http://slidepdf.com/reader/full/comp230-wk7-lab-instructions 7/8

G+ 'his #uery should generate eight records displayed in order by room number

If you have any errors, do not get the correct results, or your columns are misE

aligned, modify your program as re#uired until you get the correct output

Copy and paste your ComputerReplace.vbs proram sourcecode from !ote"ad## and the

des$top window from your Run into the spaces provided in your lab%report document.

Answer the &uestions about the Replacement S' 'uery in the lab%report document.

 'as( ;9 )rite and 1un!omputer>pgradevbs

In this scenario, we need to upgrade our company computers based on the.perating System and the amount of memory )e want to ensure that all %edora

85 machines are upgraded to %edora 8 and all )indows H- machines are

upgraded to )indows 7 If we &nd any computers with only 3 0B of memory, we

will upgrade the memory to 0B 

8+ .pen the !omputerDatabasevbs program in ?ote-adAA and Save "s the

program with the name !omputer>pgradevbs

3+ 2odify the SQL Query String *s#lStr+ in line 88 e$tract the following

information from the database

Fields Displayed from Computers Table (specified by the SELECT clause).

Computer

HostName

Room_Num 

OS_Type

 /emory

Replaeme!t Criteria (specified by the "HERE clause). (!ote Strin values in

fields must be delimited by sinle &uotes.)

Any computer with the Fedora ', Operati!# System 01Fedora ',23

Any computer with the "i!do4s 5P Operati!# System 01"i!do4s 5P23

Any computer with % (- of memory

Sort Criteria (specified by the ORDER -. clause).

Sort the extracted records by the “OS_Type” field.

!.2-3456)( 76Database6Lab 7 1evision Date9 8384

Page 8: COMP230 Wk7 Lab Instructions

7/21/2019 COMP230 Wk7 Lab Instructions

http://slidepdf.com/reader/full/comp230-wk7-lab-instructions 8/8

4+ 2odify lines 35F3 to display the correct &eld headers for the &elds being

displayed

+ 2odify the Do >ntil loop body to include only the &elds being displayed >se

the "ad( # function as needed to ma(e the header and &eld values line up

;+ 1un your program from a command window using wscri"t

Com"uter@"grade$vbs to verify correct formatting and #uery results

G+ 'his #uery should generate 8G records displayed in order by .S6'ype If you

have any errors, do not get the correct results, or your columns are misEaligned

modify your program as re#uired until you get the correct output

Copy and paste your Computer*prade.vbs proram sourcecode from !ote"ad## and the

des$top window from your Run into the spaces provided in your lab%report document.

Answer the &uestions about the *prade S' 'uery in the lab%report document.

!.2-3456)( 76Database6Lab J 1evision Date9 8384