ASP Database Fundamentals (Part 2) - How About SQL?
(Page 5 of 9 )
Specifying a tablename in your open method is not the only way to specify data for your recordset. You can also use SQL to determine the precise data you wish to retrieve. Using SQL, you can specify the exact rows and columns to be returned.
Here is an example how to accomplish the same results with SQL, as we did in our last example by specifying a table name:
<%
ConnectionString = "DSN=MyDB”
set conn = server.createobject("adodb.connection")
conn.open ConnectionString
set rs = Server.CreateObject("ADODB.recordset")
rs.open “select * from Customers”, conn
‘ Logic to manipulate data here
rs.close
set rs = nothing
conn.close
set conn = nothing
%>
You will notice that not a lot changed between the two examples. But if we make a small change to the SQL statement, the data returned can vary greatly. For example:
rs
.open “select * from Customers where status = ‘Active’”, conn
You will notice in the above line I added a WHERE condition to the SQL statement. This specifies to ADO exactly which records should be placed into the recordset. This article is not going to go into the intricacies of SQL, and will assume that you are familiar with at least the very basics of SQL.
Next: Do I Have Data Now? >>
More ASP Articles
More By Rich Smith