简体   繁体   English

Microsoft SQL Server 数据库/Visual Studio C#:日期处理

[英]Microsoft SQL Server database / Visual Studio C# : date handling

I'm trying to figure out what would be the quickest and most convenient way to grab dates from the database and parse/cast them correctly into my DAL (DataAccessLayer) methods.我试图找出从数据库中获取日期并将它们正确解析/转换为我的 DAL (DataAccessLayer) 方法的最快和最方便的方法。

The columns inside my database are of datatype date .我的数据库中的列是数据类型date

Specifically I have this Order class:具体来说,我有这个Order类:

public int Id { get; set; }
public DateTime OrderDate { get; set; }  
public DateTime ConfirmedDeliveryDate { get; set; }  
public DateTime ShippingDate { get; set; }
public Supplier Supplier { get; set; }
public List<Product> ListOfProducts { get; set; } = new List<Product>();

And then of course I have a DAL method for, let's say, getting a list of orders from the database.然后当然我有一个 DAL 方法,比方说,从数据库中获取订单列表。

List<Order> GetAll()
{
  ... ... ...
  ... ... ...
  SqlDataReader rdr = cmd.ExecuteReader()

  while(rdr.Read())
  {
    int orderId = (int)rdr["id"];
    DateTime orderDate = ???                 // HOW TO IMPLEMENT?
    DateTime? confirmedDeliveryDate = ???    // HOW TO IMPLEMENT?
    DateTime? shippingDate = ???             // HOW TO IMPLEMENT?
    ... ...
    ... ...

  }
  ... ... ...
}

NOTE: On my web I will only need YEAR , MONTH and DAY from the above mentioned dates.注意:在我的网站上,我只需要上述日期的YEARMONTHDAY Not sure if that helps you, probably not but just wanted to say.不确定这是否对您有帮助,可能不是,但只是想说。

A date column in sql maps to a DateTime in c# . sqldate列映射到c#DateTime There is no need to do any special parsing.不需要做任何特殊的解析。 Assuming you're using a SqlDataReader , just use SqlDataReader.GetDateTime , or if you don't know the column ordinal use the indexer and pass it the column name then cast the result to a DateTime just like you're doing with the "id" column.假设您使用的是SqlDataReader ,只需使用SqlDataReader.GetDateTime ,或者如果您不知道列序号,请使用索引器并将列名传递给它,然后将结果转换为DateTime就像您使用"id"列。

DateTime orderedDate = (DateTime)rdr["orderedDate"];
// or
DateTime orderdDate = rdr.GetDateTime(/*column ordinal*/)

If the date columns in the database are nullable, then you need to account for that in your code by making the DateTime objects nullable ( DateTime? ) and checking for null when reading the results from the SqlDataReader .如果数据库中的date列可以为空,那么您需要通过使DateTime对象可以为空 ( DateTime? ) 并在从SqlDataReader读取结果时检查是否为空来在代码中考虑到这一点。

List<Order> GetAll()
{
    SqlDataReader rdr = cmd.ExecuteReader()
    while(rdr.Read())
    {
        int orderId = (int)rdr["id"];
        DateTime? orderedDate = null;
        if (rdr["orderedDate"] != DBNull.Value)
        {
            orderedDate = (DateTime)rdr["orderedDate"];
        }
    }
}

You could put this into an extension method on SqlDataReader if you wanted...如果您愿意,您可以将其放入SqlDataReader上的扩展方法中...

public static class SqlDataReaderExtensions
{
    public static DateTime? GetNullableDateTime(this SqlDataReader rdr, string columnName)
    {
        if (rdr[columnName] != DBNull.Value)
        {
            return (DateTime)rdr[columnName];
        }
        return null;
    }
}

List<Order> GetAll()
{
    SqlDataReader rdr = cmd.ExecuteReader()
    while(rdr.Read())
    {
        int orderId = (int)rdr["id"];
        DateTime? orderedDate = rdr.GetNullableDateTime("ordredDate");
    }
}

If your view only needs to display the year, month, and day then you can do that in several ways.如果您的视图只需要显示年、月和日,那么您可以通过多种方式实现。 For example, DateTime.ToShortDateString or DateTime.ToString(string) .例如, DateTime.ToShortDateStringDateTime.ToString(string)

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

相关问题 在窗体C#Visual Studio中比较两个日期起始日期和数据库SQL Server中的日期 - Compare Two Date From Date at Form C# Visual Studio and Date From Database SQL Server C#Visual Studio违反PK for SQL Server数据库 - C# Visual Studio violation of PK for SQL Server database 如何将在 Visual Studio C# 中创建的数据库迁移到 SQL 服务器? - How to migrate a DataBase created in Visual Studio C# to a SQL Server? 使用C#连接到SQL Server 2012数据库(Visual Studio 2012) - Connect to SQL Server 2012 Database with C# (Visual Studio 2012) 在Visual Studio 2012 C#中连接到SQL Server数据库 - Connection to SQL Server Database inside Visual Studio 2012 C# Visual Studio 中的数据库问题(c# 和 SQL 服务器) - Database problem in visual studio (c# and SQL Server) 通过 Visual Studio 上 C# 中的 datagridview 更新 SQL 服务器数据库 - Update SQL Server database via a datagridview in C# on Visual Studio 在Visual Studio中使用C#的SQL Server - SQL Server with C# in Visual Studio 使用C#在Visual Studio中插入,更新和删除Microsoft Access数据库 - Microsoft Access Database, Insert, Update and Delete in Visual Studio with C# 在 Visual Studio 2013 中使用 C# 将文本转换为日期格式并将其保存在 SQL Server 2014 中 - Convert text into date format in Visual Studio 2013 using C# and save it in SQL Server 2014
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM