| Print Page | Close Window |
| Database Connection In ADOCON Printed From: S2H.co.uk Category: Programming & Web Design Forum Name: Database Discussion Forum Description: Discussion and chat on database related topics. URL: http://www.s2h.co.uk/forum/forum_posts.asp?TID=68 Printed Date: 26 Mar 2026 at 11:50pm Topic: Database Connection In ADOCON Posted By: Jeet Chowdhury Subject: Database Connection In ADOCON Date Posted: 13 Nov 2007 at 8:22pm
Create an
|
<%set conn=Server.CreateObject("ADODB.Connection")conn.Provider="Microsoft.Jet.OLEDB.4.0"conn.Open "c:/webdata/northwind.mdb"set rs=Server.CreateObject("ADODB.recordset")rs.Open "Select * from Customers", conn%> |
After a recordset is opened, we
can extract data from recordset.
Suppose we have a database named
"Northwind", we can get access to the "Customers" table
inside the database with the following lines:
<%set conn=Server.CreateObject("ADODB.Connection")conn.Provider="Microsoft.Jet.OLEDB.4.0"conn.Open "c:/webdata/northwind.mdb"set rs=Server.CreateObject("ADODB.recordset")rs.Open "Select * from Customers", connfor each x in rs.fields response.write(x.name) response.write(" = ") response.write(x.value)next%> |
The ADO Recordset object is used
to hold a set of records from a database table.
We have a database named
"Northwind" and we want to display the data from the
"Customers" table (remember to save the file with an .asp extension):
<html><body><%set conn=Server.CreateObject("ADODB.Connection")conn.Provider="Microsoft.Jet.OLEDB.4.0"conn.Open "c:/webdata/northwind.mdb"set rs = Server.CreateObject("ADODB.recordset")rs.Open "SELECT * FROM Customers", conndo until rs.EOF for each x in rs.Fields Response.Write(x.name) Response.Write(" = ") Response.Write(x.value & "<br />") next Response.Write("<br />") rs.MoveNextlooprs.closeconn.close%></body></html> |
Here is the result:
|
CustomerID = ALFKI CustomerID = BERGS CustomerID = CENTC .... |
We can also display the data from
the "Customers" table inside an HTML table with the following lines
(remember to save the file with an .asp extension):
<html><body><%set conn=Server.CreateObject("ADODB.Connection")conn.Provider="Microsoft.Jet.OLEDB.4.0"conn.Open "c:/webdata/northwind.mdb"set rs = Server.CreateObject("ADODB.recordset")rs.Open "SELECT Companyname, Contactname FROM Customers", conn%><table border="1" width="100%"><%do until rs.EOF%> <tr> <%for each x in rs.Fields%> <td><%Response.Write(x.value)%></td> <%next rs.MoveNext%> </tr><%looprs.closeconn.close%></table></body></html> |
Here is the result:
|
Alfreds Futterkiste |
Maria Anders |
|
Berglunds snabbköp |
Christina Berglund |
|
Centro comercial Moctezuma |
Francisco Chang |
|
Ernst Handel |
Roland Mendel |
|
FISSA Fabrica Inter. Salchichas |
Diego Roel |
|
Galería del gastrónomo |
Eduardo Saavedra |
|
Island Trading |
Helen Bennett |
|
Königlich Essen |
Philip Cramer |
|
Laughing Bacchus Wine Cellars |
Yoshi Tannamuri |
|
Magazzini Alimentari Riuniti |
Giovanni Rovelli |
|
North/South |
Simon Crowther |
|
|
Marie Bertrand |
|
|
Paula Wilson |
|
Simons bistro |
Jytte Petersen |
|
The Big Cheese |
Liz Nixon |
|
Vaffeljernet |
Palle Ibsen |
|
Wolski Zajazd |
Zbyszek Piestrzeniewicz |
We want to add headers to the HTML
table to make it more readable (remember to save the file with an .asp
extension):
<html><body><%set conn=Server.CreateObject("ADODB.Connection")conn.Provider="Microsoft.Jet.OLEDB.4.0"conn.Open "c:/webdata/northwind.mdb"set rs = Server.CreateObject("ADODB.recordset")sql="SELECT Companyname, Contactname FROM Customers"rs.Open sql, conn%><table border="1" width="100%"> <tr> <%for each x in rs.Fields response.write("<th>" & x.name & "</th>") next%> </tr> <%do until rs.EOF%> <tr> <%for each x in rs.Fields%> <td><%Response.Write(x.value)%></td> <%next rs.MoveNext%> </tr> <%loop rs.close conn.close %></table></body></html> |
Here is the result:
|
Companyname |
Contactname |
|
Alfreds Futterkiste |
Maria Anders |
|
Berglunds snabbköp |
Christina Berglund |
|
Centro comercial Moctezuma |
Francisco Chang |
|
Ernst Handel |
Roland Mendel |
|
FISSA Fabrica Inter. Salchichas |
Diego Roel |
|
Galería del gastrónomo |
Eduardo Saavedra |
|
Island Trading |
Helen Bennett |
|
Königlich Essen |
Philip Cramer |
|
Laughing Bacchus Wine Cellars |
Yoshi Tannamuri |
|
Magazzini Alimentari Riuniti |
Giovanni Rovelli |
|
North/South |
Simon Crowther |
|
|
Marie Bertrand |
|
|
Paula Wilson |
|
Simons bistro |
Jytte Petersen |
|
The Big Cheese |
Liz Nixon |
|
Vaffeljernet |
Palle Ibsen |
|
Wolski Zajazd |
Zbyszek Piestrzeniewicz |
We want to display only the
records from the "Customers" table that have a
"Companyname" that starts with an A (remember to save the file with
an .asp extension):
<html><body><%set conn=Server.CreateObject("ADODB.Connection")conn.Provider="Microsoft.Jet.OLEDB.4.0"conn.Open "c:/webdata/northwind.mdb"set rs=Server.CreateObject("ADODB.recordset")sql="SELECT Companyname, Contactname FROM CustomersWHERE CompanyName LIKE 'A%'"rs.Open sql, conn%><table border="1" width="100%"> <tr> <%for each x in rs.Fields response.write("<th>" & x.name & "</th>") next%> </tr> <%do until rs.EOF%> <tr> <%for each x in rs.Fields%> <td><%Response.Write(x.value)%></td> <%next rs.MoveNext%> </tr> <%loop rs.close conn.close%></table></body></html> |
Here is the result:
|
Companyname |
Contactname |
|
Alfreds Futterkiste |
Maria Anders |
We want to display the
"Companyname" and "Contactname" fields from the "Customers"
table, ordered by "Companyname" (remember to save the file with an
.asp extension):
<html><body><%set conn=Server.CreateObject("ADODB.Connection")conn.Provider="Microsoft.Jet.OLEDB.4.0"conn.Open "c:/webdata/northwind.mdb"set rs = Server.CreateObject("ADODB.recordset")sql="SELECT Companyname, Contactname FROMCustomers ORDER BY CompanyName"rs.Open sql, conn%><table border="1" width="100%"> <tr> <%for each x in rs.Fields response.write("<th>" & x.name & "</th>") next%> </tr> <%do until rs.EOF%> <tr> <%for each x in rs.Fields%> <td><%Response.Write(x.value)%></td> <%next rs.MoveNext%> </tr> <%loop rs.close conn.close%></table></body></html> |
Here is the result:
|
Companyname |
Contactname |
|
Alfreds Futterkiste |
Maria Anders |
|
Berglunds snabbköp |
Christina Berglund |
|
Centro comercial Moctezuma |
Francisco Chang |
|
Ernst Handel |
Roland Mendel |
|
FISSA Fabrica Inter. Salchichas |
Diego Roel |
|
Galería del gastrónomo |
Eduardo Saavedra |
|
Island Trading |
Helen Bennett |
|
Königlich Essen |
Philip Cramer |
|
Laughing Bacchus Wine Cellars |
Yoshi Tannamuri |
|
Magazzini Alimentari Riuniti |
Giovanni Rovelli |
|
North/South |
Simon Crowther |
|
|
Marie Bertrand |
|
|
Paula Wilson |
|
Simons bistro |
Jytte Petersen |
|
The Big Cheese |
Liz Nixon |
|
Vaffeljernet |
Palle Ibsen |
|
Wolski Zajazd |
Zbyszek Piestrzeniewicz |
We want to add a new record to the
Customers table in the Northwind database. We first create a form that contains
the fields we want to collect data from:
<html><body><form method="post" action="demo_add.asp"><table><tr><td>CustomerID:</td><td><input name="custid"></td></tr><tr><td>Company Name:</td><td><input name="compname"></td></tr><tr><td>Contact Name:</td><td><input name="contname"></td></tr><tr><td>Address:</td><td><input name="address"></td></tr><tr><td>City:</td><td><input name="city"></td></tr><tr><td>Postal Code:</td><td><input name="postcode"></td></tr><tr><td>Country:</td><td><input name="country"></td></tr></table><br /><br /><input type="submit" value="Add New"> <input type="reset" value="Cancel"></form></body></html> |
When the user presses the submit
button the form is sent to a file called "demo_add.asp". The
"demo_add.asp" file contains the code that will add a new record to
the Customers table:
<html><body><%set conn=Server.CreateObject("ADODB.Connection")conn.Provider="Microsoft.Jet.OLEDB.4.0"conn.Open "c:/webdata/northwind.mdb"sql="INSERT INTO customers (customerID,companyname,"sql=sql & "contactname,address,city,postalcode,country)"sql=sql & " VALUES "sql=sql & "('" & Request.Form("custid") & "',"sql=sql & "'" & Request.Form("compname") & "',"sql=sql & "'" & Request.Form("contname") & "',"sql=sql & "'" & Request.Form("address") & "',"sql=sql & "'" & Request.Form("city") & "',"sql=sql & "'" & Request.Form("postcode") & "',"sql=sql & "'" & Request.Form("country") & "')"on error resume nextconn.Execute sql,recaffectedif err<>0 then Response.Write("No update permissions!")else Response.Write("<h3>" & recaffected & " record added</h3>")end ifconn.close%></body></html> |
If you use the SQL INSERT command
be aware of the following:
In a MS Access database, you can
enter zero-length strings ("") in Text, Hyperlink, and Memo fields IF
you set the AllowZeroLength property to Yes.
Note: Not all databases support zero-length
strings and may cause an error when a record with blank fields is added. It is
important to check what data types your database supports.
We want to update a record in the
Customers table in the Northwind database. We first create a table that lists
all records in the Customers table:
<html><body><%set conn=Server.CreateObject("ADODB.Connection")conn.Provider="Microsoft.Jet.OLEDB.4.0"conn.Open "c:/webdata/northwind.mdb"set rs=Server.CreateObject("ADODB.Recordset")rs.open "SELECT * FROM customers",conn%><h2>List Database</h2><table border="1" width="100%"><tr><%for each x in rs.Fields response.write("<th>" & ucase(x.name) & "</th>")next%></tr><% do until rs.EOF %><tr><form method="post" action="demo_update.asp"><%for each x in rs.Fields if lcase(x.name)="customerid" then%> <td> <input type="submit" name="customerID" value="<%=x.value%>"> </td> <%else%> <td><%Response.Write(x.value)%></td> <%end ifnext%></form><%rs.MoveNext%></tr><%loopconn.close%></table></body></html> |
If the user clicks on the button
in the "customerID" column he or she will be taken to a new file
called "demo_update.asp". The "demo_update.asp" file
contains the source code on how to create input fields based on the fields from
one record in the database table. It also contains a "Update record"
button that will save your changes:
<html><body><h2>Update Record</h2><%set conn=Server.CreateObject("ADODB.Connection")conn.Provider="Microsoft.Jet.OLEDB.4.0"conn.Open "c:/webdata/northwind.mdb"cid=Request.Form("customerID")if Request.form("companyname")="" then set rs=Server.CreateObject("ADODB.Recordset") rs.open "SELECT * FROM customers WHERE customerID='" & cid & "'",conn %> <form method="post" action="demo_update.asp"> <table> <%for each x in rs.Fields%> <tr> <td><%=x.name%></td> <td><input name="<%=x.name%>" value="<%=x.value%>"></td> <%next%> </tr> </table> <br /><br /> <input type="submit" value="Update record"> </form><%else sql="UPDATE customers SET " sql=sql & "companyname='" & Request.Form("companyname") & "'," sql=sql & "contactname='" & Request.Form("contactname") & "'," sql=sql & "address='" & Request.Form("address") & "'," sql=sql & "city='" & Request.Form("city") & "'," sql=sql & "postalcode='" & Request.Form("postalcode") & "'," sql=sql & "country='" & Request.Form("country") & "'" sql=sql & " WHERE customerID='" & cid & "'" on error resume next conn.Execute sql if err<>0 then response.write("No update permissions!") else response.write("Record " & cid & " was updated!") end if end ifconn.close%></body></html> |
We want to delete a record in the
Customers table in the Northwind database. We first create a table that lists
all records in the Customers table:
<html><body><%set conn=Server.CreateObject("ADODB.Connection")conn.Provider="Microsoft.Jet.OLEDB.4.0"conn.Open "c:/webdata/northwind.mdb"set rs=Server.CreateObject("ADODB.Recordset")rs.open "SELECT * FROM customers",conn%><h2>List Database</h2><table border="1" width="100%"><tr><%for each x in rs.Fields response.write("<th>" & ucase(x.name) & "</th>")next%></tr><% do until rs.EOF %><tr><form method="post" action="demo_delete.asp"><%for each x in rs.Fields if x.name="customerID" then%> <td> <input type="submit" name="customerID" value="<%=x.value%>"> </td> <%else%> <td><%Response.Write(x.value)%></td> <%end ifnext%></form><%rs.MoveNext%></tr><%loopconn.close%></table></body></html> |
If the user clicks on the button
in the "customerID" column he or she will be taken to a new file
called "demo_delete.asp". The "demo_delete.asp" file
contains the source code on how to create input fields based on the fields from
one record in the database table. It also contains a "Delete record"
button that will delete the current record:
<html><body><h2>Delete Record</h2><%set conn=Server.CreateObject("ADODB.Connection")conn.Provider="Microsoft.Jet.OLEDB.4.0"conn.Open "c:/webdata/northwind.mdb"cid=Request.Form("customerID")if Request.form("companyname")="" then set rs=Server.CreateObject("ADODB.Recordset") rs.open "SELECT * FROM customers WHERE customerID='" & cid & "'",conn %> <form method="post" action="demo_delete.asp"> <table> <%for each x in rs.Fields%> <tr> <td><%=x.name%></td> <td><input name="<%=x.name%>" value="<%=x.value%>"></td> <%next%> </tr> </table> <br /><br /> <input type="submit" value="Delete record"> </form><%else sql="DELETE FROM customers" sql=sql & " WHERE customerID='" & cid & "'" on error resume next conn.Execute sql if err<>0 then response.write("No update permissions!") else response.write("Record " & cid & " was deleted!") end if end ifconn.close%></body></html>
| Print Page | Close Window
|