2017-06-02 60 views
0

當我做查詢創建一個新表與其他表

SELECT a.ID, 
     a.Employee, 
     b.Name, 
     b.[Open Date] 
    FROM tblEmployees a, 
     Stores b 

它工作得很好,但是當我做查詢

SELECT a.ID, 
     a.Employee, 
     b.Name, 
     b.[Open Date], 
     c.Task 
    FROM tblEmployees a, 
     Stores b, 
     tblTasks c 

它不工作。

它不斷sda.Fill(dt)給了一個錯誤:

的System.OutOfMemoryException: 'System.OutOfMemoryException的'

private DataTable GetData() 
{ 
    string connString = @"Data Source=aa.database.windows.net;Initial Catalog=aa;Persist Security Info=True;User ID=aa;Password=aa"; 
    string query = "SELECT a.ID, a.Employee, b.Name, b.[Open Date], c.Task FROM tblEmployees a, Stores b, tblTasks c"; 
    using (SqlConnection con = new SqlConnection(connString)) 
    { 
     using (SqlCommand comm = new SqlCommand(query)) 
     { 
      using (SqlDataAdapter sda = new SqlDataAdapter()) 
      { 
       comm.Connection = con; 
       sda.SelectCommand = comm; 
       using (DataTable dt = new DataTable()) 
       { 
        sda.Fill(dt); 
        return dt; 
       } 
      } 
     } 
    } 
} 
+3

查詢返回多少條記錄? – hardkoded

+0

[system.outofmemoryexception填充DataAdapter時可能有的重複?](https://stackoverflow.com/questions/5092510/system-outofmemoryexception-when-filling-dataadapter) – sab669

+0

您使用舊式連接語法:'FROM tblEmployees,商店,tblTask​​s'將返回所有這些表的笛卡爾乘積('CROSS JOIN'),這可能會很大 - 而且根本不是你想要的。閱讀'INNER JOIN'。如果您希望以某種方式在一個查詢中查找所有表,您需要一個'UNION ALL'(但您會得到一組奇怪的列),或者只是將三個單獨的查詢分成三個單獨的表。 –

回答

0

或者,你可以讓內的連接where子句:

SELECT a.ID, 
     a.Employee, 
     b.Name, 
     b.[Open Date], 
     c.Task 
    FROM tblEmployees a, 
     Stores b, 
     tblTasks c 
    where a.id = b.employeeid -- clearly made up these name 
    and c.taskownerid= a.id 

順便說一句,這個問題的標題似乎與實際請求大不相同。

+0

謝謝你的答案。我解決了它! – Scarlett

2

所有表中的數據鏈接?如果因此你必須要明確使用連接語句

SELECT a.ID, 
     a.Employee, 
     b.Name, 
     b.[Open Date], 
     c.Task 
    FROM tblEmployees a 
    JOIN Stores b on b.id = a.id_store -- explicit our link here 
    JOIN Tasks c on c.id = a.id_task -- explicit our link here 

此鏈接,但如果你想從3個不同的表請求數據的做得更好3個請求

+0

非常感謝!我解決了它:) – Scarlett