[英]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.注意:在我的网站上,我只需要上述日期的
YEAR
、 MONTH
和DAY
。 Not sure if that helps you, probably not but just wanted to say.不确定这是否对您有帮助,可能不是,但只是想说。
A date
column in sql
maps to a DateTime
in c#
. sql
的date
列映射到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.ToShortDateString
或DateTime.ToString(string)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.