BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Writing Data Access component

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi VCL Components Writing
View previous topic :: View next topic  
Author Message
Gary Mrenak
Guest





PostPosted: Thu Aug 14, 2003 10:54 pm    Post subject: Writing Data Access component Reply with 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't use a Data
Module and data access components because the data module needs a visual
owner (I think anyway).

I'm lost!


Back to top
Stefanides Dimitris
Guest





PostPosted: Wed Aug 20, 2003 8:52 am    Post subject: Re: Writing Data Access component Reply with 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" <gmrenak (AT) earthlink (DOT) com> wrote

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't use a
Data
Module and data access components because the data module needs a visual
owner (I think anyway).

I'm lost!





Back to top
Gary Mrenak
Guest





PostPosted: Tue Aug 26, 2003 7:46 pm    Post subject: Re: Writing Data Access component Reply with quote



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" <distef01 (AT) yahoo (DOT) gr> wrote

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" <gmrenak (AT) earthlink (DOT) com> wrote in message
news:3f3c1485$1 (AT) newsgroups (DOT) borland.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't use a
Data
Module and data access components because the data module needs a visual
owner (I think anyway).

I'm lost!








Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi VCL Components Writing All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.