簡體   English   中英

未提供的參數化查詢。 實體框架

[英]The parameterized query which was not supplied. Entity Framework

我想使用 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; }
}

我該如何解決這個錯誤?

您不能將 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};

請注意使用此方法的最佳實踐,該方法指定確切的數據類型,而不是依賴於自動檢測。

包含@符號也是最佳實踐。

我已經創建了擴展方法來處理這個問題,這里是intint?擴展方法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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM