如何获取表的准确时间值? [英] how to get the exact time value to a table?

查看:90
本文介绍了如何获取表的准确时间值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码运行时没有任何错误,但将时间值存储为表中的"1/1/1900 9:34:00 AM".如何才能仅从中获取时间呢?这里的日期也是错误的.为什么会这样呢?
请帮助我!

this code runs with out any error,but it stores the time value as "1/1/1900 9:34:00 AM" in the table.How can we get only the time out of this? Here the date is also wrong.Why is it happening like that?
please help me!!!

openConnection();
string query = "insert into votes ([time],[V1],[V2],[V3]) values (@time,@V1,@V2,@V3)";
SqlCommand cmd2 = new SqlCommand(query, con);

cmd2.Parameters.Add(new SqlParameter("@time", DateTime.Now.ToShortTimeString()));
cmd2.Parameters.Add(new SqlParameter("@V1", selCand_No1));
cmd2.Parameters.Add(new SqlParameter("@V2", selCand_No2));
cmd2.Parameters.Add(new SqlParameter("@V3", selCand_No3));

cmd2.ExecuteNonQuery();
MessageBox.Show("Succesfully Inserted");
closeConnection();

推荐答案

问题可能归结为您存储为时间"的对象的类型.它在数据库中是什么样的对象?我怀疑数据库无法计算出您传递的值.当然,从结果中可以清楚地看到,您正在使用一个期望日期/时间并且只是传递时间的对象.时间部分正确吗?如果是这样,如果您在数据库中使用DateTime类型的对象,那么当您未能提供日期部分时,它将尽其所能.
The problem probably comes down to the type of object you''re storing as ''time''. What sort of object is it, in your database ? I suspect that the DB can''t work out what to do with the value you''re passing. Of course, it''s clear from the result, that you''re using an object that expects a date/time and just passing in a time. Is the time portion correct ? If so, if you''re using a DateTime type object in the DB, it is doing the best it can, when you''re failing to provide the date portion.


可能原因可能是列的数据类型,它是DateTime吗?如果是,那么如何在日期时间类型列中输入时间值(设计问题).
The probable reason could be the datatype of the column, is it DateTime? If yes then how you can enter time value in datetime type column (design problem).


Try this it returns you only time from whole format
mydate is my datetime column in the table employee.


select convert(nvarchar,mydate,8)  from employee

thanks


这篇关于如何获取表的准确时间值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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