简体   繁体   English

日期是01/01/0001而不是数据库中的日期?

[英]Date is 01/01/0001 and not the date that is in the database?

I have this line in my sql server 2008 r2 database, but I don't understand: 我在我的sql server 2008 r2数据库中有这一行,但我不明白:

2012-12-06 11:00:36.703 and is of type DATETIME

When I display it in my view it is: 当我在我的视图中显示它时:

Mon, Jan 1, 0001 

Here my code: 这是我的代码:

var allNews = ZincService.NewsService.GetNewsPostsForId(id);

List<Zinc.Web.Areas.News.ViewModels.Home.NewsPostsViewModel> newsItems = new List<NewsPostsViewModel>();

foreach (var newsItem in allNews)
{ 
  NewsPostsViewModel newsItemViewModel = new NewsPostsViewModel();
  newsItemViewModel.CommentDate = String.Format("{0:ddd, MMM d, yyyy}", newsItem.CommentDate);
}

public class NewsPostsViewModel
{
  public Entities.News.News MainNews { get; set; }
  public virtual string News { get; set; }
  public virtual int NewsId { get; set; }
  public virtual string CommentDate { get; set; }
}

  public List<DataModels.News.NewsPostsDataModel> GetNewsPostsForId(int id)
{
  using (SqlConnection conn = new SqlConnection(ZincModelContainer.CONNECTIONSTRING))
  {
    using (SqlCommand cmd = conn.CreateCommand())
    {
      conn.Open();
      cmd.CommandType = System.Data.CommandType.StoredProcedure;
      cmd.CommandText = "[News].[GetNewsPostsForId]";

      SqlParameter param = new SqlParameter("@Id", System.Data.SqlDbType.Int);
      param.Value = id;
      cmd.Parameters.Add(param);

      List<DataModels.News.NewsPostsDataModel> news = new List<DataModels.News.NewsPostsDataModel>();

      using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
      {
        DataTable dt = new DataTable();
        adapter.Fill(dt);

        foreach (DataRow row in dt.Rows)
        {
          string message = Convert.ToString(row["NewsDescription"]);
          news.Add(new DataModels.News.NewsPostsDataModel { NewsPostId = id ,Message = message});
        }
      }

      return news;
    }
  }
}

public class NewsPostsDataModel
{
  public virtual int NewsPostId { get; set; }
  public virtual string Message { get; set; }
  public virtual DateTime CommentDate { get; set; }
}

Can some one help me, please? 有人能帮助我吗?

This is the problem: 这就是问题:

news.Add(new DataModels.News.NewsPostsDataModel { NewsPostId = id ,Message = message});

You're not populating CommentDate here, so it's got the default value of DateTime.MinValue . 你没有在这里填充CommentDate ,所以它有默认值DateTime.MinValue

Of course we now can't tell whether your stored procedure returns the value at all, but assuming it does, you should presumably be fetching it from the DataRow and putting it in your model... 当然,我们现在无法判断您的存储过程是否完全返回值,但假设它存在,您应该从DataRow获取它并将其放入模型中...

Before you just fix the code, you should take a step back and think about your diagnostic process. 在你修好的代码,你应该退一步,想想你的诊断过程。 Did you try debugging into this? 你试过调试吗? Adding logging? 添加日志? You should have been able to see very quickly that you didn't have a CommentDate in the model, and then tried to work out why you didn't have a CommentDate , which should have led you back to the code creating the instance of the model. 您应该能够非常快速地看到模型中没有CommentDate ,然后尝试找出为什么没有CommentDate ,这应该会让您回到创建实例的代码模型。 If you can improve your diagnostic processes, you can speed you your future development significantly by not having to ask as many questions. 如果您可以改进诊断过程,则可以通过不必提出尽可能多的问题来显着加快您未来的发展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DateTime将日期显示为01/01/0001 - DateTime shows the date as 01/01/0001 如果日期不是低日期,则仅包含在 where 条件中(01/01/0001) - Only include in where condition if date is not low date(01/01/0001) 如果日期等于0001-01-01,则DataGrid中的空白行 - Blank row in DataGrid if date is equal to 0001-01-01 MySQL“空”日期是否有值(0001-01-01) - Is there a value for MySQL “null” date (0001-01-01) 将 MVC 默认日期 (01/01/0001) 格式化为空 - Format MVC default Date (01/01/0001) to be empty 无法将日期“0001-01-01”从 Java 正确转换为 C# - Cannot Convert Date '0001-01-01' from Java to C# correctly ASP日历0001/01/01 - asp calendar 0001/01/01 从浏览器查看时,Sharepoint 2010设置cookie到期日期似乎可以正常工作,但是服务器代码看到01/01/0001到期 - Sharepoint 2010 setting a cookie expiration date seemsto work when viewed from browser but server code sees 01/01/0001 expiration 如何在DateTime为空(01/01/0001)时将加载时的DateTimePicker对象保留为空白(选择日期),但在有值时填充该对象? - How to leave DateTimePicker object blank (Select a date) on load when DateTime is null (01/01/0001) but populate when there is a value? 当日期= &#39;01 / 01/1900&#39;时隐藏字段 - Hide field when date ='01/01/1900'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM