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

时间:2013-06-25 12:34:01

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

我所做的只是在表单中创建一个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工作,但万一db的目录已经改变它将无法正常工作,所以这就是为什么我尝试在另一个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”); 如果可能的话,我需要从创造所有东西到编码的步骤,这将是伟大的。

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

答案 2 :(得分:0)

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