?
Solved

Two databases in one DbGrid

Posted on 2005-05-14
16
Medium Priority
?
232 Views
Last Modified: 2010-04-05
Is it posible to show data from to diferent databases in one DBgrid?
I using Tquery
0
Comment
Question by:selas
  • 4
  • 3
  • 3
  • +3
14 Comments
 
LVL 15

Accepted Solution

by:
mikelittlewood earned 400 total points
ID: 14003146
Only if the query joins the tables.
Not if you want to show two tables in one dbgrid.
0
 
LVL 5

Assisted Solution

by:Voodooman
Voodooman earned 400 total points
ID: 14005623

If the table has the same structure in both tables do a union query. Both tables can be displayed in the same grid.

Otherwise query the details into a Temporary table from both tables and then open your grid on the temporary table.

Voodooman
0
 
LVL 11

Assisted Solution

by:calinutz
calinutz earned 400 total points
ID: 14009419
Did you mean.. showing on a button click data from one tabel and then on another button click show data from the second database?
Theoretically it's easy to be done. You only need to specify the grid's Data Source and beware not to assign the fields inside the DBGrid, just leave it like you get it from the pallete of components.
I haven't try it yet but should work just fine.
Cheers
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Expert Comment

by:calinutz
ID: 14009436
Something like this:
object Form1: TForm1
  Left = 202
  Top = 103
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object DBGrid1: TDBGrid
    Left = 72
    Top = 96
    Width = 541
    Height = 120
    TabOrder = 0
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'MS Sans Serif'
    TitleFont.Style = []
  end
  object BitBtn1: TBitBtn
    Left = 92
    Top = 228
    Width = 75
    Height = 25
    Caption = 'BitBtn1'
    TabOrder = 1
    OnClick = BitBtn1Click
  end
  object BitBtn2: TBitBtn
    Left = 188
    Top = 228
    Width = 75
    Height = 25
    Caption = 'BitBtn2'
    TabOrder = 2
    OnClick = BitBtn2Click
  end
  object DataSource1: TDataSource
    DataSet = Table1
    Left = 284
    Top = 16
  end
  object DataSource2: TDataSource
    DataSet = Table2
    Left = 284
    Top = 60
  end
  object Database1: TDatabase
    AliasName = 'DBDEMOS'
    Connected = True
    DatabaseName = 'DBDemos'
    LoginPrompt = False
    SessionName = 'Default'
    Left = 144
    Top = 12
  end
  object Database2: TDatabase
    AliasName = 'DefaultDD'
    Connected = True
    DatabaseName = 'DefaultDD'
    SessionName = 'Default'
    Left = 144
    Top = 60
  end
  object Table1: TTable
    DatabaseName = 'DBDEMOS'
    TableName = 'animals.dbf'
    Left = 208
    Top = 12
  end
  object Table2: TTable
    DatabaseName = 'DefaultDD'
    TableName = 'bdesdd.DB'
    Left = 212
    Top = 60
  end
end
0
 
LVL 11

Expert Comment

by:calinutz
ID: 14009440
And the code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, DB, DBTables, Grids, DBGrids;

type
  TForm1 = class(TForm)
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    DataSource2: TDataSource;
    Database1: TDatabase;
    Database2: TDatabase;
    Table1: TTable;
    Table2: TTable;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
Table2.Active:=false;
Table1.Active:=false;
DBGrid1.DataSource:=DataSource1;
Table1.Active:=true;
end;

procedure TForm1.BitBtn2Click(Sender: TObject);
begin
Table1.Active:=false;
Table2.Active:=false;
DBGrid1.DataSource:=DataSource2;
Table2.Active:=true;

end;

end.
0
 
LVL 26

Expert Comment

by:Eddie Shipman
ID: 14036167
selas, please show the sql you are using or at least let us know what tables/fields you are looking to get
as well as the type of database you are using.
0
 

Author Comment

by:selas
ID: 14326980
How to joint to tables by query?
0
 
LVL 26

Expert Comment

by:Eddie Shipman
ID: 14343462
Show the SQL you are using and the expected result and we can show you.
0
 

Author Comment

by:selas
ID: 14418607
I have two databases:
one with: cardid, name, surname (data/data@test)
second: cardid, purse, balance (card/card@test)

and i want to show one DbGrid with:
cardid, name, surname, purse and balance

how to do this?
0
 
LVL 2

Assisted Solution

by:vatamy
vatamy earned 400 total points
ID: 14534086
Hi,

Lets say that : table1 = (data/data@test)
                     table2 = (card/card@test)

Select table1.cardid, table1.name, table1.surname, table2.purse, table2.balance
From table1 left join table2 on table1.cardid=table2.cardid


To sort the results descending just add the follwing:

Order by filed_name desc
(change field name to one of the fields you selected : table1.cardid)

To sort the results ascending just add the follwing:

Order by filed_name asc
(change field name to one of the fields you selected : table1.cardid)

Hope It Helps
0
 

Author Comment

by:selas
ID: 14561350
Not table1 and table2
but databases
0
 
LVL 2

Expert Comment

by:vatamy
ID: 14567469
HI,

Even if the two tables are in two databases.
Do the following.

Select Database1.table1.cardid, Database1.table1.name, Database1.table1.surname, Database2.table2.purse, Database2.table2.balance
From Database1.table1 left join Database2.table2 on Database2.table1.cardid=Database2.table2.cardid

Hope it helps.
0
 

Author Comment

by:selas
ID: 15072040
But connection strings diferent
0
 
LVL 26

Assisted Solution

by:Eddie Shipman
Eddie Shipman earned 400 total points
ID: 15072199
selas, we've asked before for you to show your SQL. please do so now.
Also, what DB are you using, it makes a difference...
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

864 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