2013-04-29 101 views
0

我正在使用MVC 4和Entity Framework開發Web應用程序。我有一個基於存儲過程的報告功能。這是我要怎麼做才能得到一個報告:MVC 4 - 使用SqlParameters進行報告和存儲過程

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參數,我想提供一個ID。我應該怎麼做?

回答

2

發送Id_Person作爲SQL參數,給出的值id爲int值

byte[] byteArray = BuSIMaterial.Utils.Services.GenerateReport(
(HttpContext.Server.MapPath("..") + 
"\\Reporting\\Templates" + "AllPersonsReport.rdlc"), 
"GetAllPersons", "PDF", new[] {new SqlParameter("Id_Person", id)}); 
+0

感謝您的建議 – Traffy 2013-04-29 08:36:35

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); 
+0

感謝您的幫助! – Traffy 2013-04-29 08:38:41

相關問題