Mega Code Archive

 
Categories / Delphi / ADO Database
 

Create a Delphi Ado application from Scratch

Title: Create a Delphi Ado application from Scratch Question: Sometimes we need to build an ADO application without using delphi ADO components Answer: Create a Delphi Ado application from Scratch. Sometimes we need to build an ADO application without using delphi ADO components. To do that you have to use the Delphi 5 Enterprise. For these cases we made a CookBook to do that: 1) Create a database on MSAcess with one table named clients; 2) Insert four fields in your client table; 3) In the Control Panel, choose the ODBC Data Sources and create a System DSN named Adotest; 4) open Delphi; 5) On a new project, insert a Tbutton and a Tmemo in your form; 6) Insert the ComObj in the uses clausule; 7) In the Onclick event insert this code: procedure TForm1.Button1Click(Sender: TObject); var RecordSet: OleVariant; temp: string; DSN: string; SelectString : string; i : integer; begin // ODBC dsn from ACESS database. DSN := 'dsn=AdoTest'; // the SQL string SelectString := 'SELECT * FROM clientes'; // Create an empty recordset object RecordSet := CreateOleObject('ADODB.Recordset'); // Fill the recordset RecordSet.Open(SELECTSTRING, DSN); // Display the data repeat // the client table has four fields // this loop will put one record per line in the memo for i:= 0 to 3 do temp := temp + ' ' + RecordSet.Fields[i].Value; Memo1.Lines.Add(temp); RecordSet.MoveNext; temp := ''; until RecordSet.EOF; end; These litle application will make a search in a database after you click in the button an than the application will put the results in the Memo. You can use this to build a simple COM component with data acess without using MIDAS, for example. Thats all folks. :o)