|Web Hosting Wit.Com
Webhosting, Server and Web Site Help & Information
What is SQL?
SQL stands for Structured Query Language. This is used to access information from a database by the use of commands. The following guide will show you how to use SQL with ASP (Active Server Pages) to add databsae interaction to your website(s).
To use SQL you must be using a database management system that supports it. A few of those are Microsoft SQL Server, Access, Oracle, and Sybase. If you need web hosting with SQL click here to find a server or web host with the options you need.
First save your pages as .asp instead of .html because this tells the browser to open the ASP side server for ASP functions (which, in this case, will include the SQL database commands) within the web page. You will have to load the pages on a Windows-based server.
Since we're going to use ASP along with SQL, you will need to refer to the ASP tutorial to setup your ASP pages correctly. In addition to that, you need to have database access configured on an SQL server. You may wish to contact your webhosting provider for that information if you don't already have it.
Once you have everything ready, you must make connections to the database from your .asp code. Since this is not HTML code, you must have these statements as part of your ASP code (i.e. between the <% and %> markers).
Reserve memory for the following variables at the start of your code:
Set the connection variable to connect to the database server:
Set the record variable to get records from the database:
(The query variable doesn't need to be assigned in the beginning of your code.)
To connect to your database you will need the database name, and a username and password to gain access to it.
Once you have a connection, accessing the database is easy. Assign your query varilable to an SQL instruction. (See the Quick Reference below for an abbreviated list of SQL instructions.)
Say you kept the members of an organization in a database, and wanted to see all of the members' first names, last names, and phone numbers. Assuming the field names are firstname, lastname, and phone, you would do this:
Next step is to use your record and connection variables to open the database with your query request.
The best way to go through all the entries selected by your query is by using Do While / Loop. For an explanation of how Do While / Loop works, please see the ASP Tutorial. Use the eof (end of file) command to tell the loop to keep repeating as long as we're not at the end of the query.
To display the fields on the screen, you would use Response.Write. For an explanation on how Response.Write works, please see the ASP Tutorial. Use the field command to specify which fields to retrieve.
To get to the next entry, simply use:
This is a good spot to end your Do While loop, so you would put:
For more examples on how to use SQL with ASP, please see SQL under Coding Examples.
The following are common instructions used in accessing databases in SQL, with what they do and how to use them. Note: column and table should be replaced with valid column names from a valid SQL database table.
If you have any programming tips, information, or scripts you would like to add to the site e-mail it to firstname.lastname@example.org