SQL Server Central is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 

Connecting to SQL Server with ASP

By Brian Knight, 2001/04/30

Total article views: 7971 | Views in the last 30 days: 18
Whenever you connect to a website that uses Active Server Pages (ASP), IIS will immediately compile the global.asa to retrieve any global variables that need to be used. The easiest way to connect to SQL Server through ASP is by using a global.asa file. You can also connect in each file if you wish to enable things like connection pooling. For purposes of this article, we will only discuss session connections which would only be used on a small development servers and is only to get you started. More advanced connection pooling options will be discussed in a later article.

Two main types of global variables that can be used in the global.asa file are Application and Session. Application variables are shared throughout the entire application and are shared across for anyone connecting to your website. These should be used sparingly because of this. Session variables are a little more flexible. Each connection to your website can have its own session and it's not shared. The downside to this of course, is that each user visiting your site will use its own SQL connection, and you will need to close each connection in your code.

When you create a ASP project through Visual Interdev, it will create a sample global.asa file for you. If you're starting from scratch though, the first line should begin with :

<SCRIPT LANGUAGE=VBScript RUNAT=Server>
This will set where the queries will process. If we use the Session object, then you're next section of code will call the object and set the global variables:

Sub Session_OnStart
Session("dbaconn_ConnectionTimeout") = 30 Session("dbaconn_CommandTimeout") = 60 Session("dbaconn_CursorLocation") = 2 Session("dbaconn_RuntimePassword") = "password" Session("dbaconn_RuntimeUserName") = "username" Session("dbaconn_ConnectionString") = "Driver=SQL Server;
User Id=sa;PASSWORD="&Session("dbaconn_RuntimePassword")&";
SERVER="&Session("DefaultServer")&";UID=sa;database=Northwind" Session("Datapath") = "d:\mssql7\data\" End Sub

The Session("dbaconn_ConnectionTimeout") variable will set the maximum amount of time that you will allow a user to connect. The Session("dbaconn_CommandTimeout") in contrast will set how long you will allow a query to run.

You can set cursor location at the global level with the Session("dbaconn_CursorLocation") variable. Cursor location can also be set at the query level.

The Session("dbaconn_RuntimePassword") and Session ("dbaconn_RuntimeUserName") variables are optional variables which we will use in our connection string. We did this so that we can change the password and username dynamicly through a website. We will cover changing this in another article. It is not recommended that you use the SA username for this due to security issues.

Session("dbaconn_ConnectionString") is required to connect to SQL Server. The example provided is a DSNless connection. You will call this variable in all sql queries that you run throughout your website. You can hardcode the username and password in this section. We chose to in our connection string make the username and password dynamicly assigned. If you get nothing else from this article, you must learn how to call a query dynamicly. To query dynamicly, you must concatenate the query opening with "& and close with &". For example : "&Session("dbaconn_RuntimePassword")&"

The Session("Datapath") variable is an example of a user defined variable. If you wish to expose a variable to the rest of the website.

The last step is to close the global.asa file with :

</SCRIPT>

The global.asa file provides you an easy way to connect to SQL Server dynamicly without placing a connection string on the top of all files. In the above example, you established a connection to SQL Server through the global.asa file. In future articles we will cover how to query and run DBA commands from a website.

By Brian Knight, 2001/04/30

Total article views: 7971 | Views in the last 30 days: 18
Your response
 
 
Related tags

ASP    
Programming    
 
Related content

Using InterDev's Toolbox

By Leon Platt | Category: .Net
| 3,019 reads

Invisible Comments

By Leon Platt | Category: ASP
| 3,354 reads

ASP and ADO Gotcha - Duplicate Field Names in a Query

By Leon Platt | Category: ASP
(not yet rated) | 3,267 reads

Outlook Appointments, ASP and vCalendar

By | Category: ASP
| 5,358 reads
Like this? Try these...

ASP and ADO Gotcha - Duplicate Field Names in a Query

By Leon Platt | Category: ASP
(not yet rated) | 3,267 reads
Already registered?  

Free registration required

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Register

E-mail address:
Password:
Password (confirm):

  

Subscriptions

We ask you to register on the site and subscribe to our newsletters. Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

We ask that you give the newsletter a try for a week. Over 200,000 SQL Server Professionals a day find it entertaining and useful. If not, you are welcome to unsubscribe at anytime.

Steve Jones
Editor, SQLServerCentral.com