检索数据类型为time的时间值(1)(Retrieving the time value where the datatype is time(1))

编程入门 行业动态 更新时间:2024-10-28 19:29:06
检索数据类型为time的时间值(1)(Retrieving the time value where the datatype is time(1))

在我的一个Windows应用程序中,我正在使用SQL Server 2008 R2。 存在具有数据类型time(1)的列的位置。

我在该列中有一个虚拟值,比如09:00:00 ,其格式为HH:mm:ss 。

现在,当我在代码中检索相同的值并将其存储到网格中时。 我使用以下代码。

VSFlexShift.set_TextMatrix(i, 2, objShiftMasterDTO.SHIFT_START.ToString("HH:mm:ss"));

但是我收到了一个错误

输入字符串的格式不正确

当我存储ex的日期值时:

Column: date datatype: DateTime Value: 2015-11-30 15:05:01.120

当我试图在代码中检索它时

VSFlexHoliday.set_TextMatrix(i, 3, objHolidayMasterDTO.HOLIDAY_DATE.ToString("dd/MM/yyyy"));

这是返回: 30/11/2015 。

现在我需要获得我之前提到的时间格式为9:00:00 。 怎么弄呢? 任何帮助赞赏。

In one of my windows application I am using SQL Server 2008 R2. Where there is a column with datatype time(1).

I have a dummy value in that column lets say 09:00:00 which is in the format HH:mm:ss.

Now when I am retrieving the same value in the code and storing it into grid. I use the following code.

VSFlexShift.set_TextMatrix(i, 2, objShiftMasterDTO.SHIFT_START.ToString("HH:mm:ss"));

But I get an error

Input string was not in a correct format

When I store the date value for ex:

Column: date datatype: DateTime Value: 2015-11-30 15:05:01.120

When I tried to retrieve it in code

VSFlexHoliday.set_TextMatrix(i, 3, objHolidayMasterDTO.HOLIDAY_DATE.ToString("dd/MM/yyyy"));

This is returned: 30/11/2015.

Now I need to get the time format what I have mentioned first as 9:00:00. How to get it? Any help appreciated.

最满意答案

由于time映射与 CLR端的TimeSpan , TimeSpan格式与DateTime格式略有不同。

来自自定义TimeSpan格式字符串;

自定义TimeSpan格式说明符不包括占位符分隔符符号,例如将小时数与小时数,小时数与分数数或秒数(小数秒)分隔开的符号。 相反,这些符号必须作为字符串文字包含在自定义格式字符串中。 例如, "dd\.hh\:mm"将句点( . )定义为天和小时之间的分隔符,冒号( "dd\.hh\:mm"作为小时和分钟之间的分隔符。

这就是为什么你需要使用\: for separators并且需要使用hh说明符,因为TimeSpan没有HH说明符作为自定义说明符。

VSFlexShift.set_TextMatrix(i, 2, objShiftMasterDTO.SHIFT_START.ToString("hh\\:mm\\:ss"));

要么

VSFlexShift.set_TextMatrix(i, 2, objShiftMasterDTO.SHIFT_START.ToString(@"hh\:mm\:ss"));

进一步阅读:

自定义TimeSpan格式字符串 自定义日期和时间格式字符串

Since time mapped with TimeSpan in CLR side, TimeSpan formatting is little bit different than DateTime formatting.

From Custom TimeSpan Format Strings;

The custom TimeSpan format specifiers do not include placeholder separator symbols, such as the symbols that separate days from hours, hours from minutes, or seconds from fractional seconds. Instead, these symbols must be included in the custom format string as string literals. For example, "dd\.hh\:mm" defines a period (.) as the separator between days and hours, and a colon (:) as the separator between hours and minutes.

That's why you need to use \: for separators and need to use hh specifier since TimeSpan does not have HH specifier as a custom specifier.

VSFlexShift.set_TextMatrix(i, 2, objShiftMasterDTO.SHIFT_START.ToString("hh\\:mm\\:ss"));

or

VSFlexShift.set_TextMatrix(i, 2, objShiftMasterDTO.SHIFT_START.ToString(@"hh\:mm\:ss"));

Further reading:

Custom TimeSpan Format Strings Custom Date and Time Format Strings

更多推荐

本文发布于:2023-07-18 10:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1158584.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据类型   时间   datatype   time   Retrieving

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!