Introduction
Sometimes we're anxious to create some good applications, and we forget simple things like, code being repeated in various pages. For example - when we want to create a connection to Database, we write the same code in all the ASP pages, which can be avoided easily, by creating a separate .asp or .inc page with the instructions, and then we can include (SSI - Server Side Includes) the file in all our ASP pages.
I use the technique of SSI quite often, because it helps me a lot, on changing or renewing the site.
I'm new learner in ASP, and I've realized this common mistake in all the applications I've been checking, and some of the applications are made by people working with ASP for so many years. But, I'm not writing this article to point mistakes made by others, rather I would prefer that programmers like me, at the beginning stage, read this simple but effective article and avoid these kind of mistakes, I hope somehow I can help them & of course others who are more experienced.
Now, I'll try to explain my point of view:
Step 1
Create a file, you can name it - connection.asp; and you can put the following code into it:
<%
dim conn, connstring
set conn = server.createobject("ADODB.connection")
connstring = "provider = microsoft.jet.OLEDB.4.0; data source = _
" & server.mappath("../yourDBfolder/yourDB.mdb")
%>
Step 2
You can include this file, in any of your ASP files, by the following code:
<!---->
Step 3
After the inclusion of your path to Database, you can write the other code to call the Recordset, SQL statements, to fetch data from your Database, like
<%
conn.open = connstring
dim rs
set rs = server.CreateObject("adodb.recordset")
sqlstring = "select * from tablename"
rs.Open sqlstring,conn
%>
This is what I've done so far, and realized that many programmers write the code we wrote in first file, many times thereby repeating it through many pages, but when comes the time to change the path to your database, or other changes, then instead of changing in so many files, you just change in one file, and it will affect all the others adversely.
Conclusion
So far I've been using ACCESS 2000, but when I start dealing with more data, then I'll start working with SQL Server, or other Database. I've created my own website: http://www.kundan.web.pt/. I sincerely hope I managed to help a little by this simple but effective article. Sorry for my poor English, I'm uses to writing and speaking in Portuguese. If I get a good response for this article, hopefully I'll try to write some more articles for beginners like me.