Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Stardeveloper RSS Feed
Newsletter
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:

You can follow Faisal Khan on Twitter
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : .NET : ASP : Connections, Commands and Procedures
 

Connections, Commands and Procedures

by Wrox Press.

In the previous chapter, we looked at the basics of ADO, concerning ourselves mainly with the Recordset object and the manipulation of data. In most of the examples, we obtained data by simply specifying the name of a database table, but as you've seen from the object model, ADO has other objects that allow data to be accessed.

In this chapter we are going to look at some of these in more detail. In particular, we shall examine:

  • The Connection object, and how it can be used to retrieve data and run commands.
  • The Command object, why it is necessary, and what facilities it has.
  • How to run stored procedures, especially those that require parameters.
  • Some simple optimization techniques for improving ADO performance.
  • What Data Shaping is, and how it can be used.

Like the Recordset object, we're not going to cover all of the methods and properties of the objects involved. Instead, we're going to cover the most important topics, and those methods and properties that are most applicable for an ASP developer.

The Connection Object
We mentioned in the previous chapter that the Connection object is what gives us a connection to the data store, but that's not all the Connection object does. As well as storing details of the connection (such as the type of data store and the features it supports), we can use the connection to run commands.

These commands can be action queries, such as updates, inserts or deletes, as well as commands that return a recordset. You might wonder what use this is, since we have the Recordset object, but it's all part of the flexibility of ADO, that allows you to use whichever object is the most convenient, and most suited to the task in hand.

The commands run from the Connection object are generally action queries, but it's useful to know that you can get recordsets returned too.

To return a recordset from the Connection object you use the Execute method. The syntax of this method is:

Connection.Execute CommandText, [RecordsAffected], [Options]

The arguments are:

Argument Descriptions
CommandText The text of the command to execute. This is the same as the Source of the Recordset Open method, and can also represent an existing Command Object.
RecordsAffected A variable into which is placed a number of records affected by the Command.
Books The Command options, which can be one or more of the values from the CommandTypeEnum or ExecuteOptionEnum constants, as detailed in the previous chapter.

The Execute method optionally returns a recordset, in which case you simply assign the Recordset variable as the return value. For example:

		Set conPubs = Server.CreateObject("ADODB.Connection")
		conPubs.Open strConn
set rsAuthors = conPubs.Execute ("Authors")

You might wonder what the difference is between using the Execute method of the Connection object and the Open method of the Recordset object. It may not seem that there's much difference, but remember that with the Open method of the Recordset you have the ability to change the cursor type and lock type of the resulting recordset. These options are not available for the Execute method of the connection, so you will always get a forward-only, read-only recordset.

Action Commands
If you are running action commands, such as a SQL UPDATE statement, then you can use the RecordsAffected argument to find out how many records were affected by the command. For example:

		Dim strSQL As String
		Dim lngRecs As Long
		strSQL = "UPDATE Titles SET Price = Price * 1.10" & _
			" WHERE Type = 'Business'"
		conPubs.Execute strSQL, lngRecs, adCmdText
		Response.Write lngRecs & " records were updated."

This updates the price for all business books by 10%. Once the Execute command has completed, the number of titles affected by the update will be available in lngRecs - this is the RecordsAffected argument.

Notice that we've specified adCmdText for the options of this command - this tells ADO that the command text is a text command. Whilst this isn't strictly necessary, it does allow ADO to know ahead of time what sort of command is being executed, and therefore improves performance. Remember that this value can be one or more of the values from the CommandTypeEnum values.

No Recordset Returned
If no recordset is being returned, as in the example above, then it's also best to add another option to the Execute statement:

conPubs.Execute strSQL, lngRecs, adCmdText + adExecuteNoRecords

Using adExecuteNoRecords tells ADO that the command being executed does not return any records. ADO therefore doesn't bother building a recordset. If you omit this option then ADO builds an empty recordset, which is clearly a waste of time, so adding the option will speed up the statement.

The Command Object
The Command object is designed specifically to deal with commands of any sort, but especially those that require parameters. Like the Connection object, the Command object can run both commands that return recordsets as well as those that don't. In fact, if your command doesn't have parameters, then it really doesn't matter whether you use a Connection, a Command, or a Recordset.

Returning Recordsets
For a recordset-returning command you would use the Execute method. However, unlike the Connection object, you do not specify the command text in the Execute method itself - you have to use the CommandText property:

		Set cmdAuthors = Server.CreateObject("ADODB.Command")
		cmdAuthors.CommandText = "Authors"
		Set rsAuthors = cmdAuthors.Execute

This is the simplest way to tell the Command object to run a simple command that returns a read-only recordset.

The Execute method also has some optional arguments:

Argument Descriptions
RecordsAffected A variable into which is placed the number of records affected by the command.
Parameters An array of parameter values.
Options The command options. This is similar to the Options of the Recordset Open method.

In the previous chapter, we looked at the basics of ADO, concerning ourselves mainly with the Recordset object and the manipulation of data. In most of the examples, we obtained data by simply specifying the name of a database table, but as you've seen from the object model, ADO has other objects that allow data to be accessed.

In this chapter we are going to look at some of these in more detail. In particular, we shall examine:

  • The Connection object, and how it can be used to retrieve data and run commands.
  • The Command object, why it is necessary, and what facilities it has.
  • How to run stored procedures, especially those that require parameters.
  • Some simple optimization techniques for improving ADO performance.
  • What Data Shaping is, and how it can be used.

Like the Recordset object, we're not going to cover all of the methods and properties of the objects involved. Instead, we're going to cover the most important topics, and those methods and properties that are most applicable for an ASP developer.

The Connection Object
We mentioned in the previous chapter that the Connection object is what gives us a connection to the data store, but that's not all the Connection object does. As well as storing details of the connection (such as the type of data store and the features it supports), we can use the connection to run commands.

These commands can be action queries, such as updates, inserts or deletes, as well as commands that return a recordset. You might wonder what use this is, since we have the Recordset object, but it's all part of the flexibility of ADO, that allows you to use whichever object is the most convenient, and most suited to the task in hand.

The commands run from the Connection object are generally action queries, but it's useful to know that you can get recordsets returned too.

To return a recordset from the Connection object you use the Execute method. The syntax of this method is:

Connection.Execute CommandText, [RecordsAffected], [Options]

The arguments are:

Argument Descriptions
CommandText The text of the command to execute. This is the same as the Source of the Recordset Open method, and can also represent an existing Command Object.
RecordsAffected A variable into which is placed a number of records affected by the Command.
Books The Command options, which can be one or more of the values from the CommandTypeEnum or ExecuteOptionEnum constants, as detailed in the previous chapter.

The Execute method optionally returns a recordset, in which case you simply assign the Recordset variable as the return value. For example:

		Set conPubs = Server.CreateObject("ADODB.Connection")
		conPubs.Open strConn
set rsAuthors = conPubs.Execute ("Authors")

You might wonder what the difference is between using the Execute method of the Connection object and the Open method of the Recordset object. It may not seem that there's much difference, but remember that with the Open method of the Recordset you have the ability to change the cursor type and lock type of the resulting recordset. These options are not available for the Execute method of the connection, so you will always get a forward-only, read-only recordset.

Action Commands
If you are running action commands, such as a SQL UPDATE statement, then you can use the RecordsAffected argument to find out how many records were affected by the command. For example:

		Dim strSQL As String
		Dim lngRecs As Long
		strSQL = "UPDATE Titles SET Price = Price * 1.10" & _
			" WHERE Type = 'Business'"
		conPubs.Execute strSQL, lngRecs, adCmdText
		Response.Write lngRecs & " records were updated."

This updates the price for all business books by 10%. Once the Execute command has completed, the number of titles affected by the update will be available in lngRecs - this is the RecordsAffected argument.

Notice that we've specified adCmdText for the options of this command - this tells ADO that the command text is a text command. Whilst this isn't strictly necessary, it does allow ADO to know ahead of time what sort of command is being executed, and therefore improves performance. Remember that this value can be one or more of the values from the CommandTypeEnum values.

No Recordset Returned
If no recordset is being returned, as in the example above, then it's also best to add another option to the Execute statement:

conPubs.Execute strSQL, lngRecs, adCmdText + adExecuteNoRecords

Using adExecuteNoRecords tells ADO that the command being executed does not return any records. ADO therefore doesn't bother building a recordset. If you omit this option then ADO builds an empty recordset, which is clearly a waste of time, so adding the option will speed up the statement.

The Command Object
The Command object is designed specifically to deal with commands of any sort, but especially those that require parameters. Like the Connection object, the Command object can run both commands that return recordsets as well as those that don't. In fact, if your command doesn't have parameters, then it really doesn't matter whether you use a Connection, a Command, or a Recordset.

Returning Recordsets
For a recordset-returning command you would use the Execute method. However, unlike the Connection object, you do not specify the command text in the Execute method itself - you have to use the CommandText property:

		Set cmdAuthors = Server.CreateObject("ADODB.Command")
		cmdAuthors.CommandText = "Authors"
		Set rsAuthors = cmdAuthors.Execute

This is the simplest way to tell the Command object to run a simple command that returns a read-only recordset.

The Execute method also has some optional arguments:

Argument Descriptions
RecordsAffected A variable into which is placed the number of records affected by the command.
Parameters An array of parameter values.
Options The command options. This is similar to the Options of the Recordset Open method.

 ( 8 Remaining ) Next

See all comments and questions (post-ad) posted for this tutorial.


Buy This Book From Amazon
Title: Professional Active Server Pages 3.0
Publisher: Wrox Press Inc
Price: $59.99
Pages: 1277
DatePublished: September 1999



Comments/Questions ( Threads: 2, Comments: 2 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. help in connecting oracle sql server to asp
  2. Where is the source code ?!

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed to.

 
© 1999 - 2009 Stardeveloper.com, All Rights Reserved.