检索数据类型为 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"));

但是我得到一个错误

Input string was not in a correct format

当我为 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。如何获得?任何帮助表示赞赏。

由于 time mapped with TimeSpan 在 CLR 端,TimeSpan 格式与 DateTime 格式有 一点点 不同。

来自自定义时间跨度格式字符串;

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.

这就是为什么您需要使用 \: 作为分隔符并且需要使用 hh specifier 因为 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"));

进一步阅读: