In ado, how does one use a connection object? What is a connection object?

Source: Internet
Author: User
Tags odbc
What is a connection object?
A connection object describes the physical connection to the data source. You can use odbc or ole db to connect to the data source. When you open a connectiont object, you attempt to connect to the database. The state attribute of the Connection object tells us whether the Connection is successful. Use the execute method of the connection object to send SQL statements to the data source or run the stored procedure. If the command you send to the data source requires the data source to return the record set, the record set object will be automatically created. After you connect to the database, you can close the connection object.
What are the methods and attributes of the Connection object?
The following table lists some common connection methods.
Method
Description
Open
Open a data source connection
Close
Close the connection to the data source and related objects
Execute
Execute a query (SQL language name, stored procedure, or data provider-specific text)
BeginTrans
Start a new transaction
CommitTrans
.
Save some changes or the current transaction to start a new transaction
RollbackTrans
Cancels some changes in the current transaction and ends the transaction to start a new transaction.
The following table lists the attributes of some common CONNECTION objects.
Attribute
Description
ConnectionString
Contains information about establishing a connection to the data source.
ConnectionTimeout
Displays the time spent trying to establish a connection to the data source and generating errors.
CommandTimeout
Display the time taken to execute this command before an attempt is interrupted or an error is returned.
State
Indicates whether it is connected to the data source, closed, or in connection
Provider
Display the name of the connection provider
Version
Show ado version number
CursorLocation
Sets or returns the value of a provider's cursor function.
How to connect to the data source using the connection object?
To use a connnection object, you only need to specify a connection string to specify the data source you want to join and then call the open method to establish a connection.
The information provided by connection string can be easily connected to data through the OPEN method. If you decide to use the connection object for work, you can use its STATE attribute. If the connection object is opened, the returned value is adstateopen. If not, the returned value is adstateclosed. The following example uses odbc to establish a connection with SQL.
Sub ConnectionExample1 ()
Dim cnn As ADODB. Connection
Set cnn = New ADODB. Connection
'Open the connection with ODBC.
Cnn. Open "Pubs", "sa ",""
'Check whether the connection is complete
If cnn. State = adStateOpen Then
MsgBox "Welcome to Pubs! "

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.