Asp Connection Object
Creating
connection to DB |
Methods | Properties |
We have seen that
connection is what links ASP script to Datastore and ADO is the
way we actually get data to and from the data store.The Connectionobject
is what ADO uses to store our information about data store connection.A Connection
object represents a unique session with a data store and we can have different
connection to different datastores at the same time.
Asp – Creating connection to dB
Before making
a connection to dB we need the following information.
Provider | type of OLE-DB
provider.By default provider is ODBC |
Driver | ODBC driver
such as Access or SQL Server |
Server | Which server
to connect to |
DSN | name of the
data source |
Database/DBQ | actual database
name |
USERID | optional.This
is user name |
PASSWORD | optional.Password
for user |
We can open connection
to dB using following syntax:
<%
Dim objConn
Set objConn = Server.CreateObject(“ADODB.Connection”)
objConn.Open “Driver = {Microsoft Access
Driver (*.mdb);DBQ=C;\Inet\wwwroot\EgAsp\dbname.mdb”
%>
There
is another way of opening connection by using DSN.
<%
objConn.Open “DSN = name”%>
or
<%
strconnection = “Driver = {Microsoft Access
Driver (*.mdb);DBQ=C;\Inet\wwwroot\EgAsp\dbname.mdb”
objConn.Connectionstring = strconnection
objconn.Open
%>
Asp Connection Object – Methods
Method | Syntax | Description |
Open | objConn.Open (ConnectionString, UserID, Password, Options) | Opens a connection to a data source. UserId , Password and
options are optional |
Close | objConn.Close | close a Connection object and any active Recordset objects
associated with the connection. |
Execute | objConn.Execute (CommandText, RecordsAffected, Options ) | Executes the specified query, SQL statement, stored procedure,
or provider-specific text. Where CommandText is a String value that contains the SQL statement, table name, stored procedure, a URL, or povider-specific text to execute, RecordsAffected is a variable to which the provider returns the number of records that the operation affected and Options indicates how the provider should evaluate the CommandText argument. RecordsAffected and options are Optional. |
Cancel | objConn.Cancel | Cancels execution of a pending, asynchronous method call.The
last asynchronous call to this method is Execute and open. |
Asp Connection Object – Properties
Property | Syntax | Description |
ConnectionString | objConn.ConnectionString=value | specifies a data source by passing a detailed connection
string containing a series of argument = value statements separated by semicolons. |
ConnectionTimeOut | objConn.TimeOut=40 | Indicates how long to wait while establishing a connection
before terminating the attempt and generating an error.Default is 15. |
Provider | ObjCcnn.Provider = “Microsoft.Jet.OLEDB.3.51” | Indicates the name of the provider for a Connection object |
State | objConn.state | Indicates for all applicable objects whether the state of
the object is open or closed. |
Version | objConn.Version | Indicates the ADO version number |