Use ADO object to Access ODBC data in Delphi program

Source: Internet
Author: User
Tags dsn odbc ole access database

As an ASP enthusiasts, the author often use ADO object to manipulate the ODBC database in ASP pages, I think it is very convenient to use ASP to create Web application system. Although in the programming career, the author prefers Borland series products, to Microsoft product a bit repel, to ASP is an exception. One day, brainwave, ADO object is a standard OLE object, if in Delphi application can use ADO operation database, should be quite good. Especially in the use of Delphi Network database application, if the Web site is Winnt site and support ASP page, you can use ADO Object Access ODBC database, and do not have so large BDE upload to the site up, This can take full advantage of Delphi and ASP programming advantages, make better isapi/nsapi/cgi.

After programming and testing, in Delphi can successfully use ADO Object Access ODBC database, now will use experience to write out, share with you, let us more than one access to the ODBC database method.

In 32-bit Delphi, you can declare a Variant variable (such as Avariant) and then create an OLE object, such as Avariant:=createoleobject (ADODB), by Createoleobject. Connection), you can obtain an instance of a database connection object, and then you can manipulate the ODBC database by using the object's methods and properties. adjourned

The following is a brief introduction to the ADO objects and their methods and properties used to access the ODBC database.

1. Database Connection object (ADODB. Connection)

This object is used to establish a connection to an ODBC database, and all operations on the database are performed through that connection. The role of database connection to Adodb.connection is like the Tdatabase object in Delphi. The method for establishing a Connection object is (Aconnection is a Variant type variable):

Aconnection:=createoleobject (ADODB. Connection)

The method used to establish the connection is open, using syntax (as an example of an object aconnection):

Aconnection.open (ConnectionString, UserId, Password)

All three parameters are string types, where UserID and password are user names and user passwords, used when accessing the database, and can be omitted because the user name and user password can also be specified in ConnectionString. ConnectionString is a string that describes ODBC data source information in the format:

Provider=ProviderName;DSN=DSNName;
DRIVER=driver; SERVER=server;
DATABASE=database;
UID=user; PWD=password

Where: Provider: Data provider, default for MSDASQL, for Microsoft OLE DB, usually omitted

DSN: The database to open? OBDC system Data Source (DSN), optional parameter

DRIVER: The name of the driver to use for the database to open, such as access corresponds

Microsoft Access Driver (*.mdb) is an optional parameter

Server: The names of the servers on which the database is to be opened, native available (local), optional parameters

DB: The name of the database to open is an optional parameter

UID: User name, used to access database, optional parameter

PWD: User password, used to access the database, optional parameters

All of the above parameters are optional, but sufficient information must be provided to describe a system data source. If you have defined an ODBC system DSN with the name MyDSN, you can use the following statement to establish a database connection:

Aconnection.open (DSN=MYDSN);

To prevent the DSN from not being present or the application running incorrectly if its settings are modified by others, you can use Adodb.connection to create a temporary ODBC data source that guarantees that the parameters of the System DSN we use are set correctly. The following statement creates a temporary ODBC system DSN that corresponds to an Access database with the path

C:\Inetpub\wwwroot\test.mdb

Aconnection.open (Driver={microsoft

Access Driver (*.mdb)};D bq=

C:\inetpub\wwwroot\test.mdb)

After establishing a adodb.connection, if you do not need to return the results of the operation (such as delete, modify, update, etc.) you can do normal SQL operation of the database, at this time apply adodb.connection Another method execute, using the syntax is:

Aconnection.execute (strSQL);

Where strSQL is the SQL statement that executes the action, such as a delete operation can be: Delete from Wfjcommu to close a database connection with Aconnection.close.

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.