Mega Code Archive

 
Categories / Delphi / Files
 

Creating The ADO file connection string in Run time

Title: Creating The ADO file connection string in Run-time Question: How to create The .udf Ado File with connection string? Answer: Some developpers like to create the String of connection ADO, in an archive and not in the TADOConnection component, this gives to more flexibility the application without needing always change its code source that it will have changes in the place of the same Database or in login and in the password of it. To create an archive UDF, we will need, first, to declare in the clause You use of unit, following unit: ADODB After that it places the following code: function GreateUdf(const aFileName: String; const aLogin: String; const aPassword: String; const aCatalog: String; const aDataSource: String): Boolean; var sProviderName: string; sDataSourceName: string; begin sProviderName := 'SQLOLEDB.1;Password="'+ aPassword + '";User ID=Admin'; sDataSourceName := format('Persist Security Info=True;User ID=' + aLogin + 'sa;Initial Catalog=' + aCatalog + ';Data Source=%s\'+ aDataSource + ';Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=%s;Use Encryption for Data=False;Tag with column collation when possible=False',[sNameComputer,sNameComputer]); try CreateUDLFile(aFileName, sProviderName, sDataSourceName); result := True; except MessageDlg(Error when generating archive of connection with the data base: ' + Exception(exceptobject).message, mtError, [mbOk], 0); result := False; end; end; look that the nucleus of this function is the "CreateUDLFile" function, that's declared in unit ADODB. It belongs the API of the ADO To use this function: // sUDLFilePath := ExtractFilePath(ParamStr(0)) + 'Sisglobal.udl'; sUDLFileConnectionString := 'FILE NAME=' + sUDLFilePath; if not FileExists(sUDLFilePath) then if not GeraUdF(sUDLFilePath, 'sa', 'pw', 'DATABASENAME', 'INSTANCENAME') then begin MessageBox(0,PChar(pFatalMessage + 'Cannot create Connection Database file' ),'Fatal error',mb_Ok + mb_IconStop+ mb_DefButton1 + mb_applmodal); Application.Terminate; end;