Setting up an ODBC data source dynamically in the Delphi program

Source: Internet
Author: User
Tags comments dsn odbc advantage

In the Delphi database application, we usually have two ways to access the database. One is through the BDE database search engine, that is, using Delphi's own database driver, the advantage of this method is fast, but the scope of application is limited, when the database version is updated, it may not be used to operate the new database, another way is through ODBC, the advantage of this method can be with the operating system ( such as Windows) provides, as a standard interface to adapt to a variety of databases, the disadvantage is slow speed. In programming, we can choose one of these methods as needed.

When you use ODBC to access a database, the usual method is to set up an ODBC system Data source (System DSN) in the ODBC Administration panel, and then set up a database alias (alias) in the DBD or in the program to be DSN, so that you can manipulate the database as you wish. I believe that the use of Delphi database application programmers have been very familiar with this aspect, I do not say more. In practical applications, the author has encountered such a situation, our database applications rely on ODBC system data sources to access and manipulate the database, the application has been running well until one day, A user who is familiar with the Windows system, but not very proficient, accidentally modifies or deletes our pre-set system DSN ...

Therefore, the author began to study how to dynamically set up the contents of the ODBC System DSN in the program, so as to increase the robustness of their own programs. After a whole day of research on the Windows registry, finally found the ODBC Management program to set DSN Secret ("Heaven and earth own justice, pay always return!") ", not advertising!" ), is now written to share with you, please advise the master.

ODBC Administrator set the secret of DSN in the registry, do not believe you can go to HKEY_LOCAL_MACHINE\Software\ODBC to see, it certainly makes you feel half done.

First take a look at the installed ODBC database driver in the system. In Hkey_local_machine\software\odbc\ Odbcinst.ini, information about the installed ODBC database driver is stored, from which information such as DLL files for the installed ODBC database driver can be found. In each key value of the ODBCINST.INI\ODBC drivers, the key name is the driver name (such as Microsoft Access Driv (*.mdb)) and the key value is "Installed", which indicates that the driver is installed. In Odbcinst.ini\drivername (DriverName is a driver name, such as Microsoft Access Driver (*.mdb)), there is more information about the driver, This is where we get the path and file name of the DLL file for the ODBC driver, which is the key value of the key name driver, typically "C:\WINDOWS\SYSTEM\FileName.DLL".

Then look at the registration information for the System DSN, in HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC. INI, we have the registration information for the System DSN, and the DSN parameters we set in the ODBC Admin panel are here.

Here's a look at lip ㄒ Huan 鯫 dbc system DSN, which is how the ODBC Administrator registers DSN information in the registry after we complete the parameter settings in the ODBC Admin panel. For example, to create a system DSN named Myaccess for Ms Access97 type, the parameters we specify are primarily database types (Microsoft Access Driver (*.mdb)), data source name (myaccess), Data source description ( My Access, database path (C:\Inetpub\wwwroot\Test.mdb), other parameters such as user name, user password, exclusive, read-only, system database, default directory, buffer size, number of scanned rows, page timeout, etc., using system default parameters. At this point, the registration system DSN should generally have the following steps:

1. In Hkey_local_machine\software\odbc\odbc. Add a string key value to the INI\ODBC data sources, myaccess = Microsoft Access Driver (*.mdb), which is the data source name and database type respectively. This is to register a system DSN name in the registry.

2. In Hkey_local_machine\software\odbc\odbc. INI (subkey) myaccess, create a key to HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC. Ini\myaccess, and then create some key values under it, detailed description of a System DSN configuration information, the main information ([] in the content of the author comments):

Dbq=c:\inetpub\wwwroot\test.mdb

[String, representing database path]

description= My Access

[String, representing database description]

Driver=c:\pwin98\system\odbcjt32.dll

[String, indicating driver, visible Odbcinst.ini]

driverid=0x00000019 (25)

[number, indicating driver ID, cannot be changed]

Fil=ms Access;

[String, possibly related to filtering filter]

safetransaction=0x00000000

[number, which may indicate the number of supported transactional operations]

Uid= ""

[String, representing the user name, here is an empty string]

3. In Hkey_local_machine\software\odbc\odbc. Ini\myaccess creates a subkey (subkey) engines, and then creates a subkey (subkey) Jet under it, creating a key for HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC. Ini\myaccess\engines\jet, and then create some key values under it, detailing the database engine configuration information for a System DSN, with the main information ([] in the author's comments):

Implicitcommitsync=yes

[string, which may indicate whether data modifications are immediately reflected]

MAXBUFFERSIZE=0X00000200 (512)

[number, indicating buffer size]

PAGETIMEOUT=0X00000005 (5)

[Number, indicating page timeout]

THREADS=0X00000003 (3)

[number, which may indicate the maximum number of threads supported]

Usercommitsync=yes

[string, which may indicate whether data modifications are immediately reflected to the user]

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.