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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17297476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:04:35  来源:igfitidea点击:

Creating Dataset Dynamically and passing to ReportViewer

c#.netsql-serverado.netreportviewer

提问by Maged E William

all i did is just create a reportViewer in the form, then i have this code:

我所做的只是在表单中创建一个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();
        }
    }

i have two reportViewer the reportViewer1 work but in case the directory of the db has change it will not work, so thats why i try in another reportViewer, to make it work even if the directory of the db changed, i can just change the Connection string.

我有两个 reportViewer,reportViewer1 可以工作,但是如果 db 的目录发生变化,它将无法工作,所以这就是为什么我尝试在另一个 reportViewer 中工作,即使 db 的目录发生变化,我也可以更改连接细绳。

The problem is the report don't show anything, i think the problem in the code:

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

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

this is a windows form so there is no server, ive change it to:

这是一个 Windows 窗体,因此没有服务器,我将其更改为:

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

and this one dont work:

而这个不起作用:

//this.reportViewer2.DataBind();

i cant understand this two lines, does it mean to create a Dataset1.xsd and Dataset1.xml, or just edit them. ds.WriteXmlSchema(@"G:\I.S\Testoooooooo\Testoooooooo\Dataset1.xsd"); ds.WriteXml(@"G:\I.S\Testoooooooo\Testoooooooo\Dataset1.xml"); if possible i need a steps from creating every thing to codding that will be great.

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

采纳答案by Shruti Kapoor

How did you create your dataset? Is it SQL or from Objects in memory? If you havent created a dataset, you do need to create one before the report can work properly. This might help: http://shrutikapoor-ubc.blogspot.com/2013/05/using-business-objects-in-report-viewer.html

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

回答by A Ghazal

To use the report viewer in your C# WinForm project add the following code to a button or inthe form_load:

要在 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();

Assuming that you have a stored procedure that accepts @ProjectID parameter. You have to set the cmd.CommandType = CommandType.Textif you pass these parameters along with the sql command. However, if you don't want to pass parameters just change the commandType to cmd.CommandType = CommandType.StoredProcedure.

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

Below is the stored procedure used in this example:

下面是这个例子中使用的存储过程:

CREATE PROC [dbo].[sp_GetProject]
    @ProjectID      nvarchar(50)=NULL

AS
BEGIN

   SELECT ProjectID, ProjectName FROM Projects
    WHERE 
    (@ProjectID IS NULL) OR (ProjectID = @ProjectID)

END