未提供的参数化查询。实体框架
Posted
技术标签:
【中文标题】未提供的参数化查询。实体框架【英文标题】:The parameterized query which was not supplied. Entity Framework 【发布时间】:2020-09-14 22:56:29 【问题描述】:我想使用 SQL 查询将多个参数传递给存储过程。
但是这样做时,如果任何参数为空,我会收到此错误
System.Data.SqlClient.SqlException: '参数化查询'(@P_Employee_Code nvarchar(4000),@P_Administration_Code nvarchar' 需要参数'@P_Employee_Code',但未提供。'
这是我将参数传递给我的存储过程的代码
int? Employee_Code_As_Int = string.IsNullOrEmpty(Employee_Code) ? null : (int?)Convert.ToInt32(Employee_Code);
int? Administration_Code_As_Int = string.IsNullOrEmpty(Administration_Code) ? null : (int?)Convert.ToInt32(Administration_Code);
int? Department_Code_As_Int = string.IsNullOrEmpty(Department_Code) ? null : (int?)Convert.ToInt32(Department_Code);
int? Job_Code_As_Int = string.IsNullOrEmpty(Job_Code) ? null : (int?)Convert.ToInt32(Job_Code);
int? Branch_Code_As_Int = string.IsNullOrEmpty(Branch_Code) ? null : (int?)Convert.ToInt32(Branch_Code);
int? Level_Job_Code_As_Int = string.IsNullOrEmpty(Level_Job_Code) ? null : (int?)Convert.ToInt32(Level_Job_Code);
int? Type_Of_Workers_Code_As_Int = string.IsNullOrEmpty(Type_Of_Workers_Code) ? null : (int?)Convert.ToInt32(Type_Of_Workers_Code);
int? RelationShip_Code_As_Int = string.IsNullOrEmpty(RelationShip_Code) ? null : (int?)Convert.ToInt32(RelationShip_Code);
int? Vacation_Calculate_Type_Code_As_Int = string.IsNullOrEmpty(Vacation_Calculate_Type_Code) ? null : (int?)Convert.ToInt32(Vacation_Calculate_Type_Code);
int? ConCostmor_Code_As_Int = string.IsNullOrEmpty(ConCostmor_Code) ? null : (int?)Convert.ToInt32(ConCostmor_Code);
int? Insurance_Type_Code_As_Int = string.IsNullOrEmpty(Insurance_Type_Code) ? null : (int?)Convert.ToInt32(Insurance_Type_Code);
int? Nationality_Code_As_Int = string.IsNullOrEmpty(Nationality_Code) ? null : (int?)Convert.ToInt32(Nationality_Code);
int? Gender_Code_As_Int = string.IsNullOrEmpty(Gender_Code) ? null : (int?)Convert.ToInt32(Gender_Code);
DateTime? Date_Hiring_From_As_DateTime = Date_Hiring_From == string.Empty ? null : (DateTime?)Convert.ToDateTime(Date_Hiring_From);
DateTime? Date_Hiring_To_As_DateTime = Date_Hiring_to == string.Empty ? null : (DateTime?)Convert.ToDateTime(Date_Hiring_to);
//
//
SqlParameter P_Employee_Code = new SqlParameter("P_Employee_Code", Employee_Code_As_Int);
SqlParameter P_Administration_Code = new SqlParameter("P_Administration_Code", Administration_Code_As_Int);
SqlParameter P_Department_Code = new SqlParameter("P_Department_Code", Department_Code_As_Int);
SqlParameter P_Job_Code = new SqlParameter("P_Job_Code", Job_Code_As_Int);
SqlParameter P_Branch_Code = new SqlParameter("P_Branch_Code", Branch_Code_As_Int);
SqlParameter P_Level_Job_Code = new SqlParameter("P_Level_Job_Code", Level_Job_Code_As_Int);
SqlParameter P_Type_Of_Worker_Code = new SqlParameter("P_Type_Of_Worker_Code", Type_Of_Workers_Code_As_Int);
SqlParameter P_RelationShip_Code = new SqlParameter("P_RelationShip_Code", RelationShip_Code_As_Int);
SqlParameter P_Vacation_Calcualtion_Code = new SqlParameter("P_Vacation_Calcualtion_Code", Vacation_Calculate_Type_Code_As_Int);
SqlParameter P_ConCustmer_Code = new SqlParameter("P_ConCustmer_Code", ConCostmor_Code_As_Int);
SqlParameter P_Insurance_Type_Code = new SqlParameter("P_Insurance_Type_Code", Insurance_Type_Code_As_Int);
SqlParameter P_Nationality_Code = new SqlParameter("P_Nationality_Code", Nationality_Code_As_Int);
SqlParameter P_Gender_Code = new SqlParameter("P_Gender_Code", Gender_Code_As_Int);
SqlParameter P_Date_Hiring_From = new SqlParameter("P_Date_Hiring_From", Date_Hiring_From_As_DateTime);
SqlParameter P_Date_Hiring_To = new SqlParameter("P_Date_Hiring_To", Date_Hiring_To_As_DateTime);
BindingSource bs = new BindingSource();
//
// R1
if (Report_Number == "1")
var Employee_Data = db.Database.SqlQuery<SR1_Result>("EXEC SR1 @P_Employee_Code," +
"@P_Administration_Code , @P_Department_Code ," +
"@P_Job_Code , @P_Branch_Code , " +
"@P_Level_Job_Code,"+
"@P_Type_Of_Worker_Code, @P_RelationShip_Code,"+
"@P_Vacation_Calculation_Code, @P_ConCustmer_Code,"+
"@P_Insurance_Type_Code , @P_Nationality_Code,"+
"@P_Gender_Code, @P_Date_Hiring_From,@P_Date_Hiring_To" ,
P_Employee_Code , P_Administration_Code, P_Department_Code ,
P_Job_Code, P_Branch_Code, P_Level_Job_Code, P_Type_Of_Worker_Code,
P_RelationShip_Code, P_Vacation_Calculation_Code, P_ConCustomer_Code,
P_Insurance_Type_Code, P_Nationality_Code, P_Gender_Code,
P_Date_Hiring_From, P_Date_Hiring_To)
.Select(u => new
u.EmployeeCode,
u.EmployeeName,
u.JobName,
u.Date_Hiring,
u.AdministrationName,
u.DepartmentName,
u.BranchName,
)
.ToList();
bs.DataSource = Employee_Data;
这是我的存储过程
ALTER PROCEDURE [dbo].[SR1]
@P_Employee_Code int = NULL,
@P_Administration_Code tinyint = NULL,
@P_Department_Code tinyint = NULL,
@P_Jop_Code smallint = NULL,
@P_Pranch_Code tinyint = NULL,
@P_Level_Jop_Code tinyint = NULL,
@P_Type_Of_Worker_Code tinyint = NULL,
@P_RelationShip_Code tinyint = NULL,
@P_Vacation_Calcualtion_Code tinyint = NULL,
@P_ConCustmer_Code tinyint = NULL,
@P_Insurance_Type_Code tinyint = NULL,
@P_Nationality_Code tinyint = NULL,
@P_Gender_Code tinyint = NULL,
@P_Date_Hiring_From datetime = NULL,
@P_Date_Hiring_To datetime = NULL
AS
BEGIN
SELECT
EmployeeCode,EmployeeName,
JobName,
Date_Hiring,
Nat_Salary,
AdministrationName,
DepartmentName,
BranchName
FROM
Employee_List_Code_Name_Jop_DateHiring s
WHERE
(s.EmployeeCode = @P_Employee_Code OR @P_Employee_Code IS NULL)
AND
(s.AdministrationCode = @P_Administration_Code OR @P_Administration_Code IS NULL)
AND
(s.DepartmentCode = @P_Department_Code OR @P_Department_Code IS NULL)
AND
(s.JobCode = @P_Job_Code OR @P_Job_Code IS NULL)
AND
(s.BranchCode = @P_Branch_Code OR @P_Branch_Code IS NULL)
AND
(s.JobLevelCode = @P_Level_Job_Code OR @P_Level_Job_Code IS NULL)
AND
(s.TypeOfWorkersCode = @P_Type_Of_Worker_Code OR @P_Type_Of_Worker_Code IS NULL)
AND
(s.RelationShipCode = @P_RelationShip_Code OR @P_RelationShip_Code IS NULL)
AND
(s.Vacation_Calculate_Type_Code = @P_Vacation_Calculation_Code OR @P_Vacation_Calculation_Code IS NULL)
AND
(@P_ConCustomer_Code IS NULL OR S.ConCustmerCode = @P_ConCustomer_Code)
AND
(@P_Insurance_Type_Code is null or S.Insurance_Code =@P_Insurance_Type_Code)
AND
(s.GenderCode=@P_Gender_Code or @P_Gender_Code is null)
AND
(s.NationalityCode=@P_Nationality_Code or @P_Nationality_Code is null)
AND
(@P_Date_Hiring_From is null or @P_Date_Hiring_To is null) or S.Date_Hiring BETWEEN @P_Date_Hiring_From AND @P_Date_Hiring_To
END
这是存储过程类
public partial class SR1_Result
public int EmployeeCode get; set;
public string EmployeeName get; set;
public string JobName get; set;
public Nullable<System.DateTime> Date_Hiring get; set;
public Nullable<double> Nat_Salary get; set;
public string AdministrationName get; set;
public string DepartmentName get; set;
public string BranchName get; set;
我该如何解决这个错误?
【问题讨论】:
【参考方案1】:您不能将 C# 值 null
传递给 SqlParameter
,您需要传递 System.DBNull.Value
。所以
SqlParameter P_Employee_Code = new SqlParameter("P_Employee_Code", Employee_Code_As_Int);
应该是
SqlParameter P_Employee_Code = new SqlParameter("@P_Employee_Code", SqlDbType.Int) Value = Employee_Code_As_Int ?? (object)System.DBNull.Value;
注意使用此方法的最佳实践是指定确切的数据类型,而不是依赖于自动检测。
包含@
符号也是最佳做法。
我已经创建了扩展方法来处理这个问题,这里是 int
和 int?
的扩展方法
public static SqlParameter ToSqlIntParameter(this int value, string name)
return new SqlParameter(name.IndexOf("@") > -1 ? name : "@" + name, SqlDbType.Int) Value = value ;
public static SqlParameter ToSqlIntParameter(this int? value, string name)
return new SqlParameter(name.IndexOf("@") > -1 ? name : "@" + name, SqlDbType.Int) Value = value ?? (object)DBNull.Value ;
因此,当使用这些代码时,您的代码将变为:
SqlParameter P_Employee_Code = Employee_Code_As_Int.ToSqlIntParameter("@P_Employee_Code");
【讨论】:
它的工作谢谢你亲......但是为什么当所有参数为空时,查询返回空......应该返回表中的所有数据?? @ahmedabdo 这是一个不同的问题,请随时问另一个问题。【参考方案2】:我制作了这个方法并且效果很好。
创建类以将null
值转换为DBNull.Value
。
public static object GetDataValue(object value)
if (value == null)
return DBNull.Value;
return value;
和SqlParameter
SqlParameter P_Employee_Code = new SqlParameter("@P_Employee_Code",GetDataValue(Employee_Code_As_Int));
【讨论】:
以上是关于未提供的参数化查询。实体框架的主要内容,如果未能解决你的问题,请参考以下文章