Datareader Exception Thrown Database - DateTime

时间:2015-12-22 18:36:23

标签: c# ms-access

在我的Access数据库中,我有一个看起来像这样的AutditLog表

The Log Table

我只想访问此表并在Created *(日志的日期)等于今天的日期时显示每一位信息。 这是我的代码

private void ITemail()
{
     string FinEmail;
     clsDBConnector dbConnector = new clsDBConnector();
     OleDbDataReader dr;
     dbConnector.Connect();
     var sqlStr = " SELECT  Created, [Action], ConnectionLoc, ConnectionSystem, Resource, [Text], RecordId, ToVal, ClientName"
        + "FROM tblAudit WHERE (ClientName = '" + Client + "') AND 'Created' = '" + ToDate() + "'";
     dr = dbConnector.DoSQL(sqlStr);
     while (dr.Read())
     {
         txtIT.Text = dr[0].ToString() + dr[1].ToString() + dr[2].ToString() + dr[3].ToString() + dr[4].ToString() + dr[5].ToString() + dr[6].ToString()
                + dr[7].ToString() + dr[8].ToString();
     }
}
private string ToDate()
{
    return DateTime.Today.ToString("dd/MM/yyyy");
}

问题是,即使我在服务器资源管理器中尝试使用正确的日期作为字符串,也不会返回任何内容。当它到达While循环时抛出此异常:

enter image description here

非常感谢任何帮助

1 个答案:

答案 0 :(得分:2)

您的日期格式和日期分隔符不正确且缺少空格:

private void ITemail()
{
     string FinEmail;
     clsDBConnector dbConnector = new clsDBConnector();
     OleDbDataReader dr;
     dbConnector.Connect();
     var sqlStr = "SELECT Created, [Action], ConnectionLoc, ConnectionSystem, Resource, [Text], RecordId, ToVal, ClientName "
        + "FROM tblAudit WHERE (ClientName = '" + Client + "') AND Created = #" + ToDate() + "#";
     dr = dbConnector.DoSQL(sqlStr);
     while (dr.Read())
     {
         txtIT.Text = dr[0].ToString() + dr[1].ToString() + dr[2].ToString() + dr[3].ToString() + dr[4].ToString() + dr[5].ToString() + dr[6].ToString()
                + dr[7].ToString() + dr[8].ToString();
     }
}
private string ToDate()
{
    return DateTime.Today.ToString("yyyy'/'MM'/'dd");
}