Automatically set an ODBC data source in the Delphi program

zhaozj2021-02-08  222

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 Matter-pass users accidentally modify or delete the system DSN we pre-set ... so, the author starts research how to dynamically set the contents of the ODBC system DSN in the program, so that the robustness of our program can be added . 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/wwroot/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, may be related to filtering filter] Safetractions = 0x00000000 [number, may indicate a number of support for transactional operations] UID = "" [string, indicating the 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 data modification is immediately reflected to the user] The above is the basic information of the establishment of a system DSN (other information such as options or advanced options, etc.) is also set here, but because of the default information, registry Not listed), we follow these steps in the program to operate the registry,

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 contain registry} procedure tform1.button1click (sender: Tobject) in the Uses statement; varregisterTemp: Tregistry; bdata: array [0..0] of byte; beginRegisterTemp: = TregiSTRY. Create; // create a Registry instance with registerTemp dobeginRootKey: = HKEY_LOCAL_MACHINE; // HKEY_LOCAL_MACHINE // set the root key to find Software / ODBC / ODBC.INI / ODBC Data Sourcesif OpenKey ( 'Software / ODBC / ODBC.INI / ODBC Data Sources', true) Thenbegin / / Register a DSN name WriteString ('myaccess ",' Microsoft Access Driver (* .mdb) '); endelsebegin // 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) ThenbeginWritString ('DBQ' , 'C: /inetpub/wwroot/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 ('SafetractionsAction', 0); // Support The number of business operations 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 beginMemo1.Lines.Add ('Add 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) thenbeginWriteString (' ImplicitCommitSync ',' Yes'); WriteInteger ( 'MaxBufferSize', 512); // buffer size WriteInteger ( 'PageTimeout', 10) ; // Timeout Writeinteger ('Threads', 3); // Supported Thread Number WriteString (' UserCommitsync ',' Yes'); endelse // Create key value failed beginMemo1.Lines.Add ('Increase ODBC data source failed '); exit; end; closekey; memo1.lines.add (' Add new ODBC data source success'); free; end; end; 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;], saFetractions [default is 0], UID [default empty], Engines / Jet / ImplicitCommitSync [defaults to YES], Engines / Jet / MaxBuffersize [default 512], Engines / Jet / PageTimeout [default is 512], Engines / Jet / Threads [defaults to 3], Engines / Jet / UserCommitsySync [default YES ] Optional setting: systemdb [string, system database path], readonly [binary, whether to open in read-only mode, 1 is Yes, default is 0], Exclusive [binary, whether to open 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)], DriverId [790 (Excel97), 22 (5.0 / 7.0 ), 278 (4.0), 534 (3.0)], FIL [Excel 5.0;], Readonly, Safetractions, UID, Engines / Excel / ImplicitCommitsync, Engines / Excel / MaxScanRows [Digital, Scanning Row, Default 8], ENGINES / Excel / Threads, Engines / Excel / UserCommitsyname [binary, the first line is domain name, 1 indicates that the default is 1] Note: Excel97 and Excel7.0 / 5.0 DBQ corresponds to a XLS file And Excel4.0 and Excel3.0 correspond to a directory; DefaultDir corresponds to a directory,

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

New Post(0)