2017-07-30 49 views
1

我想從我的訪問數據庫獲取數據,並在Datagridview處顯示數據。我的表是:針對4表的內部聯接查詢的語法

TABLE 1(AB)   TABLE 2(CD)   TABLE 3(EF)  TABLE 4(GH) 
-------------  ----------------- ------------- ------------------- 
SID SName CID TID TID TName Tprice  FID CID FCp  FPID FID FCp Fprice 

裏面我是用檢索在C#中的數據的查詢是:

OleDbCommand command1 = new OleDbCommand(); 
command1.Connection = connection; 
command1.CommandText = "select T.TName, T.Tprice, P.FCp, P.Fprice from (([AB] S inner join [CD] T on S.TID = T.TID) (inner join [EF] C on S.CID = C.CID) inner join [GH] P on C.FID = P.FID where (S.SID = 2) "; 
OleDbDataReader myreader = command1.ExecuteReader(); 
while (myreader.Read()) 
{ 
    //DATA IS READ HERE 
} 

我正的錯誤是:

語法錯誤在JOIN表達

我想TName, Tprice, FCp(TABLE 4), Fprice作爲我的輸出。我做對了嗎,還是有其他方法可以做到這一點。

回答

4

在你的from你有沒有結束(第一個)的開括號。

除了固定括號是更具可讀性,如果你跳線:

command1.CommandText = @"select T.TName, T.Tprice, P.FCp, P.Fprice 
         from (([AB] S 
         inner join [CD] T on S.TID = T.TID) 
         inner join [EF] C on S.CID = C.CID) 
         inner join [GH] P on C.FID = P.FID 
         where S.SID = 2"; 

(Access requires parentheseses)

+0

現在我在查詢表達式得到錯誤語法錯誤(缺少操作員) –

+0

@NitishRanjan - 見更新 - 錯過訪問需要添加圓括號 –

+0

查詢括號內的小改動應該在'from'之後開始而不是之前 –