ASP Database Fundamentals (Part 3)

So far in this series, we've been learning to use ADO from within ASP. We have gone over how to make connections, and learned how to extract data using recordsets. In this installment, we will go over the various methods of adding information to a database using ADO.

Written by
Rating: 5 stars5 stars5 stars5 stars5 stars / 40
September 30, 2003
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

There are two common methods of adding data to a database using ADO. The first method is by using additional properties of the recordset object. This method is easy to understand, but does have it’s drawbacks. The other more confusing method is to utilize the connection object directly. We will explore each method, and discuss the when it is best to use each of them.

More To Do With Recordsets

We’ve learned so far how to use recordsets to retrieve data, but they work just as well when adding data to a database. In order to use this method though, your database provider needs to support updateable recordsets. The key to inserting a record is the “addnew” method of the recordset object. The simplest way to use this method is without any arguments. This allows you to create an empty record, modify its contents, and then update it to the database. Here’s an example:

 <%
ConnectionString "DSN=MyDB”

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

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

rs.addnew
rs(“FirstName”) = ‘Rich’
rs(“LastName”) = ‘Smith’
rs.update

rs.close
set rs = nothing

conn.close
set conn = nothing
%>

The Addnew Method In More Detail

When using the addnew method to create new records in a recordset object, there are two optional parameters that can be used. These are the fieldlist and valuelist parameters, respectively. 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
.addnew “FirstName”“Rich”

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

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

rs
.close
set rs 
nothing

conn
.close
set conn 
nothing
%>

The above examples all use the addnew method to create records without the need to implement a bunch of code just to add records to a database.

What About The Connection Object?

At the beginning of the article, I did mention the ability to use the connection object to add records. In order to use this method, you will need to know a little SQL. In essence, instead of adding new records to a recordset object we will formulate the appropriate SQL statement to do the work and hand it directly to the connection object for processing. This is done using the “execute” method of the connection object. For example:

 <%

ConnectionString "DSN=MyDB”

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

mySql = “Insert into Customers (FirstName, LastName) values (“
mySql = mySql & “’Rich’, ‘Smith’)”

conn.execute mySql

conn.close
set conn = nothing
%>

As you can see, this method is a little more complicated because it requires some knowledge of SQL.

Which Is The Right One To Choose

The aforementioned methods of inserting data into a database will all achieve the same results, but which one do you use? Well, that really depends on your knowledge of databases and your application requirements. For example, using the recordset properties to add records to the database is good to use if the database resides locally on the same machine, or if you are not very proficient in SQL. While using the connection object properties is more confusing, it is extremely lightweight and very efficient.

What’s Next?

We’ve come quite a way thus far, in using ADO to manipulate the data in a database. We have learned to create connections, as well as how to add and retrieve data. When creating ASP applications, this should cover 85% or more of your database needs. In the next and final installment in this series, we will learn how to maintain the data in our database. This includes how to update or change the information that exists in the database, as well as completely remove records that have previously been added. About the author:
Rich Smith owns and operates Jamsoft Development, a programming firm who specializes in custom systems for small businesses.
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