C#WinForm - Crystal Report无法从内部联接查询中显示正确的结果

时间:2016-07-14 02:46:40

标签: c# sql-server winforms crystal-reports

我正在尝试在Crystal Report Viewer中执行内部联接查询。从SQL Server中的“新查询”部分运行时,查询有效。

SELECT TABLE1.column1,TABLE2.column1
FROM TABLE2 INNER JOIN TABLE1 ON TABLE2.Identifier=TABLE1.Identifier
WHERE DateFrom='1/1/2016' AND DateTo='1/1/2017'

但是当我将我的代码放入报告中时,相比之下结果是不正确的。当从报告运行此SQL时,为什么结果会有所不同?

DBConnection dbcon = new DBConnection();

cystalreportName crn = new cystalreportName();
private void Form1_Load(object sender, EventArgs e)
{
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = dbcon.getcon();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "..."; // the inner join query the I used.

    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = cmd;
    DataSet ds = new DataSet();
    da.Fill(ds, "TABLE2");
    crn.SetDataSource(ds.Tables["TABLE2"]);

    crystalreportviewer.ReportSource = crn;
    crystalreportviewer.Refresh();
}

1 个答案:

答案 0 :(得分:0)

我建议先在您正在运行的数据库中运行查询,然后将其粘贴到项目中或粘贴到命令(Crystal Report向导)。它将解决您的问题。

像这个示例查询:

    select a.user_id,a.user_name,b.desctiption from
    (select user_id,user_name from user) as a
    left join
    (select user_id,description from item) as b
    on a.user_id = b.user_id



    or you can make it stored procedure:
    create procedure proc_sample()
    begin
       select a.user_id,a.user_name,b.description from
       (select user_id,user_name from user) as a
       left join
       (select user_id,description from item) as b
       on a.user_id = b.user_id;
    end


    Calling procedure:

    call proc_sample() at your project or in crystal report command