0

我正在使用 MVC 4 和实体框架来开发 Web 应用程序。我有一个基于存储过程的报告功能。这是我如何获取报告:

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;

            if (parameters != null)
            {
                foreach (var parameter in parameters)
                {
                    cmd.Parameters.Add(parameter);
                }
            }

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

    return ds;
}

public static byte[] GenerateReport(string templatePath, string storedProcedure, string extension, IEnumerable<SqlParameter> storedProcedureParameters)
{
    BuSIMaterialEntities db = new BuSIMaterialEntities();

    ReportViewer personReportViewer = new ReportViewer();

    //Parameters

    ReportParameter[] reportParameters = new ReportParameter[3];
    reportParameters[0] = new ReportParameter("Title", "Reporting", true);
    reportParameters[1] = new ReportParameter("Address", "Avenue de Tervuren,  268", true);
    reportParameters[2] = new ReportParameter("PostalCode", "B-1150 Brussels", true);

    //Report Template

    personReportViewer.ProcessingMode = ProcessingMode.Local;
    personReportViewer.LocalReport.ReportPath = templatePath;

    //Source

    DataSet ds = BuSIMaterial.Utils.Services.ExecuteStoredProcedure(db, storedProcedure, storedProcedureParameters);

    ReportDataSource dsList = new ReportDataSource("TableData", ds.Tables[0]);

    personReportViewer.LocalReport.DataSources.Clear();
    personReportViewer.LocalReport.DataSources.Add(dsList);

    //Report type

    personReportViewer.LocalReport.SetParameters(reportParameters);
    byte[] byteArray = personReportViewer.LocalReport.Render(extension);

    return byteArray;

}

当我不必使用参数时,一切正常:

public ActionResult PersonReport()
{
    byte[] byteArray = BuSIMaterial.Utils.Services.GenerateReport((HttpContext.Server.MapPath("..") + "\\Reporting\\Templates" + "AllPersonsReport.rdlc"), "GetAllPersons", "PDF", null); 

    Response.ClearHeaders();
    Response.AddHeader("Content-Disposition", "attachment; Filename=\"" + DateTime.Now + " - BuSI's Employees" + "\"");
    Response.AddHeader("Content-Transfer-Encoding", "Binary");
    Response.BinaryWrite((byte[])byteArray);
    Response.Flush();
    Response.End();

    return RedirectToAction("Index");
}

问题是,我有一个需要参数的存储过程:

ALTER PROCEDURE dbo.GetPersonDetails

    (
    @Id_Person int
    )

AS
    BEGIN
        SET NOCOUNT ON 

        SELECT p.FirstName,
        p.LastName,
        p.NumNat,
        pc.PhoneNumber,
        pc.SimNumber,
        vfc.Number,
        vfcc.Name,
        pa.StartDate,
        pa.EndDate,
        pr.PurchaseDate,
        pr.CatalogPrice,
        (prc.Name + ' - ' + prt.Model + ' ' + pr.SerialNumber) AS Allocation

        FROM bm_Persons AS p 

        INNER JOIN bm_PhoneCards AS pc ON p.Id_Person = pc.Id_Person
        INNER JOIN bm_VehicleFuelCards AS vfc ON vfc.Id_Person = p.Id_Person
        INNER JOIN bm_VehicleFuelCardCompanies AS vfcc ON vfcc.Id_VehicleFuelCardCompany = vfc.Id_VehicleFuelCardCompany
        INNER JOIN bm_ProductAllocations AS pa ON pa.Id_Person = p.Id_Person
        INNER JOIN bm_Products AS pr ON pr.Id_Product = pa.Id_Product
        INNER JOIN bm_ProductType AS prt ON prt.Id_ProductType = pr.Id_ProductType
        INNER JOIN bm_ProductCompanies AS prc ON prc.Id_ProductCompany = prt.Id_ProductCompany 

        WHERE p.Id_Person = @Id_Person;
    END

我想做的是创建一个方法(例如上面),但不是为 SqlParameters 参数传递 null,而是提供一个 id。我该怎么做?

4

2 回答 2

2

send Id_Person as sql parameter, give id value as int value

byte[] byteArray = BuSIMaterial.Utils.Services.GenerateReport(
 (HttpContext.Server.MapPath("..") + 
"\\Reporting\\Templates" + "AllPersonsReport.rdlc"), 
"GetAllPersons", "PDF", new[] {new SqlParameter("Id_Person", id)}); 
于 2013-04-29T08:24:52.510 回答
1

您可以按如下方式创建 SqlParameter。

SqlParameter idParameter = new SqlParameter("@id", SqlDbType.Int, 4);

idParameter.Value = 123;

IEnumerable<SqlParameter> myParameters = new[] { idParameter };

byte[] byteArray = BuSIMaterial.Utils.Services.GenerateReport((HttpContext.Server.MapPath("..") + "\\Reporting\\Templates" + "AllPersonsReport.rdlc"), "GetAllPersons", "PDF", myParameters);
于 2013-04-29T08:33:37.550 回答