Jul 04 2000

General on ADO connection strings

Posted by admin under ADO database

In all my example code on connection strings I have imlemented a function called Incdb_GetConnectionString which takes one parameter ( nNumber ).

<%
Function Incdb_GetConnectionString( nNumber )
    Incdb_GetConnectionString = "sqlexper"
End Function
%>


Why? Well, most important is to put the connection string generation into a separate file, which is then included from all other ASP files needing a database connection. NEVER hardwire the connection string in a ASP file, cause it will come a day when you need to change something in it ( maybe you are changing host, upgrading to SQL Server etc ) and by following my advice you will only need to apply those changes in one single spot then.

Now for the other part, the nNumber parameter. I often tend to use many database files at my websites. Maybe you have downloaded a great database driven guestbook and would like to use it on your website. However, the site is live and running and people are constantly updating the existing database ( you have a database driven forum ). In this cases it is most easy to upload a totally new database and use that exclusively for the new component ( the guestbook in our case ). Now the nNumber parameter comes into use. Modify your existing Incdb_GetConnectionString from this:

<%
Function Incdb_GetConnectionString( nNumber )
    Incdb_GetConnectionString = "sqlexper"
End Function
%>
To something like:

<%
Function Incdb_GetConnectionString( nNumber )
    Select Case nNumber
        Case 0
            Incdb_GetConnectionString = "sqlexper"
        Case 1
           Incdb_GetConnectionString = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("\database\guestbook.mdb")
    End Select
End Function
%> 


Now on all your guestbook pages, have them call Incdb_GetConnectionString with nNumber set to 1.