ADO Database Delphi

Title: How to talk to a MSAccess database thru Dot Net
program DBTest;
uses
System.Windows.Forms,
Newform2 in 'c:\tempnet\Newform2.pas' {Form1};
begin
Mainform := TForm1.Create;
Application.Run(Mainform);
end.
//--------------------------------------------------------------------
// Unit refered to by the main program. Make sure your database
// path is set correctly
//--------------------------------------------------------------------
unit NewForm2;
interface
uses
{ Just like the old uses clause in a delphi program. The SYSTEM namespace refers to windows
type controls and not specifically the delphi ones. To draw a delphi button call on the
BORLAND.VCL name spaces... see below }
System.Reflection,
System.Drawing,
System.Drawing.Text,
System.ComponentModel,
System.Windows.Forms,
System.Data.OleDB,
System.Data,
System.Data.Common,
System.Runtime.InteropServices;
type
TForm1 = class(Form)
private
buttonload: system.windows.forms.button; // a button
Components: system.componentmodel.container; // a component store
datagrid1: system.windows.forms.datagrid; // not used in this implementation
public
constructor Create; // which I will inherite and amend
procedure InitializeComponents; // easy way to centralise component creation
procedure Button1_Click(Sender: TObject; E: EventArgs); // on click event
end;
var
MainForm: TForm1; // as ever a main delphi form
implementation
constructor TForm1.Create;
begin
inherited Create; // normal create stuff then set up all the required components
InitializeComponents; // sets up components
end;
procedure TForm1.InitializeComponents;
var
MyControls: array[0..2] of control; // container class for main form
begin
Self.ClientSize := system.Drawing.Size.Create(600,413); // client window on screen
Self.Components := System.ComponentModel.Container.Create();
// container class for the other bits
Self.buttonload := system.windows.forms.button.Create(); // make a button
Self.buttonload.add_click(button1_click); // set its on click event
Self.buttonload.Size := system.drawing.Size.Create(112,32); // size up the button
Self.buttonload.location := system.drawing.point.Create(480,352); // where on screen ?
Self.buttonload.Text := 'Read the database';
// text on the button - 'caption' in real delphi
Self.datagrid1 := system.windows.forms.datagrid.Create(); // draw a datagrid - not used
Self.datagrid1.Size := system.drawing.Size.Create(584,336);
Self.datagrid1.location := system.drawing.point.Create(8,8);
MyControls[0] := Self.buttonload; // add button to container class
MyControls[1] := Self.datagrid1; // add grid to container class
Self.Controls.AddRange(MyControls);
// basically add them to the form, form is now parent
end;
procedure TForm1.Button1_Click(Sender: TObject; E: EventArgs);
var
dbConnection: oleDBConnection;
dbCommand: OleDBCommand;
dbReader: OleDBDataReader;
dbDataAdapter: OleDBDataAdapter;
dbDataset: Dataset;
temp, temp1: string;
int1: Integer;
begin
temp := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb'; // connection string
temp1 := 'Select * from Shawbury'; // SQL code to fire
(* Fount that I had to use an explicit string to make the connection , The exapmple code
was'nt very clear on this - it works so hopefully it's the right solution *)
dbConnection := System.Data.OleDB.OleDbConnection.Create(temp); // make a DB Connection
dbConnection.Open(); // open a DB Conection
dbCommand := System.Data.OleDB.OleDbCommand.Create(temp1, dbConnection);
// execute the SQL
dbReader := dbCommand.ExecuteReader(); // and store in a datareader
int1 := dbReader.GetOrdinal('subcol1');
// I have a coloum in the Database called subcol1
while dbReader.read() do // keep reading all records
begin
// gives you a warm feeling to see the last record on the button
// - now I'm sure its read the file
buttonload.Text := dbreader.GetValue(int1).tostring;
end;
end;
end.