首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何利用EmploymentDate实现ADO.NET核心SqlClient对员工的过滤

如何利用EmploymentDate实现ADO.NET核心SqlClient对员工的过滤
EN

Stack Overflow用户
提问于 2022-10-08 16:40:40
回答 1查看 40关注 0票数 0

在我的ASP.Net Core-6 Web中,我正在ADO.NET核心中实现SqlClient。我要按雇用日期选择员工。

我有一个实体(表):

代码语言:javascript
复制
public class Employee
{
    public int EmployeeId { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public string Email { get; set; }
    public string EmploymentDate { get; set; }
}

然后,我在Server中创建了这个存储过程:

代码语言:javascript
复制
CREATE PROCEDURE [dbo].[sp_employees]
    @pdStartDate datetime,
    @pdEndDate datetime
AS
    SELECT
         *
    FROM
        [Employees].[dbo].[employees] 
    WHERE
       EmployementDate BETWEEN @pdStartDate AND @pdEndDate

    RETURN 1

我希望使用ADO.NET Core SqlClient在选定雇用日期的范围内对员工进行假脱机。我写了这段代码:

代码语言:javascript
复制
public IEnumerable<Employee> GetEmployees()
{
    List<Employee> employeelist = new List<Employee>();
    using (con = new SqlConnection(connection))
    {
        con.Open();
        command = new SqlCommand("sp_employees", con);
        command.CommandType = CommandType.StoredProcedure;
        dataReader = command.ExecuteReader();
        while (dataReader.Read())
        {
            Employee employee = new Employee();
            employee.EmployeeId = Convert.ToInt32(dataReader["EmployeeId"]);
            employee.Firstname = dataReader["Firstname"].ToString();
            employee.Lastname = dataReader["Lastname"].ToString();
            employee.Email = dataReader["Email"].ToString();
      employee.EmploymentDate = Convert.ToDateTime(dataReader["EmploymentDate"].ToString());

            employeelist.Add(employee);
        }
        con.Close();
    }
    return employeelist;
}

如何修改上面的代码以将StartDate和EndDate的EmploymentDate包含在存储过程中?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-08 16:59:14

你可以用:

代码语言:javascript
复制
con.Open();
command = new SqlCommand("sp_employees", con);
command.CommandType = CommandType.StoredProcedure;
    
command.Parameters.AddWithValue("@pdStartDate", startDate);
command.Parameters.AddWithValue("@pdEndDate", endDate);
    
dataReader = command.ExecuteReader();
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73998758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档