First, let me introduce myself. My name is Guilhem Fabre, I'm 21 years old and I work as dynamics Web sites developpers at E-ComOuest, a new Britton firm.
I'm working in ASP since several months, and everytime I wasting my time having to wrote the same piece of code for accessing my data bases. .
So I found a solution. it's easy and you can use it everytime you need to code in ASP : using VBScript function and subs..
It work this way : create a file (i.e. fonctions.asp) which will contain several functions and subs to control and access your DB. Next, you just need to include this file in your .asp files..
Here is the file I use :
<% ' database connection ' Sub Connexion() sConn = "PROVIDER=MSDASQL;DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Application("myBase") Set oConn = Server.CreateObject("ADODB.Connection") oConn.Open sConn End Sub
' Executing a query without return (insert, update, delete) ' Sub Executing(sReq) If sreq <> "" then oConn.Execute(sReq) else response.write("<b>Error E001 : Executing a request</b><br>") response.write("parameter is empty."<br>") end if End Sub
' Evaluing a recordset (empty or not) ' Function notFull(ByRef rstReq) If rstReq.eof OR rstReq.bof then estVide = true else rstreq.movefirst estVide = false end if end function
' Opening a RecordSet for page by page listing ' Sub Opening(sReq, ByRef rstReq, nb) Set rstReq = Server.CreateObject(""ADODB.RecordSet"") If nb <> "" And nb <> "0" then rstReq.CursorLocation = 3 rstReq.PageSize = CInt(nb) end if rstReq.Open sReq, oConn, 3, 3 End Sub
' Creating a scrolling list with a recordset ' Sub CreateSelect(ByRef rstReq, iVal1, iVal2) If Not notFull(rstReq) Then Do Until rstReq.Eof response.write("<option value='"&rstReq(iVal1)&"'>") response.write(rstReq(iVal2)) response.write("</option>") rstReq.MoveNext Loop Else response.write("<option value='NULL'>No entry</option>") End If End Sub
' Cloding a recordset ' Sub Closing(ByRef rstReq) if isObject(rstReq) then rstReq.Close Set rstReq = Nothing End Sub
' Mail sending by CDONTS ' sub mailSend(paramFrom, paramTo, paramSubject, paramBody, paramMailType) Set objCDOMail = Server.CreateObject("CDONTS.NewMail")
objCDOMail.From = paramFrom objCDOMail.To = paramTo objCDOMail.Subject = paramSubject objCDOMail.Importance = 1 objCDOMail.BodyFormat = paramMailType if paramTypeMail = 0 then objCDOMail.MailFormat = 0 end if
' message body objCDOMail.Body = paramBody
' Send the message! objCDOMail.Send Set objCDOMail = Nothing end sub
' Deconnecting a database: ' Sub Deconnecting() oConn.Close Set oConn = Nothing End Sub %>
Ok, i'ts not so good. More functions should be added..
How to use it
It's easy. Include the file then call the functions like this :.
<!-- #include file="fonctions.asp"--> <% Dim myQuery, myRecordSet myQuery = "SELECT * FROM myTable"
Openning myQuery, myRecordSet, 0 ' 0 is for listing all the contents of the table