ASP Getting Started tutorials-connection objects

Source: Internet
Author: User
Tags mdb database ole reset table name create database

1, Connection object Brief

The Connection object represents an open connection to the data source, as if a data transmission line was established in the application and the database that represents the only session with the data source. ASP uses ADO to carry on various operations to various data sources, in which, Connection object is essential, on this basis can use Command object and Recordset object to insert, delete, the database that the Connection object connects to, Updates, queries, and so on.

2, Common properties of Connection objects

Common Properties < Tables > of Connection objects

Property Describe
CommandTimeout This means that the maximum time frame to run an SQL command using the Execute method can be interrupted and an error is generated. The default value is 30 seconds, and setting to 0 means there is no limit.
ConnectionString Set up information to connect to the data source, including Fliename, Password, Userld, DataSource, Provider, and other parameters.
ConnectionTimeout Sets the time that is waiting to establish a database connection before terminating an attempt and generating an error, which sets or returns a Long value (in seconds) indicating when the connection is open, with a default value of 15. Setting this property to 0,ado will wait indefinitely until the connection is opened.
DefaultDatabase Defines the connection default database.
Mode Before establishing a connection, set how the connection is read and write, and determine whether the current data can be changed. 01 do not set (default), 11 read-only, 21 write, 31 read and write.
Provider Sets the data provider for the connection (database management program), the default is Msdasql (MICROSOT-ODBC for OLE DB)
State Reads the status of the current linked object, takes 0 to close, and 1 indicates that it is open.

3, the method of Connection object

Methods of Connection Objects < table two >

Method Description/Format/annotation
Open Establishes a connection object to the data source.
Connobject.cpen Connectionstring,userid,password
1, Connectionstring is optional parameter, it is a string variable, contains the information of the connection.

2, UserID is optional parameter, it is a string variable containing the user name used to access the database when the connection is established.

3, Password is optional parameter, it is a string variable, contains the password to access the database when the connection is established.

Close Closes the connection to the data source and frees the system resources associated with the connection.
Connobject.close
Closes the Connection object with the Close method and does not delete the object from memory. Therefore, a closed Connection object can also be opened with the open method without having to create a Connection object again. Also, when a connection is closed using the Close method, all active Recordset objects for this connection are closed. However, the command object associated with this connection will not be affected, except that the command object is no longer involved in the connection. You can use:

The Set connobject=nothing command frees all resources that are occupied by the Connection object.

Execute Execute SQL commands or stored procedures to communicate with the database.
Format with return records: Set Rs=connobject.execute (commandtext,recordsaffected,options)

No return record format: Connobject.execute commandtext,recordsaffected,options

CommandType is a string that contains a table name, or a SQL statement that will be executed;

Recordsetaffected is an optional parameter that returns the number of records affected by this operation.

Options are optional parameters that specify the nature of the CommandText parameter, which is used to specify how ADO interprets the parameter values of the CommandText parameter, as in the following table:

Options value Meaning description
1 Indicates that the string being executed contains a command text.
2 Indicates that the string being executed contains a table name.
4 Indicates that the string being executed contains a stored procedure name.
8 The contents of the string are not specified (this is the default value)
BeginTrans Begins a new transaction, in which a memory buffer is opened in memory for the transaction.
CommitTrans Commits a transaction, which writes all the changed data in a transaction to the hard disk one time from the memory buffer, ends the current transaction, and may start a new transaction.
RollbackTrans Rollback TRANSACTION, that is, cancels all operations on the data source since the start of this transaction, and ends this transaction operation.

4, to create the database connection steps are as follows:

A to create the Connection object instance in the following format:

Set conn=Server.CreateOreateObject("ADODB.Connection")

b Specify the connection string

conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:MardetData sg1.mdb"      

Or:

' Create an OLE DB connection string

CS="Provider=Microsoft.jet.OLEDB;4.0;Data Source="
conn.ConnectionStrin=CS & Server.MapPath("rsgl.mdb")

Note: The connection string does not contain spaces around the equal sign (=).

(c) Open the database connection

Connobject.Cpen connectionstring,UserID,Password

D Close the connection to the data source

object.Close

(e) Release of all resources occupied by the Connection object

Set Connobject=nothing

5, the Connection object method and the attribute simple application example

1), use the Execute method of the Connection object to create a "employee base table" in the Rsgl.mdb database. The code is as follows (etable):

<% @ language= "VBScript"%>
<title> Creating a new table on an ASP page </title
<body>
<div align= "center"
< HR width= "50%" color= "#cccc99"
<p> creating tables ... </p>
</div>
<!--#include virtual = " /adovbs.inc
<%
Dim cnn,ssql
Set Cnn=server.createobject ("ADODB"). Connection ")
' specifies the connection string, and the default database is master
CNN. connectionstring= "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & Server. MapPath ("Rsgl.mdb")
CNN. Open
ssql= CREATE Table Employee Basics Table (Employee name varchar (10), Department varchar (10), Home address varchar (12), home phone varchar, Email varchar (
Execute CREATE DATABASE command
CNN. Execute Ssql,,adcmdtext
CNN. Close
Set cnn=nothing
%>
<p><center><b> CREATE TABLE succeeded! </b></center></p>
</body>

2, (Add Record page) An instance passes a form page (ers.htm) to submit the data to the Add Records Processing page (ers.asp), where a new record is added at the end of the table by executing the INSERT command using the Connection object's Execute method. The ers.htm code is as follows:

<body>
<form name= "Form1" method= "Post" action= "ers.asp" >
<table align= "Center" border= "1" >
&LT;TR&GT;&LT;TD colspan= "2" align= "center" > Add Record Table </td></tr>
&LT;TR&GT;&LT;TD align= "Right" > Employee Name: &LT;/TD&GT;&LT;TD width= ><input type= "text" name= "Txtname" > </td></tr>
&LT;TR&GT;&LT;TD align= "Right" > Department: </td><td><input type= "text" name= "TXTBM" ></td>< /tr>
&LT;TR&GT;&LT;TD align= "Right" > Home address: </td><td><input type= "text" name= "Txtzz" ></td>< /tr>
&LT;TR&GT;&LT;TD align= "Right" > Home Tel: </td><td><input type= "text" name= "Txttel" ></td> </tr>
&LT;TR&GT;&LT;TD align= "right" >email:</td><td><input type= "text" name= "Txtemail" ></td> </tr>
&LT;TR&GT;&LT;TD align= "center" ><input type= "Submit" value= "submitted" &GT;&LT;/TD&GT;&LT;TD align= "center" >< Input type= "reset" value= "Rewrite all" ></td></tr>
</table>
</form>

The code for the Ers.asp page is as follows:

<body>
<form name= "Form1" method= "Post" action= "ers.asp" >
<table align= "Center" border= "1" >
&LT;TR&GT;&LT;TD colspan= "2" align= "center" > Add Record Table </td></tr>
&LT;TR&GT;&LT;TD align= "Right" > Employee Name: &LT;/TD&GT;&LT;TD width= ><input type= "text" name= "Txtname" > </td></tr>
&LT;TR&GT;&LT;TD align= "Right" > Department: </td><td><input type= "text" name= "TXTBM" ></td>< /tr>
&LT;TR&GT;&LT;TD align= "Right" > Home address: </td><td><input type= "text" name= "Txtzz" ></td>< /tr>
&LT;TR&GT;&LT;TD align= "Right" > Home Tel: </td><td><input type= "text" name= "Txttel" ></td> </tr>
&LT;TR&GT;&LT;TD align= "right" >email:</td><td><input type= "text" name= "Txtemail" ></td> </tr>
&LT;TR&GT;&LT;TD align= "center" ><input type= "Submit" value= "submitted" &GT;&LT;/TD&GT;&LT;TD align= "center" >< Input type= "reset" value= "Rewrite all" ></td></tr>
</table>
</form>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.