0

我的报告不断显示我认为报告中员工字段中姓名的原始数据。sql 表具有正确的数据,但报告从来没有正确的名称。它始终显示 pk 为零的员工,而不是加入中的员工。我认为我正在显示来自expenseHdrs 和expenseDtls 字段的正确数据,因为它在报告中看起来正确但是当我在调试时将鼠标悬停在查询上时,我看到的只是费用详细信息(我的报告中的行项目)但我对 Visual Studio 比较陌生.

基本上,我的查询和我的数据源之间似乎存在脱节,或者我的连接有问题。这是我从数据源获得的 sql 选择,下面是查询。任何帮助将不胜感激。

SELECT DISTINCT expenseHdr.rptNo,expenseHdr.description,expenseHdr.purpose,
expenseHdr.reimbursement, expenseHdr.department, expenseHdr.period, 
expenseDtl.expDesc, expenseDtl.expType, expenseDtl.expCost, 
expenseDtl.expDate, employees.name, expenseDtl.expNo,   
expenseDtl.location
FROM   (Intranet.dbo.expenseHdr expenseHdr INNER JOIN
Intranet.dbo.expenseDtl expenseDtl ON 
expenseHdr.rptNo=expenseDtl.rptNo) INNER JOIN Intranet.dbo.employees 
employees ON expenseHdr.empPk=employees.pk
ORDER BY expenseDtl.expNo
private void launchReport(int pKReport)
{
    using (DataClasses1DataContext db = new DataClasses1DataContext())
    {
        var query = (from s in db.expenseHdrs
                         join d in db.expenseDtls on s.rptNo equals d.rptNo
                         where s.rptNo == pKReport
                         join g in db.employees on s.empPk equals g.pk
                         select new
                         {
                             s.period,
                             s.description,
                             s.department,
                             s.rptNo,
                             s.reimbursement,
                             s.purpose,
                             g.name,
                             d.expDate,
                             d.expType,
                             d.expDesc
                     });

        expenseReportMain cr1 = new expenseReportMain();

        cr1.FileName = @"\\SRV-NAS\Intranet\Reports\expenseReportMain.rpt";

        ConnectionInfo connectionInfo = new ConnectionInfo();
        connectionInfo.ServerName = "svr-sql";
        connectionInfo.DatabaseName = "intranet";
        connectionInfo.UserID = "sa";
        connectionInfo.Password = "******";

        SetDBLogonForReport(connectionInfo, cr1);
        cr1.SetDataSource(query);
        crystalReportViewer1.ReportSource = cr1;
    }
}

private void SetDBLogonForReport(ConnectionInfo connectionInfo,
                                 expenseReportMain reportDocument)
{
    Tables tables = reportDocument.Database.Tables;
    foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
    {
        TableLogOnInfo tableLogonInfo = table.LogOnInfo;
        tableLogonInfo.ConnectionInfo = connectionInfo;
        table.ApplyLogOnInfo(tableLogonInfo);
    }
}

我还根据请求编辑了一些示例数据:

expenseHdr             expenseDtl                employees

rptNo  empPk           expNo   rptNo             pK    name    userid
1      333             3       1                 333   Tom     tom.b
2      240             4       1                 240   Kelly   kelly.c
3      542             5       2                 542   Jean    jean.g
                       6       3

因此,员工表中的任何信息都不会在报告中正确显示。其他两个表中的数据显示正常。

4

2 回答 2

0

这很正常,因为在您的 linq 处理中,您不仅有 join 运算符,而且选择了很多。

你的 sql 查询:你有三个连接的查询

你 linq 查询:你有很多选择,有两个选择,对于每个选择,你都有一个连接

所以它总是显示 pk 为零的员工而不是加入的员工是正常的,因为您没有使用费用 Hdrs 加入员工。

 var query = (from s in db.expenseHdrs
                     join d in db.expenseDtls on s.rptNo equals d.rptNo
                     where s.rptNo == pKReport
                     join g in db.employeess on g.pk equals s.empPk
                     select new
                     {
                        //Adjust your select

                     });
于 2012-08-28T16:42:06.763 回答
0

我记得通过在两个连接之后放置 where 来更改您的查询,因为前面的定义可能会影响:

var query = (from s in db.expenseHdrs
                         join d in db.expenseDtls on s.rptNo equals d.rptNo
                         where s.rptNo == pKReport
                         join g in db.employees on s.empPk equals g.pk
                         select new
                         {
                             s.period,
                             s.description,
                             s.department,
                             s.rptNo,
                             s.reimbursement,
                             s.purpose,
                             g.name,
                             d.expDate,
                             d.expType,
                             d.expDesc
                     });

至:

var query = (from s in db.expenseHdrs
                         join d in db.expenseDtls on s.rptNo equals d.rptNo
                         join g in db.employees on s.empPk equals g.pk
                         where s.rptNo == pKReport
                         select new
                         {
                             s.period,
                             s.description,
                             s.department,
                             s.rptNo,
                             s.reimbursement,
                             s.purpose,
                             g.name,
                             d.expDate,
                             d.expType,
                             d.expDesc
                     });

将解决您的问题,因为在 sql 中您需要在 where 子句之前加入所有表。

于 2012-09-06T15:59:35.413 回答