在我的 ASP.Net Core-6 Web API 中,我在 ADO.NET Core 中實作 SqlClient。我想按入職日期選擇員工。
我有這個物體(表):
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; }
}
然后我在 SQL Server DB 中創建了這個存盤程序:
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 在一系列選定的雇傭日期之間對員工進行后臺處理。我寫了這段代碼:
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;
}
如何修改上面的代碼以在存盤程序中包含就業日期的開始日期和結束日期?
uj5u.com熱心網友回復:
你可以使用:
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();
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/511729.html
