C# MVC4-使用SqlParameters的报告和存储过程
我正在使用MVC4和实体框架开发一个web应用程序。我有一个基于存储过程的报告功能。以下是获取报告的方法:C# MVC4-使用SqlParameters的报告和存储过程,c#,sql,sql-server,asp.net-mvc,reporting-services,C#,Sql,Sql Server,Asp.net Mvc,Reporting Services,我正在使用MVC4和实体框架开发一个web应用程序。我有一个基于存储过程的报告功能。以下是获取报告的方法: public static DataSet ExecuteStoredProcedure(ObjectContext db, string storedProcedureName, IEnumerable<SqlParameter> parameters) { var connectionString = ((EntityConnection)db.Co
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;
}
问题是,我有一个需要参数的存储过程:
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。我应该如何做呢?发送
id\u 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)});
发送
Id\u 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)});
您可以创建一个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);
SqlParameter idParameter=newsqlparameter(“@id”,SqlDbType.Int,4);
idParameter.Value=123;
IEnumerable myParameters=new[]{idParameter};
byte[]byteArray=BuSIMaterial.Utils.Services.GenerateReport((HttpContext.Server.MapPath(“…”)+“\\Reporting\\Templates”+“AllPersonsReport.rdlc”),“GetAllPersons”,“PDF”,myParameters);
您可以按如下方式创建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);
SqlParameter idParameter=newsqlparameter(“@id”,SqlDbType.Int,4);
idParameter.Value=123;
IEnumerable myParameters=new[]{idParameter};
byte[]byteArray=BuSIMaterial.Utils.Services.GenerateReport((HttpContext.Server.MapPath(“…”)+“\\Reporting\\Templates”+“AllPersonsReport.rdlc”),“GetAllPersons”,“PDF”,myParameters);