How to show DataColumns in Dataset DataTable

2019-07-08 10:22发布

I have a query that I put into a dataset as a datatable. The query runs great, however in the datatable it does not list anything in the Data Column, there for I cannot report it... Any ideas why this is not working? My Query is below.

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 

Current output when I run the query:

  dtTimeIn                    dtTimeOut       sfirstname    rk1      rk2
2/7/2013 2:36:00 PM 2/7/2013 7:52:33 PM        Brian        10        1

enter image description here

When Creating the Table adapter in the wizzard... I get an error message:

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.

I'm guessing it doesn't like the query... But i'm not sure how else to accomplish the query other than using the "OVER" function...

3条回答
不美不萌又怎样
2楼-- · 2019-07-08 10:29

You could create a view from your query and then simply select from your view, this should hide the OVER SQL from the tableAdapter.

查看更多
Root(大扎)
3楼-- · 2019-07-08 10:32

Due to the limitations of the TableAdapter wizard, you have to create the DataTable using code.

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);
    }
}

After having your dataTable filled, simply assign it to the ReportViewer's DataSource

ReportDataSource rds = new ReportDataSource(dataTable.TableName, dataTable);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(rds);

Please note that a large part of these code snippets are untested.

查看更多
别忘想泡老子
4楼-- · 2019-07-08 10:45

I think instead of using the drag-and-drop interface to create a tableAdapter, because your query contains the OVER SQL construct (like the error says,) you'll have to create your tableAdapter and fill your datatable in code.

查看更多
登录 后发表回答