2017-06-02 63 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