Dynamic connection database and dynamic establishment ODBC, DSN (ZT)

xiaoxiao2021-03-06  55

Configure the database in the ODBC data source, in this way, the application's writing is simple. Just need to specify a configuration that has been configured in the application to connect, access the database. However, this approach has certain limitations, and needs to be manually configured in the ODBC data source. Users who need church programs will be configured, and if they accidentally change the configuration of the ODBC data source (maybe unintentional). It will cause connection to the database failed. At this time, the program of the program may be Mo Mo, but he is no. Place the TDATABASE control on the Form or Data Modules, the property is set as follows: Object DBWORK: TDATABASE DATABASENAME = 'Work' loginprompt = false sessionname = 'default' transisolation = TidiRTYREAD = 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-82681.html

New Post(0)