简体   繁体   English

等效于时间(7).NET中的SQL Server 2008数据类型

[英]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数据类型之间的映射 - 它也建议TimeSpanNullable<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能够自动执行映射。

Got a nice article from this link. 这个链接得到了一篇很好的文章。

I will use timespan datatype in dot net as equivalent for time(7) datatype in SQL server to store hours and minutes. 我将使用dot net中的timespan数据类型作为SQL Server中time(7)数据类型的等效数据来存储小时和分钟。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM