Mega Code Archive

 
Categories / Delphi / ADO Database
 

DATABASE independency

Title: DATABASE independency. Question: How to write an application that is almost database independent? Answer: Some of us write a database application without knowing what database is he/she is going to use for their final version. So we start with a simple database like Dbase, Paradox, or Access and when the application is done we discover that our application will be using another type or brand of database engine. Then we ask our selfs: What about the code that I wrote? This is my case in many applications, which led me to come up with two functions that will make my application more than half away of DATABASE independency. Here is the code for those two functions: ******************************************************************************* procedure RunSQL(sSQL : String); var qrySQL : TADOQuery; begin qrySQL := TADOQuery.Create(Application); qrySQL.Connection := adoConnection; qrySQL.SQL.Clear; qrySQL.SQL.Text := sSQL; qrySQL.ExecSQL; qrySQL.Free; end; ******************************************************************************* Procedure GetRecords(var qryField : TADOQuery; sSQL : String; LockType : TADOLockType = ltReadOnly); begin if qryField = Nil then begin qryField := TADOQuery.Create(Application); qryField.CursorType := ctOpenForwardOnly; qryField.Connection := adoConnection; end; qryField.Close; qryField.LockType := LockType; qryField.SQL.Clear; qryField.SQL.Add(sSQL); qryField.Open; end; ******************************************************************************* Example of use for RunSQL: ............................................................................... var sSQL : String; begin sSQL := 'insert into TB_Log (UserId,UserName) values ("My User Id","My User Name")'; RunSQL(sSQL); ............................................................................... Example of use for GetRecords: ............................................................................... var qrySetup : TADOQuery; begin GetRecords(qrySetup,'select * from TB_Setup',ltOptimistic); if qrySetup['IsRegistered'] then begin ShowMessage('Your are a Registered user') else ShowMessage('Your are NOT a Registered user'); //DO NOT forget to free it when done. qrySetup .Free; ............................................................................... Another example for the use for GetRecords: ............................................................................... var qryUsers : TADOQuery; begin GetRecords(qryUsers,'select * from TB_Users',ltOptimistic); while NOT qryUsers.Eof do begin if qryUsers['UserName'] = 'Abdulaziz' then ShowMessage('It's me!!!'); qryUsers.Next; end; qryUsers.Free; ............................................................................... Those two functions are written for MSAccess database engine through ADO components that comes with Delphi 5 and 6 but they can be changed independently to work with almost any database engine that can be accessed using Delphi. So keep using the two functions and your application will be more Database independent. Rememberthis is NOT a complete solution but an excellent step toward database independency.