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/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, 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 sub-key (Subkey) Engines in HKEY_LOCAL_MACHINE / SOFTWARE / ODBC / ODBC.INI / MyAccess, then create a subkey JET, which is created 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 ([] in the author note):
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 Eight],
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, which is the path corresponding to DBQ, and the excel4.0 and Excel3.0 are the same as DBQ; the various versions of DriverId 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, 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.