[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Connecting new component to multiple Databases!

Posted on 2006-05-29
10
Medium Priority
?
217 Views
Last Modified: 2010-08-05
Hi
I'm working on a helper component for database programs. This component
derived from TComponent and connect to a TTreeView and load a tree structure
into TTreeView. I want my component could work with multiple Databases like
ADO, BDE, Interbase, DBISAM, etc.

What's the best solution for that? If I set my connection property to
TADOConnection then my component could not work with Paradox. If I set my
connection property as TCustomConnection, in the component I want to create
a DataSet and connect to database. then I don't know to create with kind of
DataSet? TADOTable, TTable, TIBTable, TDBISAMTable?

Does somebody know a good soltution?

Regards

0
Comment
Question by:Mamouri
  • 4
  • 4
  • 2
10 Comments
 
LVL 10

Assisted Solution

by:atul_parmar
atul_parmar earned 250 total points
ID: 16784482
Seperate your data connectivity into two layer. 1. Generic 2. Specific.
1. For generic implementation, you introduce a property of type TDataset and let the user to set it. This way user can select and use database of his choice. As you might be well aware that TADOTable, TTable, TIBTable, TDBISAMTable, ... all are derived from TDataset so it is compitible with all existing and future data access components.

2. You can devide this layer into two.
a. Leave data connectivity at end so you have not to bother abt it. User will create a connection and pass the TDataset descendent to you.
b. You provide connectivity. Here you can choose for which you want to provide data connectivity. i.e. ADO, BDE etc. then write that specific data access components that connect to your generic component.
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16784492
Sorry for my BAD english.
0
 
LVL 3

Author Comment

by:Mamouri
ID: 16784652
I could not get your idea completely. do you know a component that used such idea for connectivity?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:TheRealLoki
ID: 16785691
I agree with atul_parmer, try to keep it abstract at the TTreeview level, and use "TDataset".
and when you need to talk to the treeview do something like this

procedure DoTreeViewThing(dataset: TDataset);
...
if "using paradox table"  then
  DoTreeViewThing( TDataset(Table1) )  // might be (Table1 AS TDataset) ... I forget which :-)
else if "using ado table" then
  DoTreeViewThing( TDataset(ADOTable1) )
... etc

procedure MyComponent.DoTreeViewThing(dataset: TDataset);
begin
    for i := 0 to pred(dataset.fields.count) do ...
...
    newtreenode.caption := dataset.Fields[0].AsString; // could be TTable, TADOTable, TMemoryTable, etc...
end;
0
 
LVL 3

Author Comment

by:Mamouri
ID: 16785737
Thank you parmar and TheRealLoki!
Any other idea?
0
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 250 total points
ID: 16785777
You can make it even more abstract by using a TDataSource or TDataset as a property of your component, and do away with the "if using paradox" statements, becuase you know it will already be a TDataset.

Take a look at the TDBGrid and TDatasource components

  TDataSource = class(TComponent)
  private
    FDataSet: TDataSet;
    procedure SetDataSet(ADataSet: TDataSet);
  published
    property DataSet: TDataSet read FDataSet write SetDataSet;

  TCustomDBGrid = class(TCustomGrid)
  private
    function GetDataSource: TDataSource;
    procedure SetDataSource(Value: TDataSource);
  public
    property DataSource: TDataSource read GetDataSource write SetDataSource;

you can do the same as the TDBGrid, and have a Datasource. The source code for TDBCustomDBGrid (and TDBGrid) etc is in DBGrids.pas and DB.pas
0
 
LVL 3

Author Comment

by:Mamouri
ID: 16788297
In fact user must only specify the connection. the component itself must create required TTable or TADOTable or TDBISAMTable and work with these components.

Regads
0
 
LVL 3

Author Comment

by:Mamouri
ID: 16788303
I must mention the component I'm workin on is not a general component for every projects. This component will use only on one project.
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16788315
It seems that you didn't get it clearly. Your component will have a property say Dataset and the user will assign the TDataset compitible component to your propert what may be TTable or TADOTable or TDBISAMTable.

so user will create a connection and a table and assign it to your dataset property.

e.g. YourComponent.Dataset := Table1;
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16788320
Even if it is not generic component, the above said practice is highly flexible and preferable.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question