Automatically set an ODBC data source in the Delphi program

xiaoxiao2021-03-05  19

In the Delphi database application, we usually have two ways. One is the advantage of using the BDE database search engine, which utilizes Delphi's own database driver, which is fast, but the application range is limited. When the database version is updated, it is possible to operate new database; One way is to pass ODBC. The advantage of this approach is that it can be provided with an operating system (such as Windows). As a standard interface can adapt to a variety of databases, the disadvantage is slow. When programming, we can choose one of the methods as needed.

When using the ODBC access to the database, the usual method is a ODBC system data source (system DSN) in the ODBC Management panel, and then set a database alias in the DBD or in the program, which should be DSN. Wish to pay the database. I believe that programmers who have done database applications with Delphi are very familiar with this, and the author will not say much. In practical applications, the author has encountered such a situation, our database application is based on ODBC system data source access and operational 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 study how to dynamically set the contents of the ODBC system DSN in the program, which can increase the robustness of their own programs. After a whole day, the research on the Windows registry finally found the secret of the ODBC management program set DSN ("heaven and earth self-fair, paying will always return!", Not advertising!), Now write it with everyone, please master Education. The ODBC Manager Sets the secret in the registry, you can go to HKEY_LOCAL_MACHINE / SOFTWARE / ODBC to see it, and it will definitely feel halfway. First take a look at the ODBC database driver installed in the system. In HKEY_LOCAL_MACHINS / SOFTWARE / ODBCAL_MACHINST.INI, information that has been installed the ODBC database driver is stored, which can find information such as DLL files corresponding to the installed ODBC database driver. In each key value 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)), there is a driver's details, we mainly get the path and file name of the DLL file corresponding to the ODBC driver. 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 DSN's registration information is stored, and the 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 completing the parameter settings in the ODBC Management panel, how to register DSN information in the registry. Take the MS Access 97 type of MS Access 97 type name MyAccess as an example, our designated parameters mainly have database types (Microsoft Access Driver (*. MDB)), data source name (MyAccess), data source description (my Access) , Database path (C: / INETPUB / WWWWROOT / TEST.MDB), other parameters such as user name, user password, exclusive, read-only, system database, default directory, buffer size, scan line, page length, etc. Provincial parameters. At this time, the registration system DSN should generally have the following steps: 1. Add a string key value to myAccess = Microsoft Access Driver (*.), Which is data source name and database type for MyAccess = Microsoft Access Driver (*. MDB). This is to register a system DSN name in the registry.

2. Create a subkey in hkey_local_machine / soft / odbc / odbc.ini, create a key to HKEY_LOCAL_MACHINE / SOFTWARE / ODBCAL_MACHINE / SOFTWARE / ODBC / ODBC.INI / MyAccess, then create some key values ​​under then, describe a system DSN configuration information, the main information is ([] content is the author note): dbq = c: /inetpub/wwrowRoot/test.mdb [string, representation database path] Description = My Access [string, representative database Description] Driver = c: /pwin98/system/odbcjt32.dll [string, indicating the driver, can be seen ODBCINST.INI] driverid = 0x00000019 (25) [number, indicating the driver ID, can not change] FIL = MS Access; String, possibly related to filtering filter] SafetraSction = 0x00000000 [number, may indicate a number of support for transactional operations] UID = "" [string, indicating user name, here is empty string]

3. Create a subkey in HKEY_LOCAL_MACHINE / SOFTWARE / ODBC / ODBC.INI / MyAccess, then create a subkey JET, that is, create a key for hkey_local_machine / software / odbc / odbc.ini / myaccess / Engines / Jet, then create some key values ​​under detailing a system DSN database engine configuration information, the main information is ([] content is the author note): IMPLICITCOMMITSYNC = YES [string, may indicate if it is immediately Reflection Data Modification] MaxBuffersize = 0x00000200 (512) [Digital, Indicates Buffer Size] PageTimeout = 0x00000005 (5) [Digital, Indicated Page Timeout] Threads = 0x00000003 (3) [Digital, Maximum Thread Number of Support] 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), data source name: myaccess data source description: My new data source database type: Access97 Correspondence database: c: / inetpub / wwwroot / Test.mdb ******************************************************* *******} {Note should include registry} procedure tform1.button1click (sender: TOBJECT); Var RegisterTemp: Tregistry; Bdata: Array [0..0] of byte; begin registerTemp: = Tregistry.create; // Establish a registry instance with registerTemp do begin rootkey: = HKEY_LOCAL_MACHINE; / / Set Root Root 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) '); ELSEBEGIN / / Create key value failed MEMO1.LINES.ADD (' Increase 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) Thenbegin WritestRing (' DBQ ',' C: / INETPUB / Wwwroot / test.mdb '); // Database Directory WritestRing (' Description ',' My New Data Source '); // Data Source Description WriteString (' Driver ',' C: /PWIN98/System/odbcjt32.dll ' ); // driver DLL file WriteInteger ('driverid', 25); // Driver ID WRITESTRING ('Fil', 'MS Access;'); // Filter Based on Writeinteger ('SafeTransaction', 0); // Supported transaction operation WritestRing ('uid', ''); // User Name BData [0]: = 0; WriteBinaryData ('Exclusive', BData, 1); // Non-exclusive way WriteBinaryData ('Readonly', BData , 1); // Non-reader way Endelse // Create 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) THENBEGIN WRITESTRING ('ImplicitCommitsync', 'YES'); Writeinteger ('maxbuffersize', 512); // buffer size WriteInteger ('pagetimeout', 10); // page timeout WriteInteger ('Threads', 3); // support Thread number WriteString ('UserCommitsync', 'Yes'); endelse // Create key value failed becom memo1.Lines.Add ('increasing ODBC data source failed "; exit; end; closekey; memo1.lines.add (' increase New ODBC data source success');

.

The above program is debugged under PWIN98 Delphi3.0.

Below is the information that is required to create a DSN that creates a common database type ([] is an 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 [default YES], Engines / Jet / MaxBuffersize [Default 512], Engines / Jet / PageTimeout [default is 512], Engines / Jet / Threads [defaults to 3], Engines / Jet / UserCommitsync [default YES] Optional settings: systemdb [string, system Path of the database], readonly [binary, is it opened in a read-only mode, 1 is Yes, default is 0], Exclusive [binary, whether it is opened in exclusive way, 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, Safetractions, Uid, Engines / Excel / ImplicitCommitysync, Engines / Excel / MaxScanRows [Number, Scanning Row, Deads 8], Engines / Excel / Threads, Engines / Excel / UserCommitsync , Engines / Excel / FirstrowhasName [binary, whether the first line is domain name, 1 indicating that it 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, which is a path corresponding to DBQ in Excel97 and Excel7.0 / 5.0. The Excel 4.0 and Excel3.0 will be the same as DBQ; different DriverIds are different. 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, can be ASCII, International, Norwegian-Danish, Swedish-finnish], Engines / XBase / DELETED [Binary, whether it does not display recorded recorded by soft deletion, 0 indicates displaying, default 1], Engines / XBase / PageTimeout [defaults to 600], Engines / XBase / UserCommitsync, Engines / XBase / Threads, Engines / XBase / Statistics [Binary, Whether to use approximate 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, Sort by, can be ASCII, International], Engines / XBase / DELETED [Binary, whether it does not display recorded by soft deletion, 0 indicates display, default is 1], Engines / XBase / PageTimeout [default is 600], Engines / XBase / UserCommitSync, Engines / XBase / Threads, Engines / XBase / Statistics [Binary, Whether to use approximately rows, 1 is Yes, default 0] Note: (FoxPro2.5 and FoxPro2.6 two versions of Driverid)

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-38538.html

New Post(0)