ADO Object Model Database Connection Record Set Query Examples Update & Insertion Examples An Application Example
ADO Object Model Database Connection Record Set Query Examples Update & Insertion Examples An Application Example
Record Set
Query Examples Update & Insertion Examples
An Application Example
ADO is a Microsoft technology ADO stands for ActiveX Data Objects ADO is a Microsoft Active-X component ADO is automatically installed with Microsoft IIS ADO is a programming interface to access data in a database ADO can be accessed from within your Active Server Pages
The normal way to access a database from inside an ASP page is to: 1. Create an ADO connection to a database 2. Open the database connection 3. Create an ADO recordset 4. Open the recordset 5. Extract the data you need from the recordset 6. Close the recordset 7. Close the connection
Database Connection
The easiest way to connect to a database is to use a DSN-less connection. A DSN-less connection can be used against any Microsoft SQL database on your web site. If you have a database called "northwind " located in your SQL Server, you can connect to the database with the following ASP code:
1.
<% dim connobj dim connstr connstr = "Provider=SQLOLEDB;Data Source=MyServer;Database=Northwind;User Id=NorthUser;Password=abc;" set connobj = Server.CreateObject("ADODB.Connection") connobj.Open (connstr) connobj.Close set connobj = nothing %> 2. The ADO Connection object is used to create an open connection to a data source. Through this connection, you can access and manipulate a database.
1.Create the file ADORecordset.asp and type in the code below <% dim connobj dim connstr dim recobj connstr = "Provider=SQLOLEDB;Data Source=MYSERVER;Database=Northwind;User Id=NorthUser;Password=abc;" set connobj = Server.CreateObject("ADODB.Connection") connobj.Open (connstr)'this part create and assign the recordset. set recobj = Server.CreateObject("ADODB.Recordset") 'open the table customer using connobj as the default connection. recobj.Open "Customers", connobj Response.Write("Table Customers opened.") connobj.Close recobj.close
Create a file called ADORecordset2.asp and type in the code below: <% dim connobj dim connstr dim recobj connstr = "Provider=SQLOLEDB;Data Source=MYSERVER;Database=Northwind;User Id=NorthUser;Password=abc;" set connobj = Server.CreateObject("ADODB.Connection") connobj.Open (connstr) 'this part create and assign the recordset. set recobj = Server.CreateObject("ADODB.Recordset") 'open the table customer using connobj as the default connection. recobj.Open "Customers", connobj Response.Write("Table Customers opened.<br />") 'retrieve data
for each x in recobj.fields response.write(x.name) response.write(" = ") response.write(x.value & "<br />") next connobj.Close recobj.Close set recobj = nothing set connobj = nothing %>
1.After using the recordset and connection object, it is important to properly close and destroy the objects. 2.Below is the 4 lines necessary to perform such operations. Recobj.close Connobj.close Set recobj = nothing Set connobj = nothing
Recordset Objects
A Recordset object: the entire set of records Opened from a base table