ASP Database Fundamentals (Part 4)

In this series of articles, we have been introduced to using ADO to access a database from with ASP. We have learned how to create an ADO connection, and use that connection to insert data into a database as well as retrieve information... [T]he final two aspects of database interaction that have yet to be addressed are two of the most important. Not many database applications could be complete if they allowed you to merely insert and view records. Without some sort of mechanism to maintain the data within the database, the scope of the application would be quite limited.

Written by
Rating: 4 stars4 stars4 stars4 stars4 stars / 84
October 27, 2003
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement
We are now three quarters of the way around the track and are coming to the home stretch. In this series of articles, we have been introduced to using ADO to access a database from with ASP. We have learned how to create an ADO connection, and use that connection to insert data into a database as well as retrieve information.

What is it we are missing? Well, the final two aspects of database interaction that have yet to be addressed are two of the most important. Not many database applications could be complete if they allowed you to merely insert and view records. Without some sort of mechanism to maintain the data within the database, the scope of the application would be quite limited.

Updating Existing Records

If your database application did not have the ability to update or modify existing database entries, then it would be severely limiting in its potential. For example, imagine having a customer database in which you did not have the ability to change the contact information for a customer. You would not be able to update the customer’s mailing address, phone number, or email address. How about an inventory management system that had an inability to update the price or quantity of an item in stock?

For today’s mainstream database applications, database maintenance is a necessity. To that end, there are two main methods of updating information utilizing ADO. These methods very closely reflect the methods we discussed when learning to insert items into the database. They involve using the recordset object, as well as the connection object directly.

Updating Records with the Recordset Object

As with inserting records into a recordset, updating them has a particular method to facilitate this. The “update” method is used to save modifications to a record within a recordset to the recordset object, as well as to the connection object itself.

Here is an example in the simplest terms:


<%
ConnectionString "DSN=MyDB”

Dim CurrSeq
CurrSeq = 0

set conn = server.createobject("
adodb.connection")
conn.open ConnectionString

set rs = Server.CreateObject("
ADODB.recordset")
rs.open “Customers”, conn


‘ Go through all records updating record sequence
do until rs.EOF
    CurrSeq = CurrSeq + 1
rs(“CustomerSequence”) = CurrSeq
rs.Update
rs.MoveNext
loop

rs.close
set rs = nothing

conn.close
set conn = nothing
%>



As with the addnew method, there are additional ways to utilize the update method.

The Update Method in More Detail

Similar to the addnew method, there are two optional parameters that can be used. These are the fieldlist and the valuelist parameters. These parameters work in the same manner as the ones used by the addnew method.

The fieldlist parameter can be a single field name, or an array of field names, or the numeric (ordinal) position of the fields in the new record. For both the single name and array of names, each name must be enclosed within a pair of double quotes.

The valuelist parameter is a single value or an array of values for the fields that you want to populate in the new record. If the fieldlist parameter is an array, then valuelist must also be an array. Further, the valuelist must have the exact same number of members and be in the same order as the fieldlist.

Consider these examples:


<%

set rs Server.CreateObject("ADODB.recordset")
rs.open “Customers”conn

‘ Method 1
rs
.Update “FirstName”“Rich”

‘ Method 2
rs
.Update Array("FirstName""LastName"), Array("Rich",”Smith

‘ Method 3
myFieldList 
= Array("FirstName""LastName")
myValueList = Array("Rich""Smith")
rs.Update myFieldListmyValueList

rs
.close
set rs 
nothing

conn
.close
set conn 
nothing
%>



The above examples echo the update method’s ability to update information within recordsets quite similarly to the addnew method.

But What if I Goof?

There are times when, after making changes to the contents of a record, you may want to cancel or undo your changes. If you make changes to a record, and then move to another record in the recordset, the update method is implicitly called automatically.

Luckily, there is yet another method of the recordset object called “cancelupdate”. This method will cancel any changes you have made to the current record in the recordset. This only works, however, if you have not used the update method, or moved to another record in the recordset.

For example:


<%
ConnectionString "DSN=MyDB”

set conn = server.createobject("
adodb.connection")
conn.open ConnectionString

set rs = Server.CreateObject("
ADODB.recordset")
rs.open “Customers”, conn

rs(“FirstName”) = “Rich”

if UpdateRecords = “Y” then
    rs.update
else
    rs.cancelupdate
end if

rs.close
set rs = nothing

conn.close
set conn = nothing
%>

Updating Using the Connection Object

As illustrated when we discussed inserting records into the database, the execute method of the connection object can be used to execute a correctly constructed SQL statement. By using this method, you can quickly and easily update database records without the need or overhead of creating a recordset object.

For example:


<%

ConnectionString "DSN=MyDB”

set conn = server.createobject("
adodb.connection")
conn.open ConnectionString

mySql = “Update Customers set Status = ‘Active’, “ & _
         “Profile = ‘Visible’ where CustomerNo = 10”

conn.execute mySql

conn.close
set conn = nothing
%>



Again, this method can be considered a little more daunting than using the recordset object, because it requires at minimum a fundamental knowledge of SQL. It is, however, one of the most efficient and powerful ways to manipulate your data.

Deleting Information

Finally, we are down to the last topic. Deleting or removing data from your database is the last fundamental function we are missing. There are two basic ways to accomplish this using ADO (sound like a pattern is forming here?), just like adding and modifying records.

Using the recordset object, there is a “delete” method to delete the current record. The delete method has an optional parameter, but for the sake of simplicity we are not going to explore it here. When using the delete method keep in mind that the current record is deleted and is still the current record in the recordset. If you attempt to retrieve values from the record, an error will occur. This deleted record will hold its place in the recordset until you move to another record.

An example:


<%
ConnectionString "DSN=MyDB”

set conn = server.createobject("
adodb.connection")
conn.open ConnectionString

set rs = Server.CreateObject("
ADODB.recordset")
rs.open “Customers”, conn


‘ Go through all records deleting “unused” customers
do until rs.EOF
    if rs(“Status”) = “InActive” then
        rs.delete
    end if
rs.MoveNext
loop

rs.close
set rs = nothing

conn.close
set conn = nothing
%>

Deleting Using the Connection Object

And of course, there is the ability to delete records using the handy “execute” method of the connection object and a little SQL as follows:


<%

ConnectionString "DSN=MyDB”

set conn = server.createobject("
adodb.connection")
conn.open ConnectionString

mySql = “Delete from Customers where Status = ‘InActive’ “         

conn.execute mySql

conn.close
set conn = nothing
%>



As you can see, the above will delete in one simple statement all records that have an inactive status.

Where Do We Go From Here?

There are many additional properties and methods associated with the connection and recordset objects that can be utilized to gain additional control over your database manipulations. I would suggest additional reading about the ADO objects if you believe you could benefit from these additional features.

I hope in this series of articles I have given you the knowledge you need to begin creating database applications using ADO and ASP. By using the right combinations of database management code, there is virtually no limit to what you can achieve in your future database applications.
blog comments powered by Disqus
ASP ARTICLES

- Using MySQL with ASP
- ADO for the Beginner
- ADO.NET 101: Data Rendering with a DataGrid ...
- Introducing SoftArtisans OfficeWriter 3.0 En...
- Getting Remote Files With ASP
- The Real Basics of Functions in ASP
- Enhancing Readability with ASP
- Mimicking PHP's String Formatting Functions
- Windows Server Hacks 12, 77, and 98
- How to Sort a Multi-Dimensional Array
- Developing an Information Management Tool wi...
- What are Active Server Pages?
- Getting Remote Pages with ASP
- FTP’ing Files with ASP
- Apply Single-Sign-On to Your Application

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials