动态创建数据集并传递给ReportViewer

本文关键字:ReportViewer 创建 数据集 动态 | 更新日期: 2023-09-27 18:20:00

我所做的只是在表单中创建一个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''TestooOOoo''Dataset1.xml");如果可能的话,我需要一个步骤,从创造每一件事到结束,这将是伟大的。

动态创建数据集并传递给ReportViewer

您是如何创建数据集的?它是SQL还是来自内存中的对象?如果您还没有创建数据集,那么在报表正常工作之前,您确实需要创建一个数据集。这可能会有所帮助:http://shrutikapoor-ubc.blogspot.com/2013/05/using-business-objects-in-report-viewer.html

要在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

Microsoft在这里有一个非常好的演练。。。

http://blogs.msdn.com/b/sqlforum/archive/2011/04/28/sql-reporting-services-ssrs-bind-dynamic-dataset-to-your-local-report-with-reportviewer.aspx