2016-01-04 102 views
2

我正在從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.異常有誰能告訴我這是什麼意思,我怎樣才能擺脫它

+1

創建SQL查詢時,你爲什麼不添加變量? (Expense.Type ='credit'和(Expense.Date> ='「+ Year +」-04-01「和Expense.Date <='」+ Year +「-06- 30')然後Expense.Amount else 0 end)作爲QuarterTwoCredits,「;' – Slasko

回答

5

我刪除了動態年「@year」之外的字符串,因爲它是一個動態值。當寫成'@ year-04-01'時,SQL將「@year」視爲字符串而不是動態值。

使用下面的代碼:

public List<QuarterlyExpenseList> GetExpenseDataQuarterly(string Id, string Year) 
      { 
       string SQL = "select aspnetusers.username, SUM(case when Expense.Type='credit' and (Expense.Date>[email protected]+'-01-01' and Expense.Date<[email protected]+'-03-31') then Expense.Amount else 0 end) as QuarterOneCredits,"; 
       SQL += " SUM(case when Expense.Type='credit' and (Expense.Date>[email protected]+'-04-01' and Expense.Date<[email protected]+'-06-30') then Expense.Amount else 0 end) as QuarterTwoCredits,"; 
       SQL += " SUM(case when Expense.Type='credit' and (Expense.Date>[email protected]+'-07-01' and Expense.Date<[email protected]+'-09-30') then Expense.Amount else 0 end) as QuarterThreeCredits,"; 
       SQL += " SUM(case when Expense.Type='credit' and (Expense.Date>[email protected]+'-10-01' and Expense.Date<[email protected]+'-12-31') then Expense.Amount else 0 end) as QuarterFourCredits,"; 
       SQL += " SUM(case when Expense.Type='debit' and (Expense.Date>[email protected]+'-01-01' and Expense.Date<[email protected]+'-03-31') then Expense.Amount else 0 end) as QuarterOneDebits,"; 
       SQL += " SUM(case when Expense.Type='debit' and (Expense.Date>[email protected]+'-04-01' and Expense.Date<[email protected]+'-06-30') then Expense.Amount else 0 end) as QuarterTwoDebits,"; 
       SQL += " SUM(case when Expense.Type='debit' and (Expense.Date>[email protected]+'-07-01' and Expense.Date<[email protected]+'-09-30') then Expense.Amount else 0 end) as QuarterThreeDebits,"; 
       SQL += " SUM(case when Expense.Type='debit' and (Expense.Date>[email protected]+'-10-01' and Expense.Date<[email protected]+'-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; 
       } 
      } 
+0

@CodeCaster我刪除了字符串外部的動態年份」@year「,因爲它是一個動態值。當寫成'@ year-04-01'時,SQL將「@year」視爲字符串而不是動態值。 –

+0

@CodeCaster完成! –