将查询参数转换为Date数据类型

时间:2016-11-16 15:35:11

标签: c# asp.net datetime asp.net-web-api

我们正在创建一个API,它接收两个输入参数,这些参数将在Query中用于从数据库中选择数据。在两个输入参数中,一个是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。我不确定我是否做对了

2 个答案:

答案 0 :(得分:1)

使用System.Globalization命名空间来解决CultureInfo问题。然后使用DateTime.ParseExact方法:

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

答案 1 :(得分:0)

代码:

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