0

我正在开发一个c#需要生成报告的应用程序。我正在使用一个数据集,该数据集填充来自从stored procedureC# 代码中获取一个参数的数据。我正在创建一个参数report1.rdlc并使用文本框中的数据填充它。当我运行应用程序时,我在报表查看器上看不到任何内容。

public void GenerateBranchwiseReport()
        {
            conn.Open();
            SqlCommand BranchReportcmd = new SqlCommand("select [am/bsi name] from masterlookup where [asc type]='BRANCH' group by [am/bsi name]", conn);
            SqlDataReader BranchReportread = BranchReportcmd.ExecuteReader();
            while (BranchReportread.Read())
            {
                BranchManagerName.Add(BranchReportread.GetValue(0).ToString());
            }
            conn.Close();
            foreach (string managername in BranchManagerName)
            {
                conn.Open();
                SqlCommand GetReportDatacmd = new SqlCommand();
                GetReportDatacmd.CommandText = "USP_BranchwiseReport";
                GetReportDatacmd.CommandType = CommandType.StoredProcedure;
                GetReportDatacmd.Parameters.Add(new SqlParameter("@BranchManagerName", managername));
                GetReportDatacmd.Connection = conn;
                GetReportDatacmd.ExecuteNonQuery();
                SqlDataAdapter da = new SqlDataAdapter(GetReportDatacmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                conn.Close();
                reportViewer1.Reset();
                this.reportViewer1.Visible = true;
                string reportname = @"d:\users\administrator\documents\visual studio 2010\Projects\ReportwithParameter\ReportwithParameter\Report1.rdlc";
                this.reportViewer1.LocalReport.ReportPath = @"d:\users\administrator\documents\visual studio 2010\Projects\ReportwithParameter\ReportwithParameter\Report1.rdlc";
                ReportParameter[] param = new ReportParameter[1];
                param[0] = new ReportParameter("ManagerName", managername);
                this.reportViewer1.LocalReport.SetParameters(param);
                ReportDataSource ReportBranch = new ReportDataSource("DatasetWithParameter.USP_BranchwiseReport", ds.Tables[0]);
                this.reportViewer1.LocalReport.ReportEmbeddedResource = reportname;
                this.reportViewer1.LocalReport.DataSources.Clear();
                this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DatasetWithParameter.USP_BranchwiseReport", ds.Tables[0]));
                //this.reportViewer1.LocalReport.DataSources.Add(ReportBranch);
                this.reportViewer1.LocalReport.Refresh();
                //SendEmail();
            }
        }
4

2 回答 2

0

你为什么要遍历报告中的所有经理?可能您正在寻找包含所有经理数据的报告。

您正在为每个经理循环相同的报告???

public void GenerateBranchwiseReport()
{
string ManagerNames="";
    conn.Open();
    SqlCommand BranchReportcmd = new SqlCommand("select [am/bsi name] from masterlookup where [asc type]='BRANCH' group by [am/bsi name]", conn);
    SqlDataReader BranchReportread = BranchReportcmd.ExecuteReader();
    while (BranchReportread.Read())
    {
     if (ManagerNames.length==0)
         ManagerNames="'"+BranchReportread.GetValue(0).ToString()+"'";
     else
         ManagerNames =ManagerNames + ",'" + BranchReportread.GetValue(0).ToString()+ "'";

    }
    conn.Close();


  //So now, You have Multiple Managers in ManagerNames string You can send it to SQL, which must used in Where clause with IN (@Managers) in you Stored Procedure.

        conn.Open();
        SqlCommand GetReportDatacmd = new SqlCommand();
        GetReportDatacmd.CommandText = "USP_BranchwiseReport";
        GetReportDatacmd.CommandType = CommandType.StoredProcedure;
        GetReportDatacmd.Parameters.Add(new SqlParameter("@BranchManagerName", ManagerNames));
        GetReportDatacmd.Connection = conn;
        GetReportDatacmd.ExecuteNonQuery();
        SqlDataAdapter da = new SqlDataAdapter(GetReportDatacmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        conn.Close();
        reportViewer1.Reset();
        this.reportViewer1.Visible = true;
        string reportname = @"d:\users\administrator\documents\visual studio 2010\Projects\ReportwithParameter\ReportwithParameter\Report1.rdlc";
        this.reportViewer1.LocalReport.ReportPath = @"d:\users\administrator\documents\visual studio 2010\Projects\ReportwithParameter\ReportwithParameter\Report1.rdlc";
        ReportParameter[] param = new ReportParameter[1];
        param[0] = new ReportParameter("ManagerName", managername);
        this.reportViewer1.LocalReport.SetParameters(param);
        ReportDataSource ReportBranch = new ReportDataSource("DatasetWithParameter.USP_BranchwiseReport", ds.Tables[0]);
        this.reportViewer1.LocalReport.ReportEmbeddedResource = reportname;
        this.reportViewer1.LocalReport.DataSources.Clear();
        this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DatasetWithParameter.USP_BranchwiseReport", ds.Tables[0]));
        //this.reportViewer1.LocalReport.DataSources.Add(ReportBranch);
        this.reportViewer1.LocalReport.Refresh();
        //SendEmail();
于 2013-06-14T10:58:38.683 回答
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-07T07:08:09.207 回答