Dynamic connection database and dynamic establishment ODBC, DSN (ZT) Select Blog from Safef8

xiaoxiao2021-03-06  44

Dynamic Connect Database and Dynamic Establish ODBC, DSN (ZT) Configuring Database in 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.

Is placed on a form or control Tdatabase Data Modules, attributes are set as follows: object dbWork: TDatabase DatabaseName = 'work' LoginPrompt = False SessionName = 'Default' TransIsolation = tiDirtyRead Left = 32 Top = 16 End 1) connected dbWork.Connected Paradox database : = False dbwork.drivername: = 'standard'; dbwork.params.values ​​['Path']: = 'D: / data'; // Fill in the database path DBWORK.CONNECTED: = true; 2) Informix database connection of the INFORMIX database ( BDE connection) dbwork.connected: = false dbwork.drivername: = 'informix'; dbwork.pamas.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) The connection of the SQL Server database (like INFORMIX) dbwork.connected: = false dbwork.drivername: = 'mssql'; dbwork.params.values ​​['Server Name']: = SSERVER; // Database Server Name DBWORK .Pamas.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.pamas.values ​​[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: When the ODBC is connected, ensure that the ODBC driver is correct; and there is an ODBC; an automatic setting ODBC code: // SDRINAME driver name (postgreSQL) // SDSNNAME Data source Function CreateDSN (SDRINAME, SDSNAME: 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; ELSE BEGIN Result: = false; EXIT; End; CloseKey; Rootkey: = HKEY_CURRENT_UOTKEY: = 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') then writeString ( ' UserName ',' '); if not ValueExists (' Password ') then writeString (' Password ',' '); if not ValueExists (' Description ') then writeString (' Description ',' DoWhat Create '); if not ValueExists ('Driver') Then WritestRing ('Driver', STMP);

ELSE BEGIN RESULT: = false; EXIT; End; end; finally regtmp.closekey; end; end;

http://blog.9cbs.net/safef8/

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

New Post(0)