我需要使用2个日期作为参数从C#调用存储过程。我做了很多次,但是从来没有使用日期,并且它不起作用,参数无法解释。
我的约会日期类似于“ 2018-01-30”
我的C#函数:
private DataTable getAccidents ( DateTime dateDebut, DateTime dateFin)
{
DataTable dt = new DataTable();
string connectionString = ConfigurationManager.ConnectionStrings["ATPConnectionString"].ConnectionString;
using (SqlConnection cn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("getAccidents", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@p_anneeDebut", SqlDbType.DateTime).Value = dateDebut;
cmd.Parameters.Add("@p_anneeFin", SqlDbType.DateTime).Value = dateFin;
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
}
return dt;
}
我在SQL Server中的存储过程如下:
ALTER PROCEDURE [dbo].[getAccidents]
(@p_anneeDebut DATETIME,
@p_anneeFin DATETIME)
AS
SELECT *
FROM T_ACCIDENT
LEFT OUTER JOIN TR_SALARIE AS victime ON T_ACCIDENT.SAL_idVictime =
victime.SAL_id
...
LEFT OUTER JOIN ERP.dbo.TR_COST_CENTER as cost_center ON cost_center.COS_id = T_ACCIDENT.ACC_lieuPrecis
WHERE
CONVERT(DATETIME, ACC_date) BETWEEN @p_anneeDebut AND @p_anneeFin
有关信息,ACC_date
是一个字符串列,因此我将其转换为DATETIME
。当我在SQL Server中调用不带参数但日期类似“ BETWEEN'2017-01-01'和'2018-01-01'”的存储过程时,它将起作用