创建一个临时数据表来存储记录的值

本文关键字:数据表 存储 记录 一个 创建 | 更新日期: 2023-09-27 18:12:33

需要帮助的数据集我正在工作,我有一个数据集与2个表一个只持有主键值,这是"Statementnumber"和另一个持有一对多关系的其余细节

现在我想解析第一个表中的每个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();
                    }
                }




        }
    }
}

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

我的代码工作得很好,如果我只是把整个数据集作为源给crytalreport.setdatasrouce

,但它使一个长PDF文件,而不是小的单个语句文件。

创建一个临时数据表来存储记录的值

[已替换]如果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报表中的分组,您可以通过为语句号创建参数并将其传递到报表本身来实现所需的功能,并让它为您进行过滤。我不知道细节,但我知道这是可能的。