C# 如何将 DbType.Time 的 Datareader 结果转换为 Timespan 对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/592151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 09:30:56  来源:igfitidea点击:

How to Convert Datareader Result of DbType.Time to Timespan Object?

c#timedata-accesstimespandaab

提问by Element

I am reading a result from a MS SQL 2008 Database with a column type of dbtype.time from a datareader, using c# with DAAB 4.0 framework.

我正在使用带有 DAAB 4.0 框架的 c# 从数据读取器读取 MS SQL 2008 数据库的结果,列类型为 dbtype.time。

My problem is the MSDN docs say dbtype.time should map to a timespan but the only close constructor for timespan I see accepts a long, and the result returned from the datareader cannot be cast to a long, or directly to a timespan.

我的问题是 MSDN 文档说 dbtype.time 应该映射到一个时间跨度,但我看到的唯一一个关闭的时间跨度构造函数接受一个 long,并且从数据读取器返回的结果不能转换为一个 long,或直接转换为一个时间跨度。

I found this Articlewhichs shows datareader.getTimeSpan() method, but the datareader in daab 4.0 does not seem to have this method.

我发现这篇文章显示了 datareader.getTimeSpan() 方法,但是 daab 4.0 中的 datareader 似乎没有这个方法。

So how do I convert the result from the datareader to a timespan object ?

那么如何将数据读取器的结果转换为时间跨度对象?

采纳答案by Ken Browning

GetTimeSpanis a method of OleDbDataReaderand SqlDataReader(but not of the more generic IDataReader interface which DAAB's ExecuteReaderreturns). I'm assuming that the IDataReaderinstance which DAAB has returned to you is actually an instance of SqlDataReader. This allows you to access the GetTimeSpanmethod by casting the IDataReaderinstance appropiately:

GetTimeSpanOleDbDataReaderand SqlDataReader(但不是 DAABExecuteReader返回的更通用的 IDataReader 接口的方法)。我假设IDataReaderDAAB 返回给您的实例实际上是SqlDataReader. 这允许您GetTimeSpan通过IDataReader适当地转换实例来访问该方法:

using (IDataReader dr = db.ExecuteReader(command))
{
    /* ... your code ... */
    if (dr is SqlDataReader)
    {
        TimeSpan myTimeSpan = ((SqlDataReader)dr).GetTimeSpan(columnIndex)
    }
    else
    {
        throw new Exception("The DataReader is not a SqlDataReader")
    }
    /* ... your code ... */
}

Edit: If the IDataReaderinstance is not a SqlDataReaderthen you might be missing the providerattribute of your connection string defined in your app.config (or web.config).

编辑:如果IDataReader实例不是,SqlDataReader那么您可能缺少provider在 app.config(或 web.config)中定义的连接字符串的属性。

回答by Adam Ralph

What is the .NET type of the column value? If it is a DateTime then you can pass the value of its Ticks property (long) to the TimeSpan constructor. E.g.

列值的 .NET 类型是什么?如果它是 DateTime,那么您可以将其 Ticks 属性(long)的值传递给 TimeSpan 构造函数。例如

var span = new TimeSpan(colValue.Ticks);

回答by BFree

Have you tried a direct cast like this?

你试过这样的直接演员吗?

TimeSpan span = (TimeSpan)reader["timeField"];

I just tested this quickly on my machine and works fine when "timeField" is a Time datatype in the database (SQL).

我刚刚在我的机器上快速测试了这个并且当“timeField”是数据库(SQL)中的时间数据类型时工作正常。

回答by Jason Steele

Here's my take:

这是我的看法:


using (IDataReader reader = db.ExecuteReader(command))
{
    var timeSpan = reader.GetDateTime(index).TimeOfDay;
}

Cleaner, perhaps!

也许更干净!