2016-11-16 53 views
-1

我們正在創建一個接收兩個輸入參數的API,這些參數將在查詢中用於從數據庫中選擇數據。出了兩個輸入參數集合之一是Date.So我們的API調用將/api/DataReturn?id=56&DOB='01-Jan-2016' 我打算到字符串格式的兩個輸入參數,並將其轉換將查詢參數轉換爲日期數據類型

[HttpGet] 
public HttpResponseMessage Getdetails(string ROOM,string SUBMITDATE) 
{ 
    DateTime submit_date = DateTime.Parse(SUBMITDATE.Trim()).ToString("dd-MMM-yyyy", CultureInfo.InvariantCulture); 
    string connStr = ConfigurationManager.ConnectionStrings["TGSDataConnection"].ConnectionString; 
    using (OracleConnection dbconn = new OracleConnection(connStr)) 
     { 
      DataSet userDataset = new DataSet(); 
      var strQuery = "SELECT * from LIMS_SAMPLE_RESULTS_VW where ROOM = " + ROOM +"and SUBMITDATE =" +"'"+submit_date+"'"; 

它拋出錯誤說The name 'CultureInfo' does not exist in the current context。我不知道如果我做他們的權利

+0

'使用命名空間System.Globalization;' –

+0

我加入了System.Globalization現在拋出'不能鍵入「串」隱式轉換爲「System.DateTime'' – trx

+0

嘗試DateTime.TryParseExact – John

回答

1

使用System.Globalization命名空間來解決CultureInfo問題。然後使用DateTime.ParseExact方法:

DateTime submit_date = DateTime.ParseExact("01-Jan-2016", "dd-MMM-yyyy", 
              CultureInfo.InvariantCulture); 
0

代碼:

DateTime submit_date = Convert.ToDateTime(DateTime.Parse(SUBMITDATE.Trim()).ToString("dd-MMM-yyyy", CultureInfo.InvariantCulture)); 
+0

我得到運行時錯誤'FormatUserException被用戶代碼未處理' – trx

+0

確保SUBMITDATE正確: –

+0

Convert.ToDateTime(DateTime.Parse 「01-Jan-2016」.Trim())。ToString(「dd-MMM-yyyy」,CultureInfo.InvariantCulture)) –

相關問題