Comp 230 ilab week 7
VBScript Database Query Lab
Objectives
In this lab, students will complete the following objectives.
· Create a connection to an Access database.
· Create various SQL queries to extract information from a database.
· Format extracted data with column headers.
Lab Diagram
During your session you will have access to the following lab configuration.
Connecting to your lab
For this lab, we will only need to connect to Vlab-PC1.
· Vlab-PC1
· Vlab-PC2
To start simply click on the named Workstation from the device list (located on the left hand side of the screen) and click the Power on from the in 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:
· Black - Powered Off
· Orange - Working on your request
· Green - Ready to access
If the remote console is not displayed automatically in the main window (or popup) click the Connect icon located in the tools bar to start your session.
If the remote console does not appear please try the following option:
· Switch between the HTML 5 and Java client versions in the tools bar.
In the event this does not resolve your connectivity problems please visit our Help / Support pages for additional resolution options.
Task 1: Understanding the Commands in ComputerDatabase.vbs
The IT department maintains an Access database that is used to inventory the computers in the various rooms. Fields in the database include: Computer Type, Hostname, Room Number, CPU Type, Number of Bits, Speed, Number of Processors, Operating System, Memory, and Hard Drive Size. We need to query this database to determine upgrades and replacements for existing computers.
Below (and on the following page) is a listing of the Computers.accdb database contents:
Task 2: Understanding the DODB.Connection and ADODB.Recordset Objects
In NotePad++, look at the following code lines.
Line 11 contains the SQL Query String named sqlStr. This is the line you will have to modify to properly query the Computer database. The current SQL Query “SELECT * FROM Computers” will select all fields from the database table Computers.
Lines 12 and 13 uses a string named dataSource to specify the Microsoft Driver and the name and location of the local database: Computers.accdb.
Line 14 Creates the “ADODB.Connection” object while line 15 opens the connection to the database.
Line 16 Creates the “ADODB.Recordset” object while line 17 provides access to the records using the SQL Query String and the Connection object. Line 18 moves the objRecordSet pointer to the first record.
Task 3: Displaying the Record Headers and Database Records
In NotePad++, look at the following lines of the ComputerDatabase.vbs program.
Lines 20–24 display the Database fieldnames as column headers. Note the use of & to concatenate (add) string values together and _ which is the VBScript line continuation character.
Lines 25–37 are a Do Until loop that sequences through the database looking for records that match the SQL Query String. The objRecordSet.EOF method checks to see if we have reached the last record in the database. This required because reading past the end of a database will cause an error. recordStr is a string variable initially set to “”. recordStr is used to create a multi-line string that contains the column headers and records that match the SQL query. The WScript.Echo recordStr statement in line 40 displays the column headers and records to the console or desktop windows depending on whether cscript or wscript is used to run the program.
Lines 38 and 39 close the database connections made by the ADODB.Connection and ADODB.Recordset objects.
The function pad(byVal strText, ByVal len) in lines 44–46 are used to format the field values with added spaces so the tab positions will line up correctly.
Task 4: Write and Run ComputerReplace.vbs
In this scenario, we need to query the Computer database to determine which computers need to be replaced. Our decision will be based on the CPU speed, Number of Processors, and the size of the Hard Drive.
1) Open the ComputerDatabase.vbs program in NotePad++ and Save As the program with the name ComputerReplace.vbs.
2) Modify the SQL Query String (sqlStr) in line 11 to extract 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
Replacement Criteria (specified by the WHERE clause).
Any computer with a single CPU
Any computer with a CPU speed less than 2 GHz
Any Computer with a Hard Disk Drive size less than 300 GBytes
Sort Criteria (specified by the ORDER BY clause).
Sort the extracted records by the “Room_Num” field.
3) Modify lines 20–24 to display the correct field headers for the fields being displayed.
4) Modify the Do Until loop body to include only the fields being displayed. Use the pad( ) function as needed to make the header and field values line up.
5) Run your program from a command window using wscript ComputerReplace.vbs to verify correct formatting and query results.
6) This query 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 mis-aligned; modify your program as required until you get the correct output.
Copy and paste your ComputerReplace.vbs program sourcecode from NotePad++ and the desktop window from your Run into the spaces provided in your lab-report document. Answer the questions about the Replacement SQL Query in the lab-report document.
Task 5: Write and Run ComputerUpgrade.vbs
In this scenario, we need to upgrade our company computers based on the Operating System and the amount of memory. We want to ensure that all Fedora 10 machines are upgraded to Fedora 14 and all Windows XP machines are upgraded to Windows 7. If we find any computers with only 2 GB of memory, we will upgrade the memory to 4 GB.
1) Open the ComputerDatabase.vbs program in NotePad++ and Save As the program with the name ComputerUpgrade.vbs.
2) Modify the SQL Query String (sqlStr) in line 11 extract the following information from the database.
Fields Displayed from Computers Table (specified by the SELECT clause).
Computer
HostName
Room_Num
OS_Type
Memory
Replacement Criteria (specified by the WHERE clause). (Note: String values in fields must be delimited by single quotes.)
Any computer with the Fedora 10 Operating System (‘Fedora 10’)
Any computer with the Windows XP Operating System (‘Windows XP’)
Any computer with 2 GB of memory
Sort Criteria (specified by the ORDER BY clause).
Sort the extracted records by the “OS_Type” field.
3) Modify lines 20–24 to display the correct field headers for the fields being displayed.
4) Modify the Do Until loop body to include only the fields being displayed. Use the pad( ) function as needed to make the header and field values line up.
5) Run your program from a command window using wscript ComputerUpgrade.vbs to verify correct formatting and query results.
6) This query should generate 16 records displayed in order by OS_Type. If you have any errors, do not get the correct results, or your columns are mis-aligned; modify your program as required until you get the correct output.
Copy and paste your ComputerUpgrade.vbs program sourcecode from NotePad++ and the desktop window from your Run into the spaces provided in your lab-report document. Answer the questions about the Upgrade SQL Query in the lab-report document.
COMP230_Wk 7_Database_Lab 1 Revision Date: 1213