Dynamic connection database and dynamically establish ODBC'DSN Delphi implementation

zhaozj2021-02-12  182

Place the TDATABASE control on Form or Data Modules, the property is set as follows:

Object DBWORK: TDATABASE

DatabaseName = 'Work'

LoginPrompt = false

Sessionname = 'default'

TRANSISOLATION = TidirtyRead

LEFT = 32

TOP = 16

End

1) PARADOX database connection

Dbwork.connected: = false dbwork.drivername: = 'standard'; dbwork.params.values ​​['Path']: = 'd: / data'; // Fill in the database path DBWORK.CONNECTED: = True;

2) Connection to Informix Database (BDE connection)

Dbwork.connected: = false dbwork.drivername: = 'informix'; dbwork.params.values ​​['server name']: = SSERVER; // Database server name dbwork.params.values ​​['user name "]: = suse; // User Name DBWORK.Params.Values ​​['Database Name']: = 'sysmaster; // Database Name DBWork.Params.Values ​​[' password ']: = spass; // User Password Dbwork.connected: = true;

3) Connection to the SQL Server database (like Informix)

Dbwork.connected: = false dbwork.drivername: = 'mssql'; dbwork.params.values ​​['server name']: = sserver; // Database server name DBWork.Params.Values ​​['user name']: = SUSE; // User Name DBWork.Params.Values ​​['Database Name']: = 'master; // Database Name DBWork.Params.Values ​​[' password ']: = spass; // User Password Dbwork.connected: = true;

4) POSTGRD SQL database connection (ODBC connection)

Dbwork.connected: = false dbwork.drivername: = 'postgreSQL'; dbwork.params.values ​​['odbc dsn']: = 'pgsql'; / / data source name DBWork.Params.Values ​​['Server Name']: = Sserver; // Database server name DBWork.Params.values ​​['user name']: = suse; // User Name DBWORK.PARAMS.VALUES ['Database Name']: = 'master; // Database Name DBWork.Params. VALUES ['password']: = spass; // User Password DBWORK.CONNECTED: = true; PS: ODBC connection, ensure that the ODBC driver is correctly installed; and there is an ODBC;

A automatic setting ODBC code:

// SDRINAME driver name (PostgreSQL)

// SDSNNAME data source name

Function CreateDSN (SDRINAME, SDSNNAME: STRING): Boolean

VAR

REGTMP: Tregistry;

Stmp: String;

Begin

RESULT: = TRUE;

Regtmp: = tregistry.create;

Try

With regtmp do

Begin

RootKey: = HKEY_LOCAL_MACHINE

IF OpenKey ('Software / ODBC / ODBCINST.INI /' SDRINAME, FALSE) THEN

Begin

Stmp: = ReadString ('Driver');

If stmp = '' THEN

Begin

Result: = FALSE;

EXIT;

END;

end

Else

Begin

Result: = FALSE;

EXIT;

END;

Closekey;

Rootkey: = HKEY_CURRENT_USER;

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

Begin

IF not valueexists (sdsnname) THEN

WriteString (SDSnname, SDRINAME);

end

Else

Begin

Result: = FALSE;

EXIT;

END;

Closekey;

IF OpenKey ('Software / ODBC / ODBC.INI /' SDSNNAME, TRUE) THEN

Begin

IF not valueexists ('ServerName') THEN

Writestring ('ServerName', '');

IF not valueexists ('database') THEN

WriteString ('Database', '');

IF not valueexists ('username ")

WriteString ('username', '');

IF not valueexists ('password') THEN

WriteString ('password', '');

If not valueexists ('description') thenwritestString ('Description', 'Dowhat create ";

IF not valueexists ('driver') THEN

WritestRing ('Driver', STMP);

end

Else

Begin

Result: = FALSE;

EXIT;

END;

END;

Finally

Regtmp.closekey;

END;

END;

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

New Post(0)