Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未提供的参数化查询。实体框架_C#_Entity Framework_Ado.net - Fatal编程技术网

C# 未提供的参数化查询。实体框架

C# 未提供的参数化查询。实体框架,c#,entity-framework,ado.net,C#,Entity Framework,Ado.net,我想使用SQL查询将多个参数传递给存储过程 但在执行此操作时,如果任何参数为null,则会出现此错误 System.Data.SqlClient.SqlException:'参数化查询'(@p_Employee_Code nvarchar(4000),@p_Administration_Code nvarchar'需要未提供的参数'@p_Employee_Code' 这是我向存储过程传递参数的代码 int? Employee_Code_As_Int = string.IsNullOrEmpty(

我想使用SQL查询将多个参数传递给存储过程

但在执行此操作时,如果任何参数为null,则会出现此错误

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; }
}
公共部分类SR1\u结果
{
public int EmployeeCode{get;set;}
公共字符串EmployeeName{get;set;}
公共字符串JobName{get;set;}
公共可空日期{get;set;}
公共可为空的Nat_Salary{get;set;}
公共字符串管理名称{get;set;}
公共字符串DepartmentName{get;set;}
公共字符串BranchName{get;set;}
}
如何修复此错误?

您不能将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");
您不能将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");

我正在努力使这种方法及其工作良好

创建类以将
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));

我正在努力使这种方法及其工作良好

创建类以将
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));

这是一个不同的问题,请随意问另一个。这是一个工作感谢u pro…但为什么当所有参数都为null时,查询返回null…应该返回表中的所有数据???@ahmedabdo这是一个不同的问题。这是一个工作感谢u pro…但为什么当所有参数都为null时,查询返回null…应该返回表中的所有数据???@ahmedabdo这是一个问题不同的问题,请随意提问。