【问题标题】:Creating Dataset Dynamically and passing to ReportViewer动态创建数据集并传递给 ReportViewer
【发布时间】:2013-06-25 12:34:01
【问题描述】:

我所做的只是在表单中创建一个reportViewer,然后我有这个代码:

        SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=G:\I.S\C#\billingSystem\Store.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");


        private void Form1_Load()
        {
            runRptViewer();
            cn.Open();
        }

        private void rptGetDataset()
        {
            DataSet ds = new DataSet();
            ds.DataSetName = "dsNewDataSet";
            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM NewBill", cn);
            ds.GetXmlSchema();
            da.Fill(ds);
            ds.WriteXmlSchema(@"G:\I.S\Testoooooooo\Testoooooooo\Dataset1.xsd");
            ds.WriteXml(@"G:\I.S\Testoooooooo\Testoooooooo\Dataset1.xml");
        }

        private DataTable getData()
        {
            DataSet dss = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM NewBill", cn);
            da.Fill(dss);
            DataTable dt = dss.Tables["NewBill"];
            return dt;
        }

        private void runRptViewer()
        {
            this.reportViewer2.Reset();
            //this.ReportViewer1.LocalReport.ReportPath = Server.MapPath("Report.rdlc");
            this.reportViewer2.LocalReport.ReportPath =(@"G:\I.S\Testoooooooo\Testoooooooo\Report1.rdlc");
            ReportDataSource rds = new ReportDataSource("dsNewDataSet_NewBill", getData());
            this.reportViewer2.LocalReport.DataSources.Clear();
            this.reportViewer2.LocalReport.DataSources.Add(rds);
            //this.reportViewer2.DataBind();
            this.reportViewer2.LocalReport.Refresh();
        }
    }

我有两个reportViewer,reportViewer1 可以工作,但如果数据库的目录发生变化,它将无法工作,这就是为什么我尝试使用另一个reportViewer,即使数据库的目录发生变化也能使其工作,我可以更改连接字符串。

问题是报告没有显示任何内容,我认为代码中的问题:

//this.ReportViewer1.LocalReport.ReportPath = Server.MapPath("Report.rdlc");

这是一个windows窗体所以没有服务器,我把它改成:

this.reportViewer2.LocalReport.ReportPath =(@"G:\I.S\Testoooooooo\Testoooooooo\Report1.rdlc");

而这个不起作用:

//this.reportViewer2.DataBind();

我无法理解这两行,是要创建 Dataset1.xsd 和 Dataset1.xml,还是只是编辑它们。 ds.WriteXmlSchema(@"G:\I.S\Testoooooooo\Testoooooooo\Dataset1.xsd"); ds.WriteXml(@"G:\I.S\Testoooooooo\Testoooooooo\Dataset1.xml"); 如果可能的话,我需要一个从创造每件事到编码的步骤,这将是很棒的。

【问题讨论】:

    标签: c# .net sql-server ado.net reportviewer


    【解决方案1】:

    您是如何创建数据集的?是 SQL 还是来自内存中的对象? 如果您尚未创建数据集,则需要先创建一个数据集,然后报表才能正常工作。这可能会有所帮助:http://shrutikapoor-ubc.blogspot.com/2013/05/using-business-objects-in-report-viewer.html

    【讨论】:

      【解决方案2】:
      【解决方案3】:

      要在 C# WinForm 项目中使用报表查看器,请将以下代码添加到按钮或 form_load 中:

      string strConnectionString = "Data Source=(local);Initial Catalog=Projects_DB;Integrated Security=True";
      DataSet ds = new DataSet();
      SqlDataAdapter da = new SqlDataAdapter();
      SqlCommand cmd = new SqlCommand("sp_GetProject " + "'0660CAD6-6F1A-4D19-A1FD-17BF3655AC98'");
      cmd.CommandType = CommandType.Text;
      cmd.Connection = new SqlConnection (strConnectionString);
      da.SelectCommand = cmd;
      
      da.Fill(ds,"DataSet1");
      
      reportViewer1.ProcessingMode = ProcessingMode.Local;
      reportViewer1.LocalReport.DataSources.Clear();
      reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", ds.Tables[0]));
      reportViewer1.LocalReport.Refresh();
      reportViewer1.RefreshReport();
      

      假设您有一个接受@ProjectID 参数的存储过程。如果将这些参数与 sql 命令一起传递,则必须设置 cmd.CommandType = CommandType.Text。但是,如果您不想传递参数,只需将 commandType 更改为 cmd.CommandType = CommandType.StoredProcedure。

      以下是本例中使用的存储过程:

      CREATE PROC [dbo].[sp_GetProject]
          @ProjectID      nvarchar(50)=NULL
      
      AS
      BEGIN
      
         SELECT ProjectID, ProjectName FROM Projects
          WHERE 
          (@ProjectID IS NULL) OR (ProjectID = @ProjectID)
      
      END
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-09
        • 2018-09-02
        • 1970-01-01
        • 2023-03-06
        • 1970-01-01
        • 2020-02-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多