[英]Equivalent of time(7) SQL Server 2008 datatype in .NET
What is the equivalent of SQL Server 2008's time(7)
datatype in .NET? .NET中SQL Server 2008的
time(7)
数据类型的等价物是什么? I want to store hours and minutes alone in that column, for this which datatype should I use in SQL Server! 我想在该列中单独存储小时和分钟,因为我应该在SQL Server中使用哪种数据类型!
MSDN gives a mapping between SQL data types and CLR data types - and it suggests TimeSpan
and Nullable<TimeSpan>
(for nullable columns) too. MSDN提供了SQL数据类型和CLR数据类型之间的映射 - 它也建议
TimeSpan
和Nullable<TimeSpan>
(对于可空列)。 Note that how you access the data will determine how you actually get the value out though. 请注意,您访问数据的方式将决定您实际获取值的方式。
DbDataReader
doesn't have a GetTimeSpan
method for example - but SqlDataReader
does have such a method . DbDataReader
没有GetTimeSpan
方法 - 但是SqlDataReader
确实有这样的方法 。 I'd expect LINQ to SQL or Entity Framework to perform the mapping automatically. 我希望LINQ to SQL或Entity Framework能够自动执行映射。
As Jon Skeet said, Class SqlDataReader does have a GetTimeSpan Method 正如Jon Skeet所说, Class SqlDataReader确实有一个GetTimeSpan方法
conn = new SqlConnection(blablabla);
conn.Open();
string sql = "SELECT * FROM MyTable";
SqlCommand sqlCommand = new SqlCommand(sql, conn);
reader = sqlCommand.ExecuteReader();
while (reader.Read())
{
MyVeryOwnModel mvom = new MyVeryOwnModel();
mvom.timeStart = reader.GetTimeSpan(reader.GetOrdinal("column_time_start"));
mvom.timeEnd = reader.GetTimeSpan(reader.GetOrdinal("column_time_end"));
}
reader.Close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.