Connecting and Displaying an SQL Database from an ASP
Introduction. One
of the major starting points for all work with SQL Server databases is the
Northwind database. It is a fairly classic full featured
customer/product/order type of database. We will be working with the
Customers table that has most of the obvious fields.
One thing to remember when working with SQL Server is that the administrative tools mislead you to believe that your regular NT/2000 network authentications will be sufficient for working within SQL Server. This is not true. We needed to create separate SQL Server security in order to be able to use it for connecting from ASPs. Now that I am done with that diatribe against the foibles of Microsoft, it is understandable that SQL Server should have extensive security that goes above and beyond what one normally uses for network operations. It's just that one would hope the documentation wouldn't mislead you to believe that NT/2000 authentications will take care of at least your basic security needs. Connecting to a Database Using an ASP. There are two major ways to connect to a database from an ASP.
We will focus on the DSN-Less types of connections because these are what I have been able to implement on this server. The DSN or Data Source Name connections appear to be a bit cleaner, but it seems you need to have more direct access to certain things on the server in order to implement them. If you are running Windows 2000 or have direct access to the web server you are more likely to want to use this approach. DSN-Less Connections. I will present some code snippets that you can modify to fit your own webs. The snippets assume that you have an SQL Server database with name DatabaseName on the database server. The table within this database that you want to work with is called TableName. We will work an example in the next web page for this week. First we must develop the usual HTML that signals the server to use the ASP.dll. We also have a statement that tells the server to include some information from elsewhere on the server.
The first two statements should be familiar. If you've worked with C++ you have included header files and libraries. The third statement has a similar purpose. The included file has a number of things in it that are useful for connecting to databases. It also includes the "definitions" of some ADO constants that we will use as parameters. These are similar to the VB constants you've probably used in the past such as vbOkOnly, vbYesNo, vbYes and vbNo. The adovbs.inc file is automatically included on the server when the IIS = Internet Information Server is installed. It is in the C:\Program Files\Common Files\System\ado\ directory on the server. It needs to be copied to your root web by the server administrator. What I have done is e-mailed it to you as an attachment and you should upload it to your root directory.. If you are more than a user on the server the nature of this #include command is likely to change so that you can access the file differently. Unfortunately, we are all just little, nearly totally unimportant users, on the faculty server. The next snippet of code has to do with establishing the connection.
This is actually just four lines of code due to the _ line continuation character combination. Remember you must have a space in front of the underscore in order for it to work.
Now that the connection is open you need to open the particular recordset. This is done with a set of code similar to the following.
These three lines of code are quickly described in the following bullets.
The last thing you need to do after implementing these connections is to make sure you close them after you are done. This ensures a much cleaner operating environment and is very important.
You really do need to close the recordset first, then the connection and set each of them to Nothing. The Open Method for the Recordset Object. We have just made use of the objRS.Open command in the previous set of code. I probably need to say a little bit more about it. In general the Open method will be of the form objRecordsetInstance.Open Source, ActiveConnection, CursorType, LockType, Options This is why the command above has so many commas. By not filling in any values for certain parameters the default values are used.
You need to include the adovbs.inc in order to make use of these constants. The defaults are for the forward only cursor and the read only lock. This is only a brief survey which we will elaborate on when needed. Remember we will be using SQL for our database developments and not doing our database interactions based on VB. Connect and Display Customers in Northwind. Since you have essentially seen all of this code when working with Access databases you should copy it into a file called ConnectToNorthwind.asp and upload it to your cisdev account.
|
<%@ Language=VBScript %> <% Option Explicit %> <!--#include File="adovbs.inc"--> <% ' Open a connection to our SQL Server database ' We will use the DSN-less connection Dim connNorthwind Set connNorthwind = Server.CreateObject("ADODB.Connection") connNorthwind.ConnectionString="Driver={SQL Server}; Server=cisdev.quinnipiac.edu;" & _ "Database=northwind; UID=cis; PWD=csatqu" connNorthwind.Open ' Instantiate a recordset object and retrieve the info from the Customers table Dim rsCustomers Set rsCustomers = Server.CreateObject("ADODB.Recordset") rsCustomers.Open "Customers", connNorthwind, , , adCmdTable ' Display the contents of the Customers table Response.Write "<center><table bgcolor=aaccff border=1>" Response.Write"<tr><td><b><font size = 4>Customer ID</font></b></td>" & _ "<td><b><font size = 4>Company Name</font></b></td>" & _ "<td><b><font size = 4>Contact Name</font></b></td></tr>" Do While Not rsCustomers.EOF
Loop |
I have tried to put in a few extra formatting features
for the table in case you haven't seen them before. We'll discuss it in
class. The code shouldn't contain any particular surprises other than the
following line. connNorthwind.ConnectionString="Driver={SQL Server}; Server=cisdev.quinnipiac.edu;" & _ " Database=northwind; UID=cis; PWD=csatqu" You should notice several things
You should remember this UID and PWD, they are what we will be using throughout our semester.In future semesters we are likely to improve on this, but for the present IT WORKS! When you upload the file and go to the ASP on your web you should see something like the following. |