我正在尝试将 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 查询的结尾。现在可以了。对不起,这是一个无辜的错字。感谢所有回复的人。