% ' ----------------------------------------------------------------- ' Database ASP Example Made Easy ' By Paul Miller ' http://www.salientbeach.com/ ' ' Lets say you have a database at in the folder "database" in your ' root FTP. In that folder you have a database(DB) named YourDatabase.mdb ' The first thing you have to do it make a connection to this dB. This is the ' HTML version of the code, a text version can be found here. An example ' DB for your code can be found here. You can always find more cool ' stuff at saleintbeach. ' ' Everything with a ' in front of it is a comment. ' ----------------------------------------------------------------- ' ----------------------------------------------------------------- ' 1. We have to define where the dB is and how we are going to talk ' to it. We do this by setting up a connection string. ' ----------------------------------------------------------------- ' This is the default string needed to access the dB sConnString = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" ' This create a path, like "c:\webroot\serverfiles\", for whatever ' server you are on to the place on the server where all the web ' files are stored. This will tell exactly where the dB is stored. sMapPath = Server.MapPath("\") ' This will concatenate the path we just created with where the ' location of the database in your web server files. This can ' point to any database in you web directory. sMapPath = Mid(sMapPath, 1, InStrRev(sMapPath,"\")-1) & "\database\YourDatabase.mdb" ' This entire string is now concatenated to one sting for easy use. SiteConnectionString = sConnString & sMapPath ' ----------------------------------------------------------------- ' 2. We now have to set up a session with the connection string ' so that we can actually pull data down that we can use. ' ----------------------------------------------------------------- ' This creates a server side object that we can use to ask for the data set Recordset1 = Server.CreateObject("ADODB.Recordset") ' This is where we use the path defined above to tell this object where ' the dB is actually located. Recordset1.ActiveConnection = SiteConnectionString ' This is the SQL string used to "query" the data. This is extremely ' important for acquiring the correct data. Access can write this string ' for you in most simple cases, and then all you have to do is copy and ' paste the string in to this code. 'Articles' is a table name in the dB Recordset1.Source = "SELECT * FROM Articles" ' This will now execute all the commands we set above and make a direct ' connection to the dB Recordset1.Open() ' ----------------------------------------------------------------- ' 3. We now have to create HTML and insert the database information ' using ASP code. All the dB information is encapsulated in the ' asp brackets. ' -----------------------------------------------------------------
<% ' There can be as many spaces as you want between the ASP open and ' close tags. These spaces and information will be manipulated by ' the server and discarded before it is sent out to the browser. ' This is how you call and display the data from the dB You have ' to use response.write to display the data and the data is ' Recordset1.Fields.Item("Title").Value. The value of what is b/w ' "" (in this case Title) is the column name in the dB ' The if NOT statement is there to say, if there is no data in the database ' based on that query, then do not display anything. If it does try to ' display something, it will give you an error. %> <% if NOT Recordset1.EOF or Recordset1.BOF then %>