1

我正在处理的数据集需要帮助我有一个包含 2 个表的数据集,其中一个仅包含主键值,即“Statementnumber”,另一个包含具有一对多关系的其余详细信息

现在我想解析我的一个表中的每个语句编号,从第二个表中获取详细信息并将该记录放入新的数据表中。一次使用 for 循环记录一条记录,直到我到达主表中的记录末尾。我该怎么做这是c#?

到目前为止,我在同一个数据集中有两个表。

这是我已经拥有的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Oracle.DataAccess.Client;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;


namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string connetionString = null;
            OracleConnection connection;
            OracleDataAdapter OracleAdapter;
            DataSet ds = new DataSet();
            string firstSql = null;


            connetionString = "DataSoruce

            connection = new OracleConnection(connetionString);
            firstSql = @"SELECT DISTINCT statement_header.statementnumber,
                     statement_details.invoicedate,
                     statement_details.invoicenumber,
                     statement_details.invoicetotal,
                     statement_details.doc_type,
                     statement_header.statementtotal,
                     statement_details.bunumber_ru,
                     statement_details.bunumber,
                     statement_details.description,
                     statement_details.reference_number,
                     statement_header.remto_zip,
                     statement_header.remto_city,
                     statement_header.remto_state,
                     statement_header.remto_mailname,
                     statement_header.remto_addr1,
                     statement_header.remto_addr2,
                     statement_header.remto_addr3,
                     statement_header.soldto_city,
                     statement_header.soldto_state,
                     statement_header.soldto_zip,
                     statement_header.soldto_addr1,
                     statement_header.soldto_addr2,
                     statement_header.soldto_addr3,
                     statement_header.balance_forward,
                     statement_header.statementdate,
                     statement_header.custid,
                     statement_header.custname,
                     statement_header.phone_prefix,
                     statement_header.phone_number,
                     statement_details.purchases,
                     statement_details.payments,
                     statement_details.misc_credit2,
                     statement_details.misc_credit1,
                     statement_header.company_number,
                     statement_header.statementpurchases,
                     statement_header.statementpayments,
                     statement_header.statementmisc_credit1,
                     statement_header.statementmisc_credit2,
                     statement_header.nomailnoprint,
                     statement_header.SOLDTOCOUNTRYCODE,
                     statement_header.SOLDTOCOUNTRYNAME,
                     statement_header.CREDITZEROFLAG
       FROM STATEMENT_DATA_DOMESTIC statement_header
            INNER JOIN STATEMENT_DATA_DOM_DETAILS statement_details
               ON statement_header.statementnumber =
                     statement_details.statementnumber";
            string secondSql = "select statementnumber from statement_data_domestic";
                connection.Open();
                OracleAdapter = new OracleDataAdapter(firstSql, connection);
                OracleAdapter.Fill(ds, "domestic");
                OracleAdapter = new OracleDataAdapter(secondSql, connection);
                OracleAdapter.Fill(ds, "statement");

            OracleAdapter.Dispose();
                connection.Close();

                ds.Relations.Add("Statementnumber", ds.Tables["statement"].Columns["statementnumber"], ds.Tables["domestic"].Columns["statementnumber"]);



    //GridView1.DataSource = ds.Tables[1];
           //   GridView1.DataBind();
                ReportDocument reportDoc = new ReportDocument();
                reportDoc.Load(@"c:\users\soniara\desktop\statement.rpt");
                DataTable d3 = ds.Tables["statement"];
                foreach (DataRow arpan in d3.Rows)
                {
                    DataRow[] details = arpan.GetChildRows("Statementnumber");


                    foreach (DataRow detail in details)
                    {

                        reportDoc.SetDataSource(detail);
                        ExportOptions CrExportOptions;
                        DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
                        PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
                        CrDiskFileDestinationOptions.DiskFileName = @"d:\Converte5_1_13"+detail+".pdf";
                        CrExportOptions = reportDoc.ExportOptions;
                        {
                            CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                            CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                            CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
                            CrExportOptions.FormatOptions = CrFormatTypeOptions;

                        }
                        reportDoc.Export();

                    }
                }








        }
    }
}

基本上我想从我的语句表中一次检索一条记录并从详细信息表中获取所有详细信息并通过水晶报告运行它以获得 pdf 输出

如果我只是将整个数据集作为源代码提供给 crytalreport.setdatasrouce,我的代码就可以正常工作

但它会生成一个很长的 pdf 文件,而不是小的单个语句文件。这就是我想要做的。

4

1 回答 1

0

[已替换] 如果 CrystalReports 支持 DataView 对象作为数据源,则可以使用:

        foreach (DataRow statementRow in ds.Tables["statement"].Rows)
        {
            var detail = new DataView(ds.Tables["domestic"])
            {
                RowFilter = String.Format("statementnumber = {0}", statementRow["statementnumber"]),
                Sort = "" // optional
            };


            reportDoc.SetDataSource(detail);
                            ....
                      }

否则你可以使用这个:

    foreach (DataRow statementRow in ds.Tables["statement"].Rows)
    {
        var detail = GetFilteredTable(ds.Tables["domestic"], statementRow["statementNumber"]);

        reportDoc.SetDataSource(detail);
                        ....
    }


    public DataTable GetFilteredTable(DataTable dt, object statementNumber)
    {
        var detailRows = dt.Select(String.Format("statementnumber = {0}", statementNumber));
        var filteredDt = dt.Clone();
        foreach (var detailRow in detailRows)
        {
            filteredDt.Rows.Add(detailRow.ItemArray);
        }
        return filteredDt;
    }

另一个建议是,您可能想要研究 Crystal 报表中的分组,您可以通过为语句编号创建参数并将其传递到报表本身并让它为您过滤来实现您所需要的。我不知道细节,但我知道这是可能的。

于 2013-05-08T14:30:34.927 回答