Technology Software

VBScript & SQL Tutorial

    Making a Connection

    • The connection to the database is a required part of VBScript database programming. The connection holds the values of the username and password, server name and database used in the application. Below is the syntax for creating a connection to the database in VBScript:

      <%
      Set connection = Server.CreateObject("ADODB.Connection")
      FilePath = Server.MapPath("MyCustomers.mdb")
      connection.Open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & FilePath & ";"
      %>

      Notice the "%>" in the lines of code. This string of characters is used in the HTML files of VBScript to denote the usage of back-end code. Anything placed within the "<% %>" characters are processed on the server prior to rendering the HTML page for the user. Additionally, the code within these markers is not seen by the user, so the sensitive data like username and password is secure.

    Querying the Database

    • Now that the connection has been made, a query can be used to retrieve some data. In this example, a list of employee first names is retrieved from the database. Below is an example of a query assigned to a string variable:

      query = "select first_name from employees"
      Set records = connection.Execute(query)

      The first line of code is the proper syntax for a SQL query. It simply retrieves a list of first names for employees. The second line of code calls the server, retrieves the data and assigns it to the records variable. Once the data has been assigned, the variable can be used to display values to the user.

    Displaying Values from Queries

    • The records variable holds the data for display, so now the application can print it to the HTML page. Below is an example of printing the first record to an HTML element:

      <p><%=records("first_name")%></p>

      Notice how the VBScript is surrounded by the "<% %>" characters again. This is how the programmer inserts VBScript code within an HTML element. The VBScript is processed prior to rendering the HTML, but it is still wrapped within the HTML paragraph tags by the browser.

Related posts "Technology : Software"

Best Tips For Perfect Presentation

Software

Enterprise Security - Are You Ready For 2012?

Software

Magento Shopping Cart Extension Makes Shopping Manageable

Software

How to Convert VB6 to VB 2010

Software

How to Set Up Shortcut Keys in Microsoft Word

Software

Outsource CAD services – A Modern Way to Save Time and Money

Software

Best Places to Find Iron in "World of Warcraft"

Software

How to Print a PDF Drawing in SolidWorks With No Dashed Lines

Software

Open Source Web Development Cms PlatformA Wide Choice For Various Businesses

Software

Leave a Comment