1

我所做的只是在表单中创建一个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:\IS\Testoooooooo\Testoooooooo\Dataset1.xsd"); ds.WriteXml(@"G:\IS\Testoooooooo\Testoooooooo\Dataset1.xml"); 如果可能的话,我需要一个从创造每一件事到编码的步骤,这将是很棒的。

4

3 回答 3

1

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

于 2013-07-05T18:25:34.600 回答
1

微软在这里有一个很好的演练......

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

于 2014-06-10T16:35:33.443 回答
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 参数的存储过程。cmd.CommandType = CommandType.Text如果将这些参数与 sql 命令一起传递,则必须设置。但是,如果您不想传递参数,只需将 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
于 2013-11-07T09:10:54.913 回答