Connection Object : As we already know that the connection object is used for database connection.We need the following code to connect to object :
Set conn = createobject(“Adodb.connection”)
Once the object is created, we need to set connection string to connect to the database. We can define a connection string with details of database including database provider, database, and user credentials for access.
Some useful methods and properties of connection object are as follows:
Properties:
conn.connectionstring – This property sets or returns the details used to create a connection to a data source. Includes details of driver, database,username and password.
conn.ConnectionTimeout – this defines the time to wait for a connection to be established. If in the given time frame the connection is not establised it stops.
conn.provider – This sets or gets connection provider name.
conn.state – gives status whether connection is on or off.
commandtimeout - Set/get the no. of seconds to wait while attempting to execute a command . Default is 30- sec
Methods:
conn.open – opens a database connection based on the connection string provided. conn.Open connectionstring,userID,password,options
conn.Execute – execute the sql statement provided
conn.execute “Select * from table”
conn.close – This closes the adodb connection.
RecordSet Object:
Once a connection has been established, we can create recordset object to hold a set of record from database. A recorset consists of records and column
Set rs = createobject(“”Adodb.recordset”)
Some useful methods and properties of RecordSet Objects are as follows:
Properties:
BOF property returns True if the current record position is before the first record in the Recordset,
EOF property returns True if the current record position is after the last record in the Recordset, otherwise it returns False. For a empty recordset,i.e no records in the recordset or empty recordset, value of BOF and EOF is false. So the property can be used in a loop to validate RecordSet does not return any records.
MaxCount Property returns the maximum value of records to be returned in a recordset.
rs.MaxCount = 20 will return 20 rows of data in recordset.
Methods:
rs.cancel – cancels an existing execution.
rs.clone – returns a clone of existing recorset and assigns to an object
set rsclone = rs.clone
rs.Close - closes instance of recordset
rs.open – opens a recordset based on query specified.
rs.open sqlquery, conn
where sqlquery is query executed and conn is connection object.
rs.move – moves the pointer in a recordset by specified count as defined in numrec
rs.move numrec, start.
Also movenext,moveprevious, movefirst, movelast can be used to move to specified location in recordset.
Fields : it is used to point to any particular field . It represents any column of recordset object.
rs.fields.count gives number of items in the fields collection.
rs.field.item(i) returns specified item from the collection.