Delphi Access Database

zhaozj2021-02-16  62

Implement the operation of the SQLServer database. The specific steps are as follows: The first step is to register the ODBC data source. This is a crucial step, otherwise it is impossible to implement the database access. For example, we define a data source zhb that enables the zhb database on the SQL-Server server. First, select the database of SQL Server type to enter the "ODBCSQLServerseTup" window. Then, define the data source name "zhb"; define the server as "SQL-Server"; and the network path is "ιιSQL-Server"; finally, press the Option command button to select the database to zHB. Step 2, configure BDE. It is a Delphi dedicated database engine. You can start from the Delphi program group, or start from the Tools menu of the Delphi program item. Delphi is slightly different from VB when accessing the SQLServer database. VB is directly calling ODBC to connect SQLServer, and Delphi is first calling an alias in BDE, and the alias is directly implemented by ODBCDriver directly through the ODBCDriver in BDE. And in the ODBCDRIVER in BDE, you can also define other ODBC properties, such as language drivers or open modes, and more. This undoubtedly greatly increases the function of ODBC. First, in the DRIVERS tag page, press "NewoDBCDriver" to add a new driver. There are three columns to be filled in in the addition window. The first column SQLLINKDRIVER can be taken, such as: odbc-zhb; selection SQL Server in the second column default (in the third column DEFAULTDATASOAME, selecting the data source ZHB just registered. A DelphiodBC driver called ODBC-ZHB was added after the OK was confirmed. In the parameter table on the right side, you can also set the open mode OpenMode, query mode SQLQRYMODE, SQL statement execution mode SQL -PASSTHRUMODE, and language driver LangDriver et al. Next, in the AliaSes tag page, press the "Newalias" command button to add a new alias. There are two columns that need to increase the new alias window. The first column is NewaliasName, you can take a name, such as: zhbdb; this alias is the database name we want to call directly in the Table control. The second column AliaSType select the odbc-zhb that just defined in the Drivers tag page, and finally press OK to confirm, add an alias called ZHBDB. In this way, the Table control can be connected to the zhb database connected to the SQL-Server server through the ODBC Data Source ZHB through the odbcdrivers (ie: odbc-zhb), ODBC-ZHB, and ODBC-ZHB, and ODBC-ZHB. The above is equivalent to completing registration in BDE, and then the property of the control can be set. In the third step, set the DataAccess control property to connect with the database. Add Table and DataSource controls to the form and modify the properties of the Table control. First, change the DatabaseName property to the zhbdb defined in the BDE alias. After setting the DatabaseName property, you can select a table name from the drop-down list of the TABLENAME attribute. This Table control is to complete the connection with the database, and an optional table name will appear in this list after the connection is successful. Then, change the Active attribute to TRUE, activate the open database, and the DataSource control is connected. Change the ExClusive property to True.

Finally, set the DataSet property of the DataSource control to Table1. The DataSource control is the channel that Table control is associated with the DataControls control. Through the settings of the above steps, the DataSource1 object has established a contact with a table of the zhb database. In the future, you can implement the operation of the table as long as DataSource1 object is accessible. Step 4, set the DataControls control properties to implement the operation of the database. Once the control of the DataAccess class is successfully opened, the control of the DataControls tag page in the window can be used. These controls have DBGRID, DBNAVIGATOR, DBTEXT, DBEDIT, DBMEMO, DBIMAGE, DBLISTBOX, DBCOMBOX, DBCHECKBOX, and DBRADIOGROUP. These controls can make read and write operations for tables as long as their DataSource and DataFields properties are set. It can be seen that Delphi is more fast than VB in designing database software, especially SQL Server databases; and its rich control provides favorable help to interface design; in addition, Delphi's true compilation makes the program's execution efficiency is quite high. Good portability. These are owed by VB. /// database conversion method

The control used

2. Use of TbatchMove control

TBATCHMOVE allows special jobs to be performed in a set of records or entire data sheets, the main purpose of this control is to remove data from the server to the local data source for analysis or other operations. TBATCHMOVE can establish a data sheet corresponding to the original data table content in the destination, automatically automatically perform appropriate correspondence operations.

Two features of TbatchMove indicate the source tables and purposes of Batch Move operations. Source specifies a dataset corresponding to an existing source data table (a TQuery or TTABLE control), Destination indicates the TTable control corresponding to a database data table, and the destination data table can be existing.

Mode features indicate what TBATCHMOVE objects to do:

We can use the BATCopy mode to properly convert the database type, which is the conversion result from the DBASE type to other types:

Database conversion example

The accounting account database (kjkmk.dbf) in financial procedures developed by FoxBase is developed, and the system will be upgraded to the Window S environment. The database will use the Paradox language. In order to use the library structure and data, the following method can be used to switch.

1.DataSource1 control:

AutoEdit True

DataSet Table1

Enabled True

Name Datasorce1

2.Table1 control:

DatabaseName CW

Name Table1

Readonly False

TableName Kjkmk

TableType UDBase

DataSource1 control and Table1 control co-define the converted source file, which is the KJK MK aligned with CW, the file type is DBASE.

3.BatchMove1 control:

Destination Table2

Mode Balcopy

Name BatchMove1

Sorce Table1

The MatchMovel control defines the conversion type to COPY and the source file, the target file.

4.DataSource2 control:

AutoEdit True

DataSet Table2

Enabled True

Name Datasorce2

.Table2 control:

DatabaseName CWDB

Name Table2

Readonly False

TableName Kjkmk

TableType Uparadox

DataSource2 controls and Table2 controls jointly define the converted target file, which is the KJKMK aliasing CWDB, the file type is Paradox.

6.DBGRID1 Control: Datasorce Datasorce1

Name DBGRIDL

DBGRIDL Grid Control First defines the source file, and then define the target file after the conversion is executed, mainly for easy viewing.

7.Button1 control:

Onclck ButtonClck

OnDragdrop

OnDragover

OneundDrag

This control has an OnClick event indicating that the execution file type conversion. The code is as follows:

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

Begin

BatchMove1.execute;

END;

After the above method is defined, the KJKMK.DBF file can be converted to kjkmk.db files after compiling, and then the Database Desktop tool can be modified to modify the adjustment.

The conversion of any other file can also be performed in this way.

Delphi's database application is connected to the BDE via the Data Access control, thereby accessing the data source. The Data Control S control is some so-called Data_AWare control, providing user interface. Since BDE (BORLAND DATABASE ENGI-Neer supports ODBC standards, you can access a variety of data source tables.

Some problems when using SQL

Delphi's data control basically supports standard SQL languages, but some restrictions. Some numbers can be encountered when the specific programming is programmed.

The difference between the Open and ExecSql modes of the Query control. Open mode can open the contact data table, and the ECSQL mode is just running the SQL statement and does not send the running table to the connected Data-Controls control. Therefore, when the required data record is displayed with DBGRID, first use the SELECT statement to filter the meta-compliant tuple, and then operate with the QUERY control with o Pen. For example, there is a following application:

If you want to display a table in DBGRIDL (Table Name Workers.db), you can set the Query1's SQL property to: SEL ECT *WROM WORKERS, add statements in the program: query1.open; To insert a record, Query1's SQL attribute should be:

INSERT INTO WORKERS (No #, Name, SEX, AGE, Depart) VALUES

(: No # ,: name,: SEX, AGE,: DEPART)

In the program, you should add the following statement:

Query1.close;

Query1.Parambyname ('NO #'). Asintegar: = 2717;

Query1.Parambyname ('name'). Asstring: = 'Lee 4';

Query1.Parambyname ('sex'). Asstring = 'male';

Query1.Parambyname ('agn'). Asinteger: = 40;

Query1.Parambyname ('depart'). Asstring: = 'Quality Management';

Query1.execsql;

Note that if you don't have to use EXECSQL, it will be wrong.

2. To use the variables in the program in the SQL statement, you can only use the number of collections in SQL to replace the name of the name, and then assign the value of the variable to the field in the process. The above example is done. For another example, if the user enters a single number to save in the NU MBER variable, the SQL statement should be as follows:

Select * from workers where no # =: no #

Then add the following statement in the program:

Query1.close;

Query1.Parambyname ('NO #'). Asinteger: = Number;

Query1.open;

Precautions for using Chinese

1. To use Chinese in Delphi, you must use Chinese, you must configure the BDE Configuration, mainly for the selection of drive languages. For Delphi 1.0, the LangDriver can be set to DBASE CHS CP936 in the SYST EM page of BDE Configuration Utility, which can set the LANG-DRI VER in the System page to Sybase SQL DIC850. After setting, the data access controls and data control controls in Delphi can be used in Chinese. It is worth noting that if the table is generated in the BDE settings that do not support Chinese, even if the BD E is set to support Chinese, it is available in Chinese. At this point, you can use Database Desktop to modify the properties of the table. Change Table Langue in Tab Le Property to support Chinese drive language. 2. Chinese can be used as a field name and can be displayed correctly, but because the Pascal language is required to be named in English letters, the Chinese character string cannot be used as a variable name. So if there is a Chinese field name in the form, it is very careful when using SQL, especially when assigning or comparing the fields of the Chinese name. For example, the following SQL statements will be wrong when using:

Select * from test where name =: Name

INSERT INTO TEST (Name, AGE, SALARY) VALUES (: Last Name,: Age,: SALARY)

Note between the versions

Delphi 1.0 and Delphi 2.0 have some differences in the processing of the database, and pay attention during use. For example, the number of table fields of D ELPHI 1.0 cannot be too much, and the INSERT statement will not be inserted into records when more than 10 fields, while DELP HI 2.0 does not exist. As another example, when inserting the PARADOX table with the keyword, Delphi 1.0 will overwrite the same record of the original keyword, while Delphi 2.0 does not allow the insertion of the keyword already existing record, if this operation is performed, the database Use will become abnormal. ///

Implementing plain text data into the database with Delphi

Cai Xu often encounters such a problem in our daily work: a lot of pure text formats, how to share them, transfer to your own database?

For example, there is a person's archive material archive. TXT, the content is as follows:

Small male 21 engineer

Xiao Wu 23 Assistant Engineer

Xiao Cai Jun 22 Assistant Engineer

Xiaoyu 22 engineer

To transfer it into the database archive. DBF, Archive. The DBF is as follows:

Name, gender, age, title

How to do it? Now use Delphi programming, this problem is well solved. Delphi provides a lot of powerful, rich characters handlers and processes, commonly available:

(1) FunctionLength (S: String): Integer

// Return the length of the string

(2) FunctionCopy (S: String; Index, Count: in Teger): String

/ / Give a copy of a string in a string

(3) FunctionPOS (SUBSTR: STRING; S: String); Integer

/ / Find the location of the substrings in the string

(4) Procedurelete (VARS: String; Index, Coun NT: INTE-GER); // Remove the subtrings from a string

Using the existing functions provided by Delphi, the three functions are prepared on the basis of the process, and the plain text format is transferred to the database function. As long as Delphi supported databases can be supported.

Archive. Each line of data in txt is a string. Each split data in the string is a field, and the characters of each field are split, here is space, or,,,,,,,, The specific idea is: first adjust the string, then compare each character in the string, the characters of the split will be added to the MyStr string, and finally get the content of a field. With a loop, you can divide a character string into several fields.

FunctionRegulate (Astring, Sepchaar: string): String

/ / Remove extra split, specification string

Functiongetsubstr (Varstring: String; Sepchar: String): String;

// get a substring in the string

// Because the value of the parameter astring is to be changed, it uses a VAR definition.

Functiongetsubstrnum (Astring, Sepcha: String): Integer;

// Calculate a string to be split into several fields.

Parameters: astring is a string required by the segmentation,

Sepchar is a split.

FunctionRegulate; STRING: STRING

VAR

I, Num: Integer;

Flag: boolean;

Mystr, Tempstr: String;

Begin

Flag: false; // Mark, remove excess split

Num: length (astring); // calculate the length of the Astring string

Fori: 1tonumdo

Begin

Tempstr: copy (astring, i, 1); // Take a character in the Astrin G

iftempstrιλsepcharthen

Begin

Κmystr tempdtr;

Flag: κtrue;

end

Else

IF (Flagκ True) THEN

Begin

Κmystr tempsrt;

Flag: κfalse;

END;

END;

IFMYSTR [Length (mystr)] ιλsepcharthenmystr: κmystr sepchar;

RegulateSrt: κmystr;

END;

Functiongetsubstr (Varastring: Strign): String;

VAR

Mysrt: string;

Strlen: Integer;

Sepcharpso: integer;

Begin

Strlen: length;

Sepcharpos: POS (SEPCHAR, Astring); / / Calculation of the split in the substring

MyStr: κcopy (astring, 1, sepcharpos-1); / / put all the characters before the splitter into the MySTR string

Delete (astring, 1, sepcharpos); // Remove the substrings before the splitter and the division

GetSubstr: κMystr; // Return a field

END;

FunctionTFORML. Getsubstrnum (Astring: String; Sepchar: String): Integer;

VAR

i: integer;

Strlen: Integer;

Num: integer;

Begin

Strlen: length;

Num: 0;

Fori: κ1, toostrlendo

IFCopy (Astring, I, 1) κsepcharthen

Num: NUM 1;

Getsubsrtnum: kappaun;

End:

With the above three functions, now introduce specific applications:

1. First create a form FORML, join a Richeditl (or Menol), a button Buttonl and a TableL, set the Tablel's properties:

Tablell. Databaseκ'c: archivs'

Tablell. Tablenameκ'rchive. DBF '

2. Add the following procedures separately:

Constspaceκ ''

Proceduretforml. Formcreate (Sender: TOBJECT);

Begin

Richrditl. Lines. LoadFromfile ('Archive.txt');

Proceduretforml. Button1Click (Sender: TO-BJE CT);

VAR

I, J: Integer;

MYLINE: STRING;

Begin

Withtableldo

Begin

Open;

κ0toricheditl. Lines. Count-1DO

Begin

MYLINE: κRegulateStr (Richeditl.Lines [i], space);

Forj: 1tonumtoken (MYLINE, SPACE) DO

Begin

Append;

FileDS [J-1]. getsubsrt (myline, space);

POST;

END;

END;

END;

END;

Then you can compile, run. This procedure is debugged under Win95 / Del PHI2.0.

///

Just asked if INSTALLSHIELD for Delphi made his BDE installation disk, and a piece of my program.

Sender: FUSE (fuse), letter area: Visual Title: Re: Can Delphi leave BDE? Sending station: BBS Shuimu Tsinghua Station (THU NOV 6 18:02:21 1997) Copy the files in the BDE directory of Delphi 3 to your own directory. Some of the c: / my / DRV may not be used, generally, the following is required: IDAPI32.cfg (in advance) IDAPI32.DLL BLW32.DLL (language driver) iDASCI32.DLL IDR20009.DLL (language driver Program): USA.BLL Charset.bll Fareast.bll (not you can use in Chinese) japan.bll Other.bll Europe.bll Ceeurope.bll (The following is the database driver, select or b) according to the type used or b) IDDBAS32. DLL (DBASE) IDPDX32.DLL (Paradox)

IDBAT32.DLL IDDAO32.DLL (Access) IDDR32.DLL IDODBC32.DLL (using ODBC) IDSQL32.DLL (use SQL) IDPROV32.DLL SQLINT32.DLL (using SQL Interbase) SQLORA32 .DLL SQLSYB32.DLL SQLSSC32.DLL SQLMSS32.DLL SQLDB232.DLL SQLINF32.DLL BLAPIPATH provided under the following set RegEdit / HKEY_LOCAL_MACHINE / software / borland / BLW32: "c: / my / drv" LOCALE_LIB1: "c: / my / drv / USA.BLL "local_lib2:" c: / dimv/europe.bll "locale_lib3:" c :/my/drv/other.bll "locale_lib4:" c: /my/drv/charset.bll "local_lib5:" c : /my/drv/ceeurope.bll "local_drv/fareast.bll" locale_lib7: "c: /my/drv/japan.bll" / hkey_local_machine / software / borland / database engine Set configfile01 : "C: /my/drv/idapi32.cfg" DLLPATH: "C: / MY / DRV" Delphi Export Editor: China ASP

Users typically want to directly get data in the database in some word handles. Delphi features so powerful, of course, will not miss these applications. The following describes three export methods: 1. Data export to Excel and Word documents via DELPHI, which can easily access other applications that provide OLE interfaces, such as Excel, Word, etc. The perfect data processing feature provided in Excel makes data export to Excel to become an urgent need. The main work steps are: 1. Create a connection and workbook. 2. Read the database data and write to the Excel document. 3. Save the result and exit Excel. The main process is the following example: procedure GenerateXLSFile (strFileName: string); varExcelApp, MyWorkBook: Variant; begintryExcelApp: = CreateOleObject ( 'Excel.Application'); MyWorkBook: = CreateOleObject ( 'Excel.Sheet'); exceptapplication.Messagebox ( 'Unable Open the XLS file, please confirm that Excel. ', ", MB_OK MB_ ICONSTOP); exit; end; myworkbook: = excelapp.workbooks.add; // Insert a read database here and write the Excel document Trymyworkbook.saveas (StrfileName) ); MyWorkbook.close; Excelapp.quit; Excelapp: = unassigned; // Release Variant Variable Except // When saving an existing document without overwriting, MYWORKBOOK.CLOT; // generates an exception Excelapp.quit; End; END, the key statement of the write Excel document is as follows: myworkbook.worksheets [1] .Cells [1, 1] .Value: = '1234'; related object reference can be in the help file in the office directory VBaxls.hlp Finding. Delphi also provides an example of a Word connection to developer reference, but not considering Chinese Word so "INSERT Query" button is invalid, and can only modify the form of the form to "Simplified Chinese (China)." Must The statement of judging the language is modified from if (lang = 'english (uk)'). "" "" = = 'English (uk)') THEN is modified to if Lang <> English (US) 'TEN / / Do not wait for the right side to write one The string can then do this in the Chinese version of the query result. Second, the data export is the TXT document using the QuickRep control in Delphi to output the print result to the text format. Example: myquickrep.exportfilter: = TQRASCIIEXPORTFILTER.CREATE ('c: /test.txt'); myquickrep.prepare; myquickrep.exportfilter.free Third, the data export is the same manner as the HTML document, and the print result can be output to an HTML format via the QuickRep control. Examples are as follows: myquickrep.exportfilter: = tqrasciiexportfilter.create ('c: /test.htm'); myquickrep.prepare; myquickrep.exportfilter.Free; //

I am afraid not? ADO also said to the data source. For example, OLE DB, ODBC, JET must be configured. If you use a small locally library, use Paradox, FoxPro to configure BDE. Especially using InstallShieldExpress for Delphi 5, the whole package is easy. If you are using SQL Server7, it is recommended to use ADO. However, if you use Oracle, BDE is definitely the best choice. I came with a few days ago, I did a small test. A portion of each record of the table is traversed in a 120,000 recorded Oracle table and put it in a TclientDataSet. It takes about 4 to 5 seconds using BDE. Using ADO, the ODBC driver provided by Oracle8 is similar to the OLE DB Object results, and it takes more than ten minutes, and the CPU is covered. Maybe I am more stupid, I don't find the optimization parameters of the ADO configuration. But such a huge gap makes me believe that BDE has made full optimization of the interface of Oracle. //

Dynamically set the ODBC data source in the Delphi program

Shenzhen

Wang Fajun

---- In the Delphi database application, we usually have two ways to access the database. One is through the number of BDEs

According to the library search engine, this method is the advantage of using Delphi, which is fast, but should

Well range, when the database version is updated, it is possible to operate new databases; another method is

ODBC, this method has the advantage of being able to be provided with operating systems (such as Windows), can accommodate a variety of

Database, the disadvantage is slower. When programming, we can choose one of the methods as needed.

---- When using an ODBC to access the database, the usual method is a number of ODBC systems in the ODBC management panel.

According to the source (system DSN), then set a database alias in the DBD or in the program, this should be DSN, so

You can operate the database as you wish. Programmers who have used Delphi have done database applications have been

Very familiar, the author will not say much. In practical applications, the author has encountered such a situation, our database

The application is relying on the ODBC system data source access and operational database, the application is running well until a

One day, a user who is more familiar but uncomfortable to the Windows system is inadvertently modified or deleted.

System DSN ...

---- Thus, the author starts studying how to dynamically set the contents of the ODBC system DSN in the program, so

Increase the robustness of your own program. After a whole day of research on Windows registry, I finally found ODBC management.

The program sets the secret of DSN ("" Heaven and earth self-fair, pay will always return! ", Not advertising!), Now written

Sharing with everyone, please expert advice.

---- ODBC Manager Set the secret in the registry, do not believe in HKEY_LOCAL_MACHINE / SOFTW

Are / ODBC to see, will definitely make you feel halfway.

---- First look at the ODBC database driver installed in the system. In HKEY_LOCAL_MACHINE / SOFTWA

In RE / ODBC / ODBCINST.INI, information that has been installed has been installed, from here

To find information such as DLL files corresponding to the installed ODBC database driver. ODBCINST.INI / ODBC DRI

In each key value of VERS, 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, ie the key name driver

Value, generally "c: /windows/system/filename.dll".

---- then see the system DSN's registration information, in hkey_local_machine / software / odbc / odbc.ini

, Store the registration information of the system DSN, and our DSN parameters set in the ODBC management panel are here.

---- Let's see the steps to create an ODBC system DSN, that is, we complete the parameter settings in the ODBC Administration panel.

After the ODBC manager is to register DSN information in the registry. Create a MS named myaccess

Access97 type system DSN is an example, our designated parameters mainly have database types (Microsoft Access D

River (*. MDB)), Data Source Name (MyAccess), Data Source Description (My Access), Database Path (C: / i

Netpub / wwwroot / test.mdb), other parameters such as user name, user password, exclusive, read-only, system data

Library, default directory, buffer size, scan line, page length, etc. use system default parameters. At this time, the registration system

DSN generally should have the following steps:

---- 1. In addition to HKEY_LOCAL_MACHINE / SOFTWARE / ODBC / ODBC.INI / ODBC DATA SOURCES

String key value, for myaccess = Microsoft Access Driver (*. MDB), which is data source name

Name and database type. This is to register a system DSN name in the registry.

---- 2. Create a subkey in HKEY_LOCAL_MACHINE / SOFTWARE / ODBC / ODBC.INI (Subkey) Myac

CESS, that is, create a key for hkey_local_machine / software / odbc / odbc.ini / myaccess, then

Create some key values ​​under then describe the configuration information of a system DSN, the main information is ([] in the pen

Note):

DBQ = C: /inetpub/wwwroot/test.mdb

[String, indicated database path]

Description = My Access

[String, indicated database description]

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

[String, indicating the driver, see ODBCINST.INI]

Driverid = 0x00000019 (25)

[Digital, indicating the driver ID, can not change]

Fil = MS Access;

[String, may be related to filtering filter]

SafeTransaction = 0x00000000

[Digital, may indicate the number of support for transactional operations]

UID = ""

[String, indicate the user name, here is empty string]

---- 3. Create a sub-key in HKEY_LOCAL_MACHINE / SOFTWARE / ODBC / ODBC.INI / MyAccess, and then create a subkey JET, create a key for HKEY_LOCAL_MACHIN

E / Software / ODBC / ODBC.INI / MyAccess / Engines / Jet, then create some key values ​​under detail

Describe the database engine configuration information of a system DSN, the main information is ([] content as the author note):

IMPlicitCommitsync = YES

[String, may indicate whether to reflect data modification immediately]

Maxbuffersize = 0x00000200 (512)

[Number, indicating the size of the buffer]

Pagetimeout = 0x00000005 (5)

[Number, indicating page timeout]

Threads = 0x00000003 (3)

[Digital, may indicate the maximum number of threads support]

UserCommitsync = YES

[String, may indicate whether to reflect the data to the user immediately]

---- Or above is the basic information of a system DSN (other information such as options or advanced options is also here.

In the setting, only because the default information is used, there is not listed in the registry), we will follow the steps in the program

Albums also add a system DSN or modify its configuration. In the example program below, it will be built above steps.

Establish a system DSN, 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;

// Establish 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 logo

WriteString ('fil ",' ms access;");

// Filter basis

Writeinteger ('SafeTransaction', 0);

// Supported transaction number

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

BDATA [0]: = 0;

WriteBinaryData ('Exclusive', BData, 1);

// Non-exclusive way

WriteBinaryData ('Readonly', BData, 1);

// Non-read only way

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.

---- The following is the information that is required to create 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 [defaults YES],

Engines / Jet / MaxBuffersize [Default 512],

Engines / Jet / PageTimeout [defaults to 512],

Engines / Jet / Threads [defaults to 3],

ENGINES / JET / UserCommitsync [default YES]

Optional setting: systemdb [string, path system of system database], readonly [binary, whether to open in 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, is the first line of the domain name,

1 said that it is default 1]

---- Note: The DBQ of Excel97 and Excel7.0 / 5.0 corresponds to an XLS file, and Excel4.0 and Excel3.0 are

Should be a directory; DefaultDir corresponds to a directory, the road corresponding to DBQ in Excel97 and Excel7.0 / 5.0

The trail is the same as DBQ in Excel 4.0 and Excel3.0; the various versions of DRIVERID are different.

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

DEFAULTDIR [String, Database File Office ",

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, is there no display

Documented by soft deletion, 0 means display, default is 1],

ENGINES / XBASE / PAGETIMEOUT [认 6 600],

Engines / XBase / UserCommitsync,

ENGINES / XBASE / Threads, Engines / XBase

/ Statistics [binary, whether to use about the number of rows, 1 is Yes, default 0]

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

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

DEFAULTDIR [Directory where database files],

Description, Driver [odbcjt32.dll],

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

Engines / XBase / Collating [String,

Sorting basis, can be ASCII, INTERNATIONAL],

ENGINES / XBASE / DELETED [Binary, is there no display

Documented by soft deletion, 0 means display, default is 1],

ENGINES / XBASE / PAGETIMEOUT [认 6 600],

Engines / XBase / UserCommitsync,

ENGINES / XBASE / Threads, Engines / XBase

/ Statistics [binary, whether to use about the number of rows, 1 is Yes, default 0]

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

---- Do a COM or ACTIVEX control, in many advanced programming languages ​​such as Delphi, C

Buider, VB, VC, and PB can be used.

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

New Post(0)