SQL语句没有给出正确的输出

时间:2013-06-11 12:55:26

标签: c# sql sql-server parameters

我有一个数据库驱动的webapp。在我的数据库中,我有四个表:

User
----
UserID
FirstName
LastName

UserGroup
---------
UserGroupID
UserID
GroupID

Group
-----
GroupID
Name

Adress
------
AdressID
UserID
Street
...

使用我当前的SQL语句,我什么都没有回复。这是我的SQL:

select 
    a.UserID, a.FirstName, a.LastName, b.GroupID, b.UserID, 
    c.GroupID, c.Name, d.AdressID, d.UserID, 
from User as a, UserGroup as b, Group as c, Adress as d 
where a.UserID = b.UserID and b.GroupID = c.GroupID 
    and a.UserID = d.UserID and d.AdressID= @id"

我使用以下语句提供变量id:

dbCommand.Parameters.Add("@id", SqlDbType.Int).Value = id;

知道为什么我没有得到任何回报?我也没有任何错误。我将结果存储在DataTable中,调试时为空。

2 个答案:

答案 0 :(得分:3)

你在'from'之前有逗号,而且你使用inner join比使用select a.UserID, a.FirstName, a.LastName, b.GroupID, b.UserID, c.GroupID, c.Name, d.AdressID, d.UserID from User as a inner join UserGroup as b on b.UserId = a.UserId inner join Group as c on c.GroupID = b.GroupID inner join Adress as d on a.UserID = d.UserID where d.AdressID = @id 更好:

{{1}}

尝试这个。

答案 1 :(得分:1)

尝试使用INNER JOIN查询

SELECT a.UserID, a.FirstName, a.LastName, b.GroupID, b.UserID, c.GroupID, c.Name, d.AdressID, d.UserID
FROM User as a
INNER JOIN UserGroup as b 
ON a.UserID = b.UserID 
INNER JOIN Group as c 
ON b.GroupID = c.GroupID
INNER JOIN Adress as d 
ON a.UserID = d.UserID
WHERE d.AdressID= @id