Board index » delphi » Writing Data Access component

Writing Data Access component


2003-08-15 06:54:52 AM
delphi196
I'm losing my bearings a little here. I want to create either a class or a
non-visual component that, given a value, can access a database to look up
information about that value. For example, given a zip code database, I want
a class or non-visual component that, once instantiated and given a zip
code, looks up and returns City/State. I can, of course, do this easily
using VCL components (a form, a data module, and data access components).
But how do I make this all non-visual? Something I can instantiate in any
application, then call its methods and get results. Seems I can not use a Data
Module and data access components because the data module needs a visual
owner (I think anyway).
I'm lost!
 
 

Re:Writing Data Access component

Let's suppose that you have an TAdoConnection with DataBase (you must have a
connection !!!!)
TmyInfo class =
fConnection:TAdoConnection;
fZipCode,
fCity,
fState:String;
private
procedure SetZipCode(Value:String);
published
property ZipCode read fZipCode write SetZipCode;
property City:String read fCity write fCity;
property State:String read fState write fState;
property Connection:TAdoConnection read fConnection write fConnection;
end;
procedure TMyInfo.SetZipCode(Value:String);
var
aQuery:TAdoQuery;
begin
fZipCode := Value;
If Assigned(fConnection) do
begin
aQuery := TADoQuery.Create(Nil);
try
aQuery.Connection := TAdoConnection;
aQuery.SqlText := 'Select * from MyTable where ZipCode =
'+QuotedStr(Value);
aQuery.Open;
If not aQuery.IsEmpty then
begin
fCity := aQuery.FieldByName('City').AsString;
fState := aQuery.FieldByName('State').AsString;
end
else
begin
fCity := EmptyStr;
fState := EmptyStr;
end;
finally
aQuery.Free;
end;
end;
end;
Whell i supposed this is what you want. If you use bde then you must change
TAdoConnection with TDataBase and etc. If you use adoconnection i thing you
do not even need aQuery and you can execute and use recordset as :
procedure TMyInfo.SetZipCode(Value:String);
var
RecordSet:_RecordSet;
begin
fZipCode := Value;
If Assigned(fConnection) do
begin
RecordSet := fConnection.Execute( 'Select * from MyTable where
ZipCode = '+QuotedStr(Value));
If RecordSet.RecordCount<>0 then
begin
fCity := RecordSet.Fields['City'].Value
fState := RecordSet.Fields['State'].Value
end
else
begin
fCity := EmptyStr;
fState := EmptyStr;
end;
end;
end;
I hope i helped
Dimitris
"Gary Mrenak" <XXXX@XXXXX.COM>writes
Quote
I'm losing my bearings a little here. I want to create either a class or a
non-visual component that, given a value, can access a database to look up
information about that value. For example, given a zip code database, I
want
a class or non-visual component that, once instantiated and given a zip
code, looks up and returns City/State. I can, of course, do this easily
using VCL components (a form, a data module, and data access components).
But how do I make this all non-visual? Something I can instantiate in any
application, then call its methods and get results. Seems I can not use a
Data
Module and data access components because the data module needs a visual
owner (I think anyway).

I'm lost!


 

Re:Writing Data Access component

Just saw your response. Thanks for your insights.
I was able to create a ZipCode component that accessed data without using a
data module, in particular using the IBX database, query, and transaction
components. The magic involved was where to use the "self" reference (to
name the owner of the IBX components) within the ZipCode component. I had
tried to instantiate a component within the initialization part of the
ZipCode unit, and kept getting "undefined variable: self" messages.
At any rate, sorry I didn't respond to your input sooner ... I guess I had
given up on getting one and stopped checking.
Thanks!
Gary
"Stefanides Dimitris" <XXXX@XXXXX.COM>writes
Quote
Let's suppose that you have an TAdoConnection with DataBase (you must have
a
connection !!!!)
TmyInfo class =
fConnection:TAdoConnection;
fZipCode,
fCity,
fState:String;
private
procedure SetZipCode(Value:String);
published
property ZipCode read fZipCode write SetZipCode;
property City:String read fCity write fCity;
property State:String read fState write fState;
property Connection:TAdoConnection read fConnection write fConnection;
end;


procedure TMyInfo.SetZipCode(Value:String);
var
aQuery:TAdoQuery;
begin
fZipCode := Value;
If Assigned(fConnection) do
begin
aQuery := TADoQuery.Create(Nil);
try
aQuery.Connection := TAdoConnection;
aQuery.SqlText := 'Select * from MyTable where ZipCode =
'+QuotedStr(Value);
aQuery.Open;
If not aQuery.IsEmpty then
begin
fCity := aQuery.FieldByName('City').AsString;
fState := aQuery.FieldByName('State').AsString;
end
else
begin
fCity := EmptyStr;
fState := EmptyStr;
end;
finally
aQuery.Free;
end;
end;
end;


Whell i supposed this is what you want. If you use bde then you must
change
TAdoConnection with TDataBase and etc. If you use adoconnection i thing
you
do not even need aQuery and you can execute and use recordset as :

procedure TMyInfo.SetZipCode(Value:String);
var
RecordSet:_RecordSet;
begin
fZipCode := Value;
If Assigned(fConnection) do
begin
RecordSet := fConnection.Execute( 'Select * from MyTable where
ZipCode = '+QuotedStr(Value));
If RecordSet.RecordCount<>0 then
begin
fCity := RecordSet.Fields['City'].Value
fState := RecordSet.Fields['State'].Value
end
else
begin
fCity := EmptyStr;
fState := EmptyStr;
end;
end;
end;


I hope i helped
Dimitris



"Gary Mrenak" <XXXX@XXXXX.COM>writes
news:3f3c1485$XXXX@XXXXX.COM...
>I'm losing my bearings a little here. I want to create either a class or
a
>non-visual component that, given a value, can access a database to look
up
>information about that value. For example, given a zip code database, I
want
>a class or non-visual component that, once instantiated and given a zip
>code, looks up and returns City/State. I can, of course, do this easily
>using VCL components (a form, a data module, and data access
components).
>But how do I make this all non-visual? Something I can instantiate in
any
>application, then call its methods and get results. Seems I can not use a
Data
>Module and data access components because the data module needs a visual
>owner (I think anyway).
>
>I'm lost!
>
>