【问题标题】:Compare Selected Date and Database Date比较选定日期和数据库日期
【发布时间】:2019-04-04 04:10:39
【问题描述】:

我的逻辑是:

"如果选择的日期与Holiday表中的数据库日期匹配,则返回消息为"OK"

我已经在下面显示的代码中格式化了日期。当我使用硬编码的数据库日期进行测试时,代码工作正常。

如何从我的Holiday 表中获取数据库日期?

PS:Holiday 表包含不同的日期,因此系统需要循环搜索Holiday 表中的每一行。

代码:

[System.Web.Services.WebMethod]
public static string GetDateFromDB(DateTime compareDate)
{      
    string selectedDate = compareDate.ToString("yyyy/MM/dd");   

    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LoginDBConnectionString1"].ConnectionString);
    SqlCommand com = new SqlCommand("SELECT * from Holiday", conn);
    SqlDataAdapter sqlDa = new SqlDataAdapter(com);
    DataTable dt = new DataTable();
    sqlDa.Fill(dt);

    //hardcoded is ok 
    string dbDateString = "2019-02-20";
    DateTime date1 = DateTime.ParseExact(dbDateString.Split(' ')[0], "yyyy/MM/dd", null);           
    string dateDB = date1.ToString("yyyy/MM/dd");

    if (dateDB == selectedDate)
    {     
        return "OK";
    }
    else
    {
        return "NG";
    }          
}

【问题讨论】:

  • 您是否有格式化日期或从DataTable获取数据的问题?

标签: c# asp.net sql-server date


【解决方案1】:
string selectedDate = compareDate.ToString("yyyy/MM/dd");   

    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LoginDBConnectionString1"].ConnectionString);
    SqlCommand com = new SqlCommand("SELECT * from Holiday where Date='" + selectedDate + "'", conn);
    SqlDataAdapter sqlDa = new SqlDataAdapter(com);
    DataTable dt = new DataTable();
    sqlDa.Fill(dt);

if (dt == null || dt.Rows.Count() == 0)
    return "NG";
else
    return "OK";

【讨论】:

  • 嗨谢谢你,它现在工作我必须使用:if (dt == null || dt.Rows.Count == 0) 而不是: if (dt == null || dt.Rows.Count() == 0)
【解决方案2】:

你说你有多行,所以你可以在循环所有行之后返回“OK”或“NG”(或者在有错误时中断):

[System.Web.Services.WebMethod]
public static string GetDateFromDB(DateTime compareDate)
{      
    string selectedDate = compareDate.ToString("yyyy/MM/dd");   

    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LoginDBConnectionString1"].ConnectionString);
    SqlCommand com = new SqlCommand("SELECT * from Holiday", conn);
    SqlDataAdapter sqlDa = new SqlDataAdapter(com);
    DataTable dt = new DataTable();
    sqlDa.Fill(dt);

    if (dt != null && dt.Rows.Count > 0)
    {
        string formatDate = "yyyy/MM/dd";
        foreach (DataRow dr in dt.Rows)
        {
            string dateString = dr["yourColumnName"].ToString();
            if (string.IsNullOrEmpty(dateString))
            {
                continue; // Or set error or something
            }

            dateString = DateTime.ParseExact(dateString.Split(' ')[0], formatDate, null).ToString(formatDate);
            if (dateString.Equals(compareDate))
            {
                // Do something
            }
            else
            {
                // Set error message or something
            }
        }

        // Check after loop and return "OK" or "NG"
    }     
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 2015-07-20
    相关资源
    最近更新 更多