【问题标题】:MVC 4 - Reporting Service and Stored ProceduresMVC 4 - 报告服务和存储过程
【发布时间】:2013-04-05 13:18:24
【问题描述】:

我正在使用 MVC4 和实体框架来开发 Intranet Web 应用程序,其中一项要求是该应用程序将能够创建报告。所以我正在使用报告服务来做到这一点。

在我的报告文件 (.rdlc) 中,我有一个表,它应该包含我在“人员”表中的所有记录。我创建了一个存储过程,它从我的数据库中获取所有人员以及一个允许下载报告的操作。

问题是,我不知道如何告诉我的操作我的数据源是我的存储过程。

这是我的行动:

public ActionResult PersonReport()
{

    ReportViewer personReportViewer = new ReportViewer();

    List<ReportParameter> reportParameters = new List<ReportParameter>();

    reportParameters.Add(new ReportParameter("Title", "Test"));

    personReportViewer.LocalReport.SetParameters(reportParameters);

    personReportViewer.ProcessingMode = ProcessingMode.Local;
    personReportViewer.LocalReport.ReportEmbeddedResource = 
       HttpContext.Server.MapPath(".") + "\\Reporting\\Templates\\"
        + "PersonReport.rdlc";

    byte[] byteArray = personReportViewer.LocalReport.Render("PDF");

    Response.ClearHeaders();
    Response.AddHeader("Content-Disposition", 
         "attachment; Filename=\"" + DateTime.Now + "_Title_" + "\"");
    Response.AddHeader("Content-Transfer-Encoding", "Binary");
    Response.BinaryWrite(byteArray);
    Response.Flush();
    Response.End();

    return RedirectToAction("Index");
}

这是我的存储过程:

ALTER PROCEDURE dbo.GetAllPersons

AS

BEGIN

    SET NOCOUNT ON

    SELECT * FROM  bm_Persons;

END

知道如何做到这一点吗?非常感谢。

【问题讨论】:

  • 您能解释一下“告诉我的操作我的数据源是我的存储过程”吗?因为这对我来说毫无意义?
  • 事实上,当我启动应用程序并单击与我的操作相关的链接时,它会返回一个错误,告诉我没有来源可以填充我的表格。

标签: c# sql asp.net-mvc stored-procedures reporting-services


【解决方案1】:

您需要运行该过程并将结果传递给 ReportViewer。
但目前无法回答是如何做到的。您是使用普通的旧 ADO.NET 连接到您的数据库、实体框架或 NHibernate 等...

您的存储过程将返回结果集合,因此我会将其分配给一个变量并将其传递给 ReportViewer。或者有一个方法 GetAllPersons 返回 ArrayIList&lt;T&gt; 或其他合适的类型,然后你可以在你的方法范围之外使用它。

鉴于您正在使用 EF,那么为了从您的存储过程中获取一组结果,您需要一个类似于我从 the answer I referred to in my comment 获取的方法:

public static DataSet ExecuteStoredProcedure(ObjectContext db, 
                                             string storedProcedureName, 
                                             IEnumerable<SqlParameter> parameters)
{
    var connectionString = 
        ((EntityConnection)db.Connection).StoreConnection.ConnectionString;
    var ds = new DataSet();

    using (var conn = new SqlConnection(connectionString))
    {
        using (var cmd = conn.CreateCommand())
        {
            cmd.CommandText = storedProcedureName;
            cmd.CommandType = CommandType.StoredProcedure;
            foreach (var parameter in parameters)
            {
                cmd.Parameters.Add(parameter);
            }

            using (var adapter = new SqlDataAdapter(cmd))
            {
                adapter.Fill(ds);
            }
        }
    }

    return ds;
}

【讨论】:

  • 通常你有一个 dbContext 可用,你可以用它来调用你的过程。有这个:msdn.microsoft.com/en-us/data/gg699321.aspx 这可能会有所帮助
  • 非常感谢您的回答!但是,在您的静态方法中,有一个参数我无法理解它的含义(第三个)。什么时候用这个方法,最后一个参数应该传什么?
  • 只需在 this 的情况下传递 null。参数是可以传递给存储过程的参数的集合。例如,在另一个过程中,您可能需要传递StartDateEndDate,在这种情况下,您将创建一个新的 IEnumerable SqlParamters 并将其传递给方法调用。由于您的程序没有任何输入参数,您可以传递 null
  • 好的,我想我明白你要解释的了。
【解决方案2】:

我觉得你需要设置ReportViewer1.LocalReport.DataSources:

SqlCommand cmd = new SqlCommand();

cmd.Parameters.Add(new SqlParameter("@StartDate", startDate)); 
cmd.Parameters.Add(new SqlParameter("@EndDate", endDate)); 

var thisConnectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString; 

SqlConnection thisConnection = new SqlConnection(thisConnectionString); 

cmd.Connection = thisConnection; 

cmd.CommandText = string.Format("[dbo].[StoredProcedureName]"); 
cmd.CommandType = CommandType.StoredProcedure; 
SqlDataAdapter da = new SqlDataAdapter(cmd); 

System.Data.DataSet thisDataSet = new System.Data.DataSet(); 

da.Fill(thisDataSet); 


////////////
/// HERE ///
////////////
ReportDataSource datasource = new ReportDataSource(rptName, thisDataSet.Tables[0]); 

ReportViewer1.LocalReport.DataSources.Clear(); 
ReportViewer1.LocalReport.DataSources.Add(datasource); 

....................

ReportViewer1.LocalReport.Refresh();

【讨论】:

  • 感谢您的回答。无论如何,当我尝试像你一样为`thisConnectionString做事时,我遇到了一个问题,有一个错误,VS建议我生成属性存根。
  • 已修复。它应该是字符串 - 只是到您的数据库的连接字符串。
  • 似乎以下问题描述了类似的问题:stackoverflow.com/questions/8802707/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-03
  • 1970-01-01
  • 2012-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多