我正在從sql server獲取季度記錄。記錄就像每年每季度的總借記和貸記。在我的SQL查詢中,我創建了宿舍,並且年份是動態的,正如您在代碼中看到的那樣。無法將日期從字符串轉換爲
public List<QuarterlyExpenseList> GetExpenseDataQuarterly(string Id, string Year)
{
string SQL = "select aspnetusers.username, SUM(case when Expense.Type='credit' and (Expense.Date>='@year-01-01' and Expense.Date<='@year-03-31') then Expense.Amount else 0 end) as QuarterOneCredits,";
SQL += " SUM(case when Expense.Type='credit' and (Expense.Date>='@year-04-01' and Expense.Date<='@year-06-30') then Expense.Amount else 0 end) as QuarterTwoCredits,";
SQL += " SUM(case when Expense.Type='credit' and (Expense.Date>='@year-07-01' and Expense.Date<='@year-09-30') then Expense.Amount else 0 end) as QuarterThreeCredits,";
SQL += " SUM(case when Expense.Type='credit' and (Expense.Date>='@year-10-01' and Expense.Date<='@year-12-31') then Expense.Amount else 0 end) as QuarterFourCredits,";
SQL += " SUM(case when Expense.Type='debit' and (Expense.Date>='@year-01-01' and Expense.Date<='@year-03-31') then Expense.Amount else 0 end) as QuarterOneDebits,";
SQL += " SUM(case when Expense.Type='debit' and (Expense.Date>='@year-04-01' and Expense.Date<='@year-06-30') then Expense.Amount else 0 end) as QuarterTwoDebits,";
SQL += " SUM(case when Expense.Type='debit' and (Expense.Date>='@year-07-01' and Expense.Date<='@year-09-30') then Expense.Amount else 0 end) as QuarterThreeDebits,";
SQL += " SUM(case when Expense.Type='debit' and (Expense.Date>='@year-10-01' and Expense.Date<='@year-12-31') then Expense.Amount else 0 end) as QuarterFourDebits";
SQL += " from Expense inner join AspNetUsers on AspNetUsers.Id=Expense.MadeBy";
if (Id == null)
{
SQL += " group by aspnetusers.username";
}
else
{
SQL += " where [email protected] group by AspNetUsers.UserName group by aspnetusers.username";
}
using (IDbConnection cn=Connection)
{
cn.Open();
List<QuarterlyExpenseList> objList = cn.Query<QuarterlyExpenseList>(SQL, new { year = Year, id = Id }).ToList();
return objList;
}
}
在這裏我得到的Conversion failed when converting date and/or time from character string.
異常有誰能告訴我這是什麼意思,我怎樣才能擺脫它
創建SQL查詢時,你爲什麼不添加變量? (Expense.Type ='credit'和(Expense.Date> ='「+ Year +」-04-01「和Expense.Date <='」+ Year +「-06- 30')然後Expense.Amount else 0 end)作爲QuarterTwoCredits,「;' – Slasko