線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1839
推到 Plurk!
推到 Facebook!

請問Delphi如何連結兩個不同的資料庫?

尚未結案
tommyli0913
一般會員


發表:9
回覆:19
積分:5
註冊:2004-02-02

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-04-25 22:12:35 IP:211.76.xxx.xxx 未訂閱
小弟有兩個不同檔案的access資料庫,而這兩個資料庫中的一個資料表中有一個相同的資料欄位,我想要將這兩個資料庫用此資料欄位做一個關聯,但是在delphi6或7中的資料庫元件的"Connectionstring"是不是只能連結一個資料庫呢? 我第一個資料庫(A1.mdb)中有一個資料表(person)其內有三個資料欄位(姓名,居住地,電話)。 我另一個資料庫(A2.mdb)中有一個資料表(salary)其內亦有三個資料欄位(姓名,公司,薪水)。 我想要在一個DBGrid中用"姓名"將兩個資料庫做關聯列出(姓名、居住地、公司、薪水、電話),要如何做呢?
stillalive
初階會員


發表:7
回覆:148
積分:41
註冊:2004-04-07

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-04-25 23:11:13 IP:211.75.xxx.xxx 未訂閱
試著另開一TABLE元件指向A2 , GRID中A1外再開新欄位 , CLIENTDATASET ADD NEW FIELD , 以LOOKUP形式帶出A2.FLD .
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-04-26 09:34:31 IP:147.8.xxx.xxx 未訂閱
select * from Person A in 'A1.mdb', Salary B in 'A2.mdb' where A.姓名=B.姓名
stillalive
初階會員


發表:7
回覆:148
積分:41
註冊:2004-04-07

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-04-26 11:42:53 IP:211.75.xxx.xxx 未訂閱
select * from Person A in 'A1.mdb', Salary B in 'A2.mdb' where A.姓名=B.姓名 ============================================================= 在VFP中 , 可以下這語法 SELECT * FROM A1!Person ,A2!Salary WHERE Person.姓名=Salary.姓名
tommyli0913
一般會員


發表:9
回覆:19
積分:5
註冊:2004-02-02

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-04-26 14:28:27 IP:211.76.xxx.xxx 未訂閱
小弟不材,有點不太了解先進們的意思,各位先進是說用SQL的SELECT語法直接下達就可以了嗎?? 在我的初淺想法是用 "TADOQuery"、"TDataSource"、"TDBGrid"三個元件來做的,而在"TADOQuery"的元件,必須要設ADOQuery1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\A1.mdb;Persist Security Info=False'; 的資料庫連結,才能做SQL的"SELECT"語法指令,是不是如此呢??還是不用做connectionstring的資料庫連結設定就可以直接做select的指令呢???
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-04-26 14:53:24 IP:147.8.xxx.xxx 未訂閱
For connection string built using A1.mdb, try:    
select * from Person A, Salary B in 'C:\A2.mdb'
  where A.姓名=B.姓名
The in 'filename' is Access syntax.
stillalive
初階會員


發表:7
回覆:148
積分:41
註冊:2004-04-07

發送簡訊給我
#7 引用回覆 回覆 發表時間:2004-04-26 14:57:15 IP:211.75.xxx.xxx 未訂閱
Sorry , 我想成VFP中PASS TROUGH方式下答SQL指令 . 你還是試一試LOOKUP方式看看 .
stillalive
初階會員


發表:7
回覆:148
積分:41
註冊:2004-04-07

發送簡訊給我
#8 引用回覆 回覆 發表時間:2004-04-26 22:05:11 IP:211.75.xxx.xxx 未訂閱
William 大大你好 , 閣下方式似乎行不通 . Tommy 網友 送佛上西天 . Reference Below Article #16285: How to Create a TDBGrid Lookup Field in 32-bit Delphi Technical Information Database TI1285D.txt - How to Create a TDBGrid Lookup Field in 32-bit Delphi Category :Database Programming Platform :All-32Bit Product :All32Bit, Description: 1. Drop 2-TTable's, 1-TDataSource and 1-TDBGrid on a form. 2. Connect Table1 to DataSource1 to DBGrid1 a. DataSource1.DataSet = Table1 b. DBGrid1.DataSource = DataSource1 3. Setup Table1 a. Table1.Database = DBDemos b. Table1.TableName = Customer c. Table1.Active = True 4. Setup Table2 a. Table2.Database = DBDemos b. Table2.TableName = Orders c. Table2.Active = True 5. Add all of the fields for Table1 by bringing up the Fields Editor: a. Double click on Table1 b. Right click on Fields Editor c. Add New Fields. Add all of them 6. Add a new field for Table1. a. Right click on Fields Editor, and select New Field. 7. Specify the following parameters for the newly added field. a. Name: Bob b. Type: String c. Size: 30 d. Select Lookup e. Key Fields: CustNo - Field in Table1 to store value f. DataSet: Table2 - Table lookup is being done on g: LookUpKeys: CustNo - This Key gets copied to KeyField h: Result Field: OrderNo - Value to display to the user in the drop down box 8. Run the application
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#9 引用回覆 回覆 發表時間:2004-04-27 09:37:50 IP:147.8.xxx.xxx 未訂閱
quoted from Access help:    

SELECT Statement

Instructs the Microsoft Jet database engine to return information from the database as a set of records.
Syntax
SELECT [predicate] { * | table.* | [table.]field1 [AS alias1] [, [table.]field2 [AS alias2] [, ...]]}
FROM tableexpression [, ...] [IN externaldatabase]
[WHERE... ]
[GROUP BY... ]
[HAVING... ]
[ORDER BY... ]
[WITH OWNERACCESS OPTION]
The SELECT statement has these parts:
Part Description
predicate One of the following predicates: ALL, DISTINCT, DISTINCTROW, or TOP. You use the predicate to restrict the number of records returned. If none is specified, the default is ALL.
* Specifies that all fields from the specified table or tables are selected.
table The name of the table containing the fields from which records are selected.
field1, field2 The names of the fields containing the data you want to retrieve. If you include more than one field, they are retrieved in the order listed.
alias1, alias2 The names to use as column headers instead of the original column names in table.
tableexpression The name of the table or tables containing the data you want to retrieve.
externaldatabase The name of the database containing the tables in tableexpression if they are not in the current database.
The in externaldatabase works perfectly for Access files, don't know about others.
tommyli0913
一般會員


發表:9
回覆:19
積分:5
註冊:2004-02-02

發送簡訊給我
#10 引用回覆 回覆 發表時間:2004-04-27 13:38:51 IP:211.76.xxx.xxx 未訂閱
william板主先進,我將您的方法寫在"ADOQuery1.SQL.ADD('select * from Person A, Salary B in ''C:\A2.mdb'' where A.姓名=B.姓名');"然後 "ADOQuery1.Active :=True;" 結果就出現如下圖的錯誤訊息,不知道是那裡出了問題?? 若我的ADOQuery1 connection string 是A1.mdb ,而下"ADOQuery1.SQL.ADD('select * from salary in ''C:\A2.mdb'');"是可以run的。 另外stillalive先進,您的方法我有試過可以用說,不過有一個問題是如何用SQL語法來寫呢?因為我有要做姓名的查詢是篩選等等查詢動作,所以也必須要用SQL法來寫。 一直麻煩大家,實在不好意思,煩請各位先進再幫幫小弟解決這個困擾的問題,謝謝囉。
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#11 引用回覆 回覆 發表時間:2004-04-27 14:38:30 IP:147.8.xxx.xxx 未訂閱
Oh... only one DB at a time.. let me check 發表人 - william 於 2004/04/27 14:51:44
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#12 引用回覆 回覆 發表時間:2004-04-27 15:09:44 IP:147.8.xxx.xxx 未訂閱
My fault  >
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#13 引用回覆 回覆 發表時間:2004-04-27 15:35:05 IP:147.8.xxx.xxx 未訂閱
Find a way now (looks quite odd though) without linked table: select * from Person A inner join (select * from Salary in 'C:\A2.mdb') B on A.姓名=B.姓名
tommyli0913
一般會員


發表:9
回覆:19
積分:5
註冊:2004-02-02

發送簡訊給我
#14 引用回覆 回覆 發表時間:2004-04-27 19:09:36 IP:211.76.xxx.xxx 未訂閱
william板主,感謝您的幫助,我的兩個mdb資料庫終於能連結起來了,實在太謝謝了^__^。 另外,還有一個問題想再請教一下,目前我是用有路徑檔名的mdb檔來做資料庫連結,但若是另一個資料庫是SQL SERVER,如資料庫名稱為"SQLA",那如何與mdb作資料庫連結呢? 因為目前的SQL語法是絕對路徑的檔名的資料庫,SQL SERVER 勢必要用ODBC或相關的方式才有可能做資料庫連結,對吧??
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#15 引用回覆 回覆 發表時間:2004-04-28 09:33:38 IP:147.8.xxx.xxx 未訂閱
No experience, but linked tables can do the job.... In Access, File|Get External Data|Link Tables, select ODBC Databases().... Of course you need to setup proper ODBC DSN first.
tommyli0913
一般會員


發表:9
回覆:19
積分:5
註冊:2004-02-02

發送簡訊給我
#16 引用回覆 回覆 發表時間:2004-04-28 11:35:16 IP:211.76.xxx.xxx 未訂閱
我了解了,感謝各位先進的幫忙,這裡真是像我們這種初學delphi資料庫的很讚的地方,將來我的能力達一定水平且此板有我可以解答的問題,我一定要盡我所知來貢獻於此,再次感謝各位先進喔!
系統時間:2024-09-12 12:51:48
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!