0

我在我的 ASP.NET Web 表单应用程序中创建了一个本地 .rdlc 报告。

我想使用 ReportViewer 控件加载报告。我正在使用数据库中的存储过程获取报告数据集的数据。存储过程需要一个参数来从表中返回单个数据行。我希望能够以编程方式将此参数传递给存储过程,以便报表加载数据。

我该如何做到这一点?

4

2 回答 2

2

您在 ASPX 上的 reportViewer

<rsweb:ReportViewer ID="ReportViewer1" runat="server" SizeToReportContent="True">
</rsweb:ReportViewer>

获取数据集的方法

private DataTable GetSPResult()
{
    DataTable ResultsTable = new DataTable();

    SqlConnection conn = new SqlConnection(ConfigurationManager
      .ConnectionStrings["connectionString"].ConnectionString);

    try
    {
        SqlCommand cmd = new SqlCommand("yourStorePorcedure", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id", 1);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        adapter.Fill(ResultsTable);
    }

    catch (Exception ex)
    {
        Response.Write(ex.ToString());
    }
    finally
    {
        if (conn != null)
        {
            conn.Close();
        }
    }

    return ResultsTable;
}

将结果绑定到报表查看器

DataTable dt = GetSPResult();
ReportViewer1.Visible = true;
ReportViewer1.LocalReport.ReportPath = "Report1.rdlc";
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", dt));
于 2013-09-14T06:33:16.960 回答
0
 DataTable dtval = new DataTable();

//添加参数

 SqlParameter[] param = new SqlParameter[5];
 if (!String.IsNullOrEmpty(ddlcust.SelectedValue))
 { param[0] = new SqlParameter("@custnam", ddlcust.SelectedValue); }
 else { param[0] = new SqlParameter("@custnam", DBNull.Value); }
 if (!string.IsNullOrEmpty(ddlproj.SelectedValue))
 { param[1] = new SqlParameter("@projid", Convert.ToInt32(ddlproj.SelectedValue)); }
 else { param[1] = new SqlParameter("@projid", DBNull.Value); }
 if (!string.IsNullOrEmpty(ddlvisittype.SelectedItem.Text))
 { param[2] = new SqlParameter("@vtype", ddlvisittype.SelectedItem.Text); }
 else { param[2] = new SqlParameter("@vtype", DBNull.Value); }
 param[3] = new SqlParameter("@frmdt", txtfrmdate.Text);
 param[4] = new SqlParameter("@todt", txttodate.Text);
 //Call storedprocedure with parameter
 dtval = objDal.Getvalue("[Usp_CRMRpt]", param);
 if (dtval != null && dtval.Rows.Count > 0)
 {

//加载到reportviewer

 RptCrm.Visible = true;
 RptCrm.LocalReport.DataSources.Clear();
 ReportDataSource rds = new ReportDataSource("dsCrmrpt", dtval);
 RptCrm.LocalReport.ReportPath = "Report1.rdlc";
 RptCrm.LocalReport.DataSources.Add(rds);
 RptCrm.DataBind();
 RptCrm.LocalReport.Refresh();
 }
 else
 { RptCrm.Visible = false; }
于 2013-09-14T04:54:14.457 回答