简体   繁体   English

使用C#在Crystal Reports中使用外键显示来自两个表的数据

[英]Display data from two tables with foreign key in Crystal Reports using C#

I have two tables, BonLivraison and LigneBonLivraison : 我有两个表BonLivraisonLigneBonLivraison

BonLivraison : CodeBonLivraison , DateBonLivraison BonLivraisonCodeBonLivraisonDateBonLivraison

LigneBonLivraison : Id , Quantite , LibArticleAr , CodeBonLivraison (Foreign key from BonLivraison table). LigneBonLivraisonIdQuantiteLibArticleArCodeBonLivraison (BonLivraison表中的外键)。

I used this code in order to display data from these two tables in Crystal Reports: 我使用此代码来显示Crystal Reports中这两个表中的数据:

string sql3 = "SELECT BonLivraison.CodeBonLivraison, BonLivraison.DateBonLivraison, LigneBonLivraison.Quantite, LigneBonLivraison.LibArticleAr FROM BonLivraison, LigneBonLivraison WHERE BonLivraison.CodeBonLivraison = @numm AND MONTH(BonLivraison.DateBonLivraison) = @d1 AND BonLivraison.CodeBonLivraison = LigneBonLivraison.CodeBonLivraison";

SqlCommand cmd3 = new SqlCommand(sql3, con);
cmd3.Parameters.AddWithValue("@numm", row);
cmd3.Parameters.AddWithValue("@d1", Convert.ToInt32(comboBox1.SelectedValue));
SqlDataAdapter dscmd3 = new SqlDataAdapter(cmd3);

dscmd3.Fill(dsS, "BonLivraison");
dscmd3.Fill(dsS, "LigneBonLivraison");
CrystalReport6 objRpt6 = new CrystalReport6();
objRpt6.SetDataSource(dsS);

crystalReportViewer6.ReportSource = objRpt6;

But this code displays doubled data like this: 但是这段代码显示的数据是这样的两倍:

在此处输入图片说明

It should display just one record "HP S1933 MONITOR" and one record "CANON LIDE 110". 它应仅显示一条记录“ HP S1933 MONITOR”和一条记录“ CANON LIDE 110”。 I tried INNER JOIN , but had same problem, how do I solve this? 我尝试了INNER JOIN ,但是遇到了同样的问题,如何解决呢?

You should only fill your dataset dsS once. 您应该只填充一次数据集dsS。

You are joining two tables, but end up still with one result set of data. 您正在联接两个表,但最终仍得到一个结果数据集。

You also don't need to use that overload of Fill to specify a table. 您也不需要使用Fill的重载来指定表。 Just do: 做就是了:

dscmd3.Fill(dsS);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM