开发者

Equivalent of time(7) SQL Server 2008 datatype in .NET

What is the equivalent of SQL Server 2008's time(7) datatype in .NET? I want to store hours and minutes alone in that column, for this which datatype should I use in SQL S开发者_Go百科erver!


MSDN gives a mapping between SQL data types and CLR data types - and it suggests TimeSpan and Nullable<TimeSpan> (for nullable columns) too. 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. I'd expect LINQ to SQL or Entity Framework to perform the mapping automatically.


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.


As Jon Skeet said, Class SqlDataReader does have a GetTimeSpan Method

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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜