Automatically set an ODBC data source in the Delphi program

zhaozj2021-02-11  179

In the Delphi database application, we usually have two ways. One is to search through BDE database

The engine, that is, using the Delphi's own database driver, the advantage of this method is fast, but the application range is limited.

When the database version is updated, it is possible to operate the new database; another method is to pass ODBC, this method is excellent

Point is to be available with the operating system (such as Windows). As a standard interface can adapt to a variety of databases, the disadvantage is slow. In the editor

When we can choose one of them as needed.

When using an ODBC to access the database, the usual method is an ODBC system data source in the ODBC management panel.

(System DSN), then set a database alias (alias) in DBD or in the program, which should be DSN, so you can

Decontent the database. I believe that programmers who have done database applications with Delphi are very familiar with this, and the author is not much.

said. In practical applications, the author has encountered such a situation, and our database app is based on ODBC system data sources.

If the access and operation database, the application is running well until a day, a more familiar with the Windows system but not

The user who is too proficient is inadvertently modified or deleted the system DSN we pre-set ...

As a result, the author starts researching how to dynamically set the contents of the ODBC system DSN in the program, so you can increase your own

The sturdy is strong. After a whole day, the research on the Windows registry finally found the secret of the ODBC management program to set DSN.

("Heaven and Earth have fair, pay will always return!", Not advertising!), Now written out with you, please expert advice.

The ODBC Manager sets the secret in the registry, you can go to HKEY_LOCAL_MACHINE / SOFTWARE / ODBC

Look, it will definitely make you feel halfway.

First take a look at the ODBC database driver installed in the system. In HKEY_LOCAL_MACHINE / SOFTWARE / ODBC /

In ODBCINST.INI, the information that has been installed has been installed, which can find the installed ODBC from here.

According to information such as DLL files corresponding to the library driver. In the other key values ​​of odbcinst.ini / odbc drivers, the key name is the driver

Name (such as Microsoft Access Driver (*. MDB)), the key value is "installed", indicating that the driver is installed. in

ODBCINST.INI / DriverName (drivername is a driver name, such as Microsoft Access Driver (*. MDB))

In the process of drivers, we mainly get the path and file name of the DLL file corresponding to the ODBC driver, namely

The key value of the key name Driver is generally "c: /windows/system/filename.dll".

Then to see the registration information of the system DSN, in HKEY_LOCAL_MACHINE / SOFTWARE / ODBC / ODBC.INI, the system is stored

DSN's registration information, our DSN parameters set in the ODBC management panel are here.

Let's take a look at the steps to create an ODBC system DSN, that is, after the completion parameter settings in the ODBC management panel, ODBC management

How is the program registered DSN information in the registry. Take the MS Access 97 type of MS Access 97 type name MyAccess as an example. We specify the parameters mainly have 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, scan line, page length, etc.

By default parameters. At this time, the registration system DSN should generally have the following steps:

1. Add a string key in hkey_local_machine / soft / odbc / odbc.ini / odbc data source

Value, for 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. Create a child key (Subkey) myaccess in hkey_local_machine / software / odbc / odbc.ini,

Built a key for hkey_local_machine / software / odbc / odbc.ini / myaccess, then create some

Key value, detail the configuration information of a system DSN, and the main information is ([] content is noted):

DBQ = C: /inetpub/wwrowroot/test.mdb [string, indicating database path]

Description = My Access [string, represents database description]

Driver = c: /pwin98/system/odbcjt32.dll [string indicates the driver, you can see ODBCINST.INI]

Driverid = 0x00000019 (25) [Digital, indicating the driver ID, can not change]

FIL = MS Access; [string, it may be related to filtering filter]

SafeTransaction = 0x00000000 [number, may indicate the number of support for transactional operations]

UID = "" [string, represents the user name, here is empty string]

3. Create a child key (Subkey) Engines in HKEY_LOCAL_MACHINE / SOFTWARE / ODBC / ODBC.INI / MyAccess.

Then create a subkey JET, that is, create a key to

HKEY_LOCAL_MACHINE / SOFTWARE / ODBC / ODBC.INI / MyAccess / Engines / Jet, then create some

Key value, detail the database engine configuration information of a system DSN, and the main information is ([] content as the author):

ImplicitCommitSync = yes [string may indicate whether to reflect data modification immediately]

MaxBuffersize = 0x00000200 (512) [Digital, indicating buffer size]

PageTimeout = 0x00000005 (5) [Digital, indicating page timeout]

Threads = 0x00000003 (3) [Digital, may indicate the maximum number of threads]

UserCommitsync = YES [string may indicate whether to reflect the data to the user immediately]

The above is the basic information of the establishment of a system DSN (other information such as options or advanced options) is also set here, but because the default information is used, there is not listed in the registry, we will operate the registry in the program. You can also add a system DSN or modify its configuration.

In the following example, a system DSN will be established in the above steps, please note the comments in the program.

{********************************************************** ******

In this program, an ODBC system data source (DSN) will be created.

Data Source Name: MyAccess Data Source Description: My New Data Source

Database type: Access97

Correspondence database: c: /inetpub/wwrowroot/test.mdb

*********************************************************** *****}

{Note should contain registry} in the USES statement}

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

VAR

Registertemp: Tregistry;

BDATA: ARRAY [0..0] of byte;

Begin

Registertemp: = Tregistry.create; // Create an instance of a Registry

With registertemp do

Begin

Rootkey: = hkey_local_machine; // Set the root key value is hkey_local_machine

// Find software / odbc / odbc.ini / odbc data sources

If OpenKey ('Software / ODBC / ODBC.INI / ODBC DATA SOURCES', TRUE) THEN

Begin // Register a DSN name

WriteString ('myaccess ",' Microsoft Access Driver (* .mdb));

end

Else

Begin // Creating a key value failed

Memo1.Lines.Add ('increasing ODBC data source failed ");

EXIT;

END;

Closekey;

// Find or create Software / ODBC / ODBC.INI / MyAccess, write DSN configuration information

IF OpenKey ('Software / ODBC / ODBC.INI / MyAccess', True) THEN

Begin

WriteString ('DBQ', 'C: /inetpub/wwwroot/test.mdb'); // Database Director

WriteString ('Description', 'My New Data Source'); // Data Source Description

Writestring ('driver', 'c: /pwin98/system/odbcjt32.dll'); // Driver DLL file

WriteInteger ('driverid', 25); // driver identification

WriteString ('fil ",' ms access; '); // filter basis

WriteInteger ('SafeTransaction', 0); // Supported Transaction Operations

WritestRing ('uid', ''); // User Name

BDATA [0]: = 0;

WriteBinaryData ('Exclusive', BData, 1); // Non-exclusive way

WriteBinaryData ('Readonly', BData, 1); // Non-read-only mode END

ELSE // Creating a key value failed

Begin

Memo1.Lines.Add ('increasing ODBC data source failed ");

EXIT;

END;

Closekey;

// Find or create software / odbc / odbc.ini / myaccess / engines / jet

// Write DSN Database Engine Configuration Information

IF OpenKey ('Software / ODBC / ODBC.INI / MyAccess / Engines / Jet', True) THEN

Begin

WriteString ('ImplicitCommitsync', 'Yes');

WriteInteger ('maxbuffersize', 512); // buffer size

WriteInteger ('Pagetimeout', 10); // Page Time

WriteInteger ('Threads', 3); // Supported thread number

Writestring ('UserCommitsync', 'YES');

end

ELSE // Creating a key value failed

Begin

Memo1.Lines.Add ('increasing ODBC data source failed ");

EXIT;

END;

Closekey;

Memo1.Lines.Add ('Adds new ODBC data source success ");

FREE;

END;

END;

The above program is debugged under PWIN98 Delphi3.0.

Below is the information that creates the DSN of the Common Database Type ([] is comment, except for special comments, each parameter can be seen in the previous description):

1. Access (Microsoft Access Driver (*. MDB))

DBQ, Description, Driver [odbcjt32.dll], driverid [25], fil [ms access;],

Safetransaction [default is 0], UID [default is empty],

Engines / Jet / ImpliCitCommitsync [defaults to YES], Engines / Jet / MaxBuffersize [default 512],

Engines / Jet / PageTimeout [defaults to 512], Engines / Jet / Threads [default is 3],

ENGINES / JET / UserCommitsync [default YES]

Optional setting: systemdb [string, path of system database],

Readonly [binary, is it open in a read-only mode, 1 is Yes, default is 0],

EXCLUSIVE [binary, is it opened in exclusive ways, 1 is Yes, default is 0],

PWD [string, user password]

2. Excel (Microsoft Excel Driver (*. Xls))

DBQ [excel97 (= path / xxx.xls), 5.0 / 7.0 (= path / xxx.xls), 4.0 (= PATH), 3.0 (= PATH)],

Description, Driver [odbcjt32.dll],

Defaultdir [Excel97 (<> DBQ), 5.0 / 7.0 (<> DBQ), 4.0 (= DBQ), 3.0 (= DBQ)],

DriverId [790 (Excel97), 22 (5.0 / 7.0), 278 (4.0), 534 (3.0)],

Fil [Excel5.0;], Readonly, SafeTransaction, UID,

Engines / Excel / ImplicitCommitsync, Engines / Excel / MaxScanRows [Digital, Scanning Row, Default 8], Engines / Excel / Threads, Engines / Excel / UserCommitsync,

Engines / Excel / FirstrowhasName [binary, the first line is domain name, 1 indicating that the default is 1]

Note: Excel97 and Excel7.0 / 5.0 DBQ correspond to an XLS file, and Excel4.0 and Excel3.0 correspond to a directory;

Defaultdir corresponds to a directory, in Excel97 and Excel7.0 / 5.0, the path corresponding to DBQ,

Excel4.0 and Excel3.0 are the same as DBQ; Different DriverIds in each version.

3. DBASE (Microsoft DBase Driver (*. dbf))

Defaultdir [String, Database File ", Description, Driver [odbcjt32.dll],

DriverId [277 (IV), 533 (5.0)], Fil [DBASE III;], SafetransAction, UID,

Engines / XBase / ImplicitCommitsync,

Engines / XBase / collating [string, sorting basis, available for ASCII, International, Norwegian-Danish,

Swedish-finnish],

ENGINES / XBASE / DELETED [Binary, whether it does not display recorded by soft deletion, 0 means display, default is 1],

Engines / XBase / PageTimeout [default is 600], Engines / XBase / UserCommitsync,

ENGINES / XBASE / THREADS, ENGINES / XBASE / STATISTICS [Binary, Whether to use about the number of lines, 1 is Yes, default 0]

Note: (DBASEIV and DBASE5.0 two versions of DRIVERID have different)

4. FoxPro (Microsoft FoxPro Driver (*. Dbf))

Defaultdir [Directory in Database File], Description, Driver [odbcjt32.dll],

Driverid [536 (2.6), 280 (2.5)], FIL [FoxPro 2.0;], SafeTransaction, UID,

Engines / XBase / Collating [string, sorting basis, available for ASCII, International],

ENGINES / XBASE / DELETED [Binary, whether it does not display recorded by soft deletion, 0 means display, default is 1],

Engines / XBase / PageTimeout [default is 600], Engines / XBase / UserCommitsync,

ENGINES / XBASE / THREADS, ENGINES / XBASE / STATISTICS [Binary, Whether to use about the number of lines, 1 is Yes, default 0]

Note: (FoxPro2.5 and FoxPro2.6 two versions of Driverid have different)

Make the above program into a COM or ActiveX control, in many advanced programming languages ​​such as Delphi, C Buider, VB, VC, PB can be used.

转载请注明原文地址:https://www.9cbs.com/read-4202.html

New Post(0)