InvalidCastException:无法将类型为'System.TimeSpan'的对象强制转换为'System.Nullable`1 [System.DateTime]'

时间:2016-12-20 07:30:15

标签: c# sql datetime error-handling casting

这是我的表格结构 我附上表的图像与记录 On this table structure

当我尝试从此表中获取数据并想要在页面上显示时,我会收到这样的错误

invalidCastException:无法将类型为'System.TimeSpan'的对象强制转换为'System.Nullable`1 [System.DateTime]'。 ThrowDataException InvalidOperationException:解析第5列时出错(EventTime = 11:30:00 - Object)

我在存储过程中使用此命令select * from events来获取此表中的所有记录。 EventTime列的数据类型是时间,当我在c#上创建此列的属性时,数据类型为datetime 我能解决这个问题吗?

这里是存储过程代码,以便更好地理解

USE [RG_DEC12]
GO
/****** Object:  StoredProcedure [dbo].[GetAllBlogs]    Script Date: 12/20/2016 12:01:57 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Gaurav Sharma>
-- Create date: <20/12/2016>
-- Description: <Description,,>
-- =============================================
alter PROCEDURE [dbo].[GetEvents]
    -- Add the parameters for the stored procedure here

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

   select * from Events

END

2 个答案:

答案 0 :(得分:1)

我得到了像这样

投射eventtime列所需的答案

CONVERT(char(10), eventtime, 108) as EventTime

答案 1 :(得分:1)

有一个投射问题来投射您的活动时间 你必须像这样投射事件时间栏。

CONVERT(char(10), eventtime, 108) as EventTime

请这样做并告诉我。