我的报告不断显示我认为报告中员工字段中姓名的原始数据。sql 表具有正确的数据,但报告从来没有正确的名称。它始终显示 pk 为零的员工,而不是加入中的员工。我认为我正在显示来自expenseHdr
s 和expenseDtl
s 字段的正确数据,因为它在报告中看起来正确但是当我在调试时将鼠标悬停在查询上时,我看到的只是费用详细信息(我的报告中的行项目)但我对 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
因此,员工表中的任何信息都不会在报告中正确显示。其他两个表中的数据显示正常。