0

我在GridView从 SQL Server 提取信息时,在我的 . 我正在使用存储过程并使用一些 C# 来设置参数,然后使用 将SqlDataSource存储过程中的 sql 数据放入GridView. 我也看过一些其他的问题,就像我的一样,但是 SQL 脚本与我的不同。我也知道我的 C# 代码工作得很好,但我也会发布,

这是代码:

SQL:

SELECT 
    people.name, comage.age, grad.date
FROM 
    people
INNER JOIN 
    (SELECT age, MAX(term)[term] 
     FROM comage GROUP BY date) comage ON people.term = comage.term
INNER JOIN 
    (SELECT date, MAX(term)[term] 
     FROM grad GROUP BY date) grad ON people.term = grad.term
WHERE 
    people.ID = @ID
ORDER BY 
    LastName

ASP.NET:

    <asp:GridView ID="grid" runat="server"></GridView>

C#代码:

    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "prcedureName";
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    SqlParameter param = null;
    param = cmd.Parameters.Add("@ID",System.Data.SqlDataType.NChar,255);
    param.Direction = System.Data.ParameterDirection.Input;
    param.value = in; //in is a string of some value
    cmd.Connection = con; //con is a SqlConnection of some sort
    SqlDataAdapter adp = new SqlDataAdapter(cmd);
    SqlCommandBuilder command = new SqlCommandBuilder(adp);
    DataTable data = new DataTable();
    data.Locale = System.Globalization.CultureInfo.InvariantCulture;
    adp.Fill(data);
    grid.DataSource = data;
    grid.AutoGenerateColumns = true;
    grid.DataBind();

以下是表格:

表:

    --------------------
    |name  | ID | term |
    --------------------
    |jeff  | 0  |   A  |
    |Jake  | 1  |   B  |
    --------------------

图像表:

    --------------------
    |Age  |   Term     |
    --------------------
    |23   |    A       |
    |25   |    B       |
    --------------------

毕业表:

    --------------------
    | Date |    Term   |
    --------------------
    | DEC  |     A     |
    | SUM  |     B     |
    --------------------
4

2 回答 2

2

您确定您的 SQL 中没有向后聚合吗?我本来希望它更像这样写的:

SELECT people.name, Max(comage.age) age, Max(grad.date) date
FROM   people
INNER JOIN comage ON people.term = comage.term
INNER JOIN grad ON people.term = grad.term
WHERE people.ID = @ID
GROUP BY people.name
ORDER BY LastName

这将产生如下结果集:

name | age | date
------------------
Jeff |  23 | DEC
Jake |  25 | SUM
于 2013-08-08T20:55:07.130 回答
0

根据我提交的 ID,我只得到一条记录。

DECLARE @People TABLE ([Name] VARCHAR(10), ID INT, Term VARCHAR(10));
DECLARE @Comage TABLE (Age INT, Term VARCHAR(10));
DECLARE @Grad TABLE ([Date] VARCHAR(10), Term VARCHAR(10));
DECLARE @ID INT;

SET @ID = 0;

INSERT INTO @People VALUES ('Jeff', 0, 'A');
INSERT INTO @People VALUES ('Jake', 1, 'B');

INSERT INTO @Comage VALUES (23, 'A');
INSERT INTO @Comage VALUES (25, 'B');

INSERT INTO @Grad VALUES ('DEC', 'A');
INSERT INTO @Grad VALUES ('SUM', 'B');

SELECT age, MAX(term) [term]
INTO #Comage
FROM @Comage 
GROUP BY [Age]

SELECT [Date], MAX(term) [term] 
INTO #Grad
FROM @Grad GROUP BY [Date]

SELECT 
    p.name, c.age, g.[Date]
FROM 
    @People p
INNER JOIN #Comage c
    ON c.term = p.term
INNER JOIN #Grad g
    ON g.term = p.term
WHERE 
    p.ID = @ID

DROP TABLE #Comage
DROP TABLE #Grad
于 2013-08-08T20:59:53.800 回答