Monday, August 27, 2007

A simple ADO DB connection in ASP

A simple ADO DB connection in ASP
=============================


* Create a Connection instance
* Open Connection with provider details either sql server/ms access
* Create a Recordset instance
* Open Recordset with your sql statement
* loop it to display the fetched query


Here is your code to do so :

<body>
<%
Dim DB
Set DB = Server.CreateObject("ADODB.Connection")
DB.Open("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & _
"C:\My documents\students.mdb")

Set Rs = Server.CreateObject("ADODB.Recordset")

Rs.Open "Select * from students",DB

If Rs.EOF Then
Response.Write "There are no records"
Else
Rs.MoveFirst

While Not Rs.EOF
Response.Write Rs.Fields("FirstName")
Response.Write Rs.Fields("LastName")
Response.Write "<hr/>"
Rs.MoveNext
Wend
End If

%>
</body>
</html>

'If you want to add new record using ado. plz look at the following:

Rs.AddNew
Rs("FirstName")="XMan"
Rs("LastName")="XFemale"
Rs("DateOfBirth") = CDate("25 july 1981")
Rs.Update




No comments: