如何在。net应用程序中使用sql server time数据类型?

时间:2021-12-29 16:06:02

Does anybody knows how to use time data type introduced in sql server 2008 in c# .net application ? I've been trying to get it to work but no success.

有人知道如何在c#。net应用程序中使用sql server 2008中引入的时间数据类型吗?我一直试图让它发挥作用,但没有成功。

5 个解决方案

#1


41  

Here is an MSDN article that reviews all the new Date and Time datatypes introduced in SQL Server 2008 with respect to ADO.NET. As the document says: For System.Data.DbType.Time you would use the .NET Framework type System.TimeSpan

这里有一篇MSDN文章,它回顾了SQL Server 2008中关于ADO.NET的所有新日期和时间数据类型。正如文档所说:For System.Data.DbType。使用。net框架类型System.TimeSpan的时间

#2


5  

Also even people from Microsoft tend to recommend mapping the sql datatype time to System.Timestamp I would not recommend doing so,

甚至微软的人也倾向于建议将sql数据类型时间映射到系统。我不建议这样做,

since the range of sql time is 00:00:00.0000000 - 23:59:59.9999999

因为sql时间的范围是00:00 - 00000000 - 23:59:59.9999999。

wheras the range of System.TimeSpan is 10675199.02:48:05.4775808 - 10675199.02:48:05.4775807

系统的范围。TimeSpan是10675199.02:48:05.4775808 - 10675199.02:48:05.4775807。

which is just slightly different and can lead to nasty runtime out of range errors.

这只是略有不同,可能会导致运行时出现错误。

#3


4  

I think you can use TimeSpan data type for your purpose. Here is an article that explains the use of Time datatype in ado.net

我认为您可以将TimeSpan数据类型用于您的目的。下面是一篇解释ado.net中时间数据类型的使用的文章

#4


1  

How are you accessing the data? Using Entity Frameworks, StoredProcedures etc.
If you have to pass a string representation of the date make sure that you do it in the format "yyyy-mm-dd hh:mm:ss" otherwise you will run the risk of the dd/mm/yyyy vs mm/dd/yyyy confusion. If you are using Entity Framework or DataSets then you should just pass the parameter a DataTime instance eg DateTime.Now

如何访问数据?使用实体框架、存储过程等。如果您必须传递日期的字符串表示形式,请确保以“yyyyy -mm-dd -dd - hh:mm”格式进行,否则您将面临dd/mm/yyy vs mm/dd/yyyy混淆的风险。如果您正在使用实体框架或数据集,那么您应该只传递参数a DataTime实例eg DateTime.Now

#5


0  

you can read it using datareader using something similar to following statement .

您可以使用datareader使用类似于以下语句的内容来读取它。

TimeSpan time = dr.GetTimeSpan(dr.GetOrdinal(“Time7FieldName”));

#1


41  

Here is an MSDN article that reviews all the new Date and Time datatypes introduced in SQL Server 2008 with respect to ADO.NET. As the document says: For System.Data.DbType.Time you would use the .NET Framework type System.TimeSpan

这里有一篇MSDN文章,它回顾了SQL Server 2008中关于ADO.NET的所有新日期和时间数据类型。正如文档所说:For System.Data.DbType。使用。net框架类型System.TimeSpan的时间

#2


5  

Also even people from Microsoft tend to recommend mapping the sql datatype time to System.Timestamp I would not recommend doing so,

甚至微软的人也倾向于建议将sql数据类型时间映射到系统。我不建议这样做,

since the range of sql time is 00:00:00.0000000 - 23:59:59.9999999

因为sql时间的范围是00:00 - 00000000 - 23:59:59.9999999。

wheras the range of System.TimeSpan is 10675199.02:48:05.4775808 - 10675199.02:48:05.4775807

系统的范围。TimeSpan是10675199.02:48:05.4775808 - 10675199.02:48:05.4775807。

which is just slightly different and can lead to nasty runtime out of range errors.

这只是略有不同,可能会导致运行时出现错误。

#3


4  

I think you can use TimeSpan data type for your purpose. Here is an article that explains the use of Time datatype in ado.net

我认为您可以将TimeSpan数据类型用于您的目的。下面是一篇解释ado.net中时间数据类型的使用的文章

#4


1  

How are you accessing the data? Using Entity Frameworks, StoredProcedures etc.
If you have to pass a string representation of the date make sure that you do it in the format "yyyy-mm-dd hh:mm:ss" otherwise you will run the risk of the dd/mm/yyyy vs mm/dd/yyyy confusion. If you are using Entity Framework or DataSets then you should just pass the parameter a DataTime instance eg DateTime.Now

如何访问数据?使用实体框架、存储过程等。如果您必须传递日期的字符串表示形式,请确保以“yyyyy -mm-dd -dd - hh:mm”格式进行,否则您将面临dd/mm/yyy vs mm/dd/yyyy混淆的风险。如果您正在使用实体框架或数据集,那么您应该只传递参数a DataTime实例eg DateTime.Now

#5


0  

you can read it using datareader using something similar to following statement .

您可以使用datareader使用类似于以下语句的内容来读取它。

TimeSpan time = dr.GetTimeSpan(dr.GetOrdinal(“Time7FieldName”));