-6

我正在尝试将 DataBind 绑定到 GridView 控件,但它始终失败。

给定以下代码(为了便于阅读,sql 扩展了多行):

protected void Page_Load(object sender, EventArgs e)
{

string dsn = "foo";
string sql = "SELECT * FROM (SELECT F.Project AS 'Project Number', F.Account AS   
             'Account', F.Pd AS Period, F.Incurred AS Totals, C.Project AS 
             'Project Name' FROM Ultron.Final F INNER JOIN Ultron.Custom  
              C ON F.Project = C.Project WHERE F.Project LIKE '61000.003%' AND 
              F.Account NOT LIKE '%-01' AND F.Fy = YEAR(GetDate()) ) Budget PIVOT 
              (SUM(Totals) FOR Period in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],
              [11],[12]) AS PivotTable";
using (SqlConnection conn = new SqlConnection(dsn))
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
    conn.Open();
    SqlDataReader reader = cmd.ExecuteReader();
    testGrid.DataSource = reader;
    testGrid.DataBind();
}
}    

我确定该错误与查询中的括号有关,但这些是查询工作所必需的。在 SSMS 中测试时,查询通过没有问题。

我如何重组它以使代码不会失败?

编辑

我错过As PivotTable了 SQL 查询的结尾。现在可以了。对不起,这是一个无辜的错字。感谢所有回复的人。

4

3 回答 3

3

缺少空间和关闭)

protected void Page_Load(object sender, EventArgs e)
{

   string dsn = "foo";
   string sql = @"SELECT * FROM 
                  (
                     SELECT F.Project AS 'Project Number', F.Account AS   
                          'Account', F.Pd AS Period, F.Incurred AS Totals, C.Project AS 
                          'Project Name' FROM Ultron.Final F INNER JOIN Ultron.Custom  
                          C ON F.Project = C.Project 
                     WHERE 
                          F.Project LIKE '61000.003%' AND 
                          F.Account NOT LIKE '%-01' AND F.Fy = YEAR(GetDate()) 
                   ) Budget PIVOT 
                   (
                     SUM(Totals) FOR Period in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],
                     [11],[12])
                   )";
   using (SqlConnection conn = new SqlConnection(dsn))
   using (SqlCommand cmd = new SqlCommand(sql, conn))
   {
       conn.Open();
       SqlDataReader reader = cmd.ExecuteReader();
       testGrid.DataSource = reader;
       testGrid.DataBind();
   }
}    

但实际上你应该为这样的东西创建一个存储过程:

CREATE PROCEDURE [GetProjectBudgetInfo]
AS
BEGIN
    SET NOCOUNT ON;

    SELECT * FROM 
                      (
                         SELECT F.Project AS 'Project Number', F.Account AS   
                              'Account', F.Pd AS Period, F.Incurred AS Totals, C.Project AS 
                              'Project Name' FROM Ultron.Final F INNER JOIN Ultron.Custom  
                              C ON F.Project = C.Project 
                         WHERE 
                              F.Project LIKE '61000.003%' AND 
                              F.Account NOT LIKE '%-01' AND F.Fy = YEAR(GetDate()) 
                       ) Budget PIVOT 
                       (
                         SUM(Totals) FOR Period in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
                       )

END

然后在代码中调用它:

protected void Page_Load(object sender, EventArgs e)
{

   string dsn = "foo";
   string sql = @"GetProjectBudgetInfo";
   using (SqlConnection conn = new SqlConnection(dsn))
   using (SqlCommand cmd = new SqlCommand(sql, conn))
   {
       cmd.CommandType = CommandType.StoredProcedure;
       conn.Open();
       SqlDataReader reader = cmd.ExecuteReader();
       testGrid.DataSource = reader;
       testGrid.DataBind();
   }
}    
于 2013-06-28T17:27:28.023 回答
1

如果我数得正确,您的陈述末尾缺少括号。

string sql = "SELECT * FROM (SELECT F.Project AS 'Project Number', F.Account AS   
             'Account', F.Pd AS Period, F.Incurred AS Totals, C.Project AS 
             'Project Name' FROM Ultron.Final F INNER JOIN Ultron.Custom  
              C ON F.Project = C.Project WHERE F.Project LIKE '61000.003%' AND 
              F.Account NOT LIKE '%-01' AND F.Fy = YEAR(GetDate()) ) Budget PIVOT 
              (SUM(Totals) FOR Period in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],
              [11],[12]))";
于 2013-06-28T17:27:47.947 回答
0

)在您的 sql 查询中缺少最后一个。试试这个:

protected void Page_Load(object sender, EventArgs e)
{    
    string dsn = "foo";
    string sql = "SELECT * FROM (SELECT F.Project AS 'Project Number', F.Account AS   
                 'Account', F.Pd AS Period, F.Incurred AS Totals, C.Project AS 
                 'Project Name' FROM Ultron.Final F INNER JOIN Ultron.Custom  
                  C ON F.Project = C.Project WHERE F.Project LIKE '61000.003%' AND 
                  F.Account NOT LIKE '%-01' AND F.Fy = YEAR(GetDate()) ) BudgetPIVOT  
                  (SUM(Totals) FOR Period in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],
                  [11],[12]))";

    using (SqlConnection conn = new SqlConnection(dsn))
    using (SqlCommand cmd = new SqlCommand(sql, conn))
    {
        conn.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        testGrid.DataSource = reader;
        testGrid.DataBind();
    }
}    
于 2013-06-28T17:26:23.220 回答