我有一个查询,我将其作为数据表放入数据集中。查询运行良好,但是在数据表中它没有在数据列中列出任何内容,因为我不能报告它......为什么这不起作用的任何想法?我的查询如下。
SELECT * FROM (
SELECT timeclock.dtTimeIn, timeclock.dtTimeOut, employees.sfirstname,
RANK() OVER ( ORDER BY dtTimeIn) rk1, --earliest record gets 1
RANK() OVER (ORDER BY dtTimeOut DESC) rk2 --latest record gets 1
FROM TimeClock INNER JOIN
Employees ON TimeClock.lEmployeeID = Employees.lEmployeeID
WHERE (dtTimeIn > dateadd(day, datediff(day, 0, getdate())-1, 0)) AND (dtTimeOut < dateadd(day, datediff(day, 0, getdate()), 0)) AND
(sDept IN ('1', '2', '3'))
) A
WHERE rk2=1
运行查询时的当前输出:
dtTimeIn dtTimeOut sfirstname rk1 rk2
2/7/2013 2:36:00 PM 2/7/2013 7:52:33 PM Brian 10 1
在 wizzard 中创建表适配器时...我收到一条错误消息:
The Wizard detected the following problems when configuring the TableAdapter:
"OpenTime":
Details:
Generated SELECT statement.
The OVER SQL construct or statement is not supported
To add these components to your dataset, click Finish.
我猜它不喜欢这个查询...但是我不确定除了使用“OVER”函数之外还有什么方法可以完成查询...
最佳答案
由于 TableAdapter
向导的限制,您必须使用代码创建 DataTable
。
DataTable dataTable;
using (SqlConnection sqlConn = new SqlConnection())
{
sqlConn.Open();
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter())
using (sqlDataAdapter.SelectCommand = sqlConn.CreateCommand())
{
sqlDataAdapter.SelectCommand.CommandType = CommandType.Text;
sqlDataAdapter.SelectCommand.CommandText = '
SELECT * FROM (
SELECT timeclock.dtTimeIn, timeclock.dtTimeOut, employees.sfirstname,
RANK() OVER ( ORDER BY dtTimeIn) rk1, --earliest record gets 1
RANK() OVER (ORDER BY dtTimeOut DESC) rk2 --latest record gets 1
FROM TimeClock INNER JOIN
Employees ON TimeClock.lEmployeeID = Employees.lEmployeeID
WHERE (dtTimeIn > dateadd(day, datediff(day, 0, getdate())-1, 0)) AND (dtTimeOut < dateadd(day, datediff(day, 0, getdate()), 0)) AND (sDept IN ('1', '2', '3'))
) A
WHERE rk2=1';
sqlDataAdapter.Fill(dataTable);
}
}
填充dataTable
后,只需将其分配给ReportViewer 的DataSource
ReportDataSource rds = new ReportDataSource(dataTable.TableName, dataTable);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(rds);
请注意,这些代码片段中有很大一部分未经测试。
关于c# - 如何在 Dataset DataTable 中显示 DataColumns,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14781694/