检索时间跨度值 [英] Retrieve timespan value

查看:101
本文介绍了检索时间跨度值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在时间(7)格式的sqlserver数据库中存储了一个时间跨度值,但是现在我需要记录那个时间跨度值并执行一些计算我使用了这段代码,但显示错误





I have stored a timespan value in sqlserver database in Time(7) format but now i need to retieve that timespan value and perform some calculation i used this code but its showing error


string tm1 = "Select Test1Time from Result where UserName='" + ref1.ToString() + "'";
sqlCommand tkk1 = new SqlCommand(tm1, con);
TimeSpan tm11 = tkk1.ExecuteScalar;
SqlCommand tkk2 = new SqlCommand(tm2, con);






and

TimeSpan tm33 = TimeSpan.Parse(tkk3.ToString());







添加的代码块[/ edit]

推荐答案

你正在做几个错误。 />
TimeSpan用于计算两个日期/时间之间的差异。 SQL查询返回单个值 - Test1Time。

这可能是字符串或单个日期/时间。因此,您不需要使用TimeSpan,并且通常不会在数据库中存储时间跨度。相反,您可以存储单个日期/时间并使用时间跨度来计算您在数据库中保存的值与当前日期/时间之间的差异。



假设您已经修复了这个问题,首先,要将DateTime转换为TimeSpan,请使用:

There are several mistakes you are doing.
TimeSpan is used to calculate the difference between two dates / times. You SQL query returns a single value - Test1Time.
This is probably either a string or a single date/time. So you don''t need to use TimeSpan, and you normally won''t store a Time Span in your database. Instead you would store a single date/time and use Time Span for example, to calculate the difference between the value you have saved in the database and the current date/time.

Assuming you have fixed that, First, to convert DateTime into TimeSpan use this:
TimeSpan span1 = new TimeSpan(tm1.Now.Ticks),span2 = new TimeSpan(tm2.Now.Ticks),span3 = new TimeSpan(tm3.Now.Ticks);



要添加三个,你需要:


To add them three, you need :

TimeSpan span1=tm11;
TimeSpan total = span1.Add(span2);
total=total.Add(span3);


这篇关于检索时间跨度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆