如何从C#.net检索SQL Server中的其他日期时间格式 [英] How to retrieve different Date time format in sql server from C#.net

查看:80
本文介绍了如何从C#.net检索SQL Server中的其他日期时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Datetime格式varchar method.在我的数据库中存储了两种格式22/01/2011和01-Apr-2011.如何在C#.net中检索两种格式.


问候
......

I am using Datetime format varchar method.in my db stored two format 22/01/2011 and 01-Apr-2011 . how to retrieve both format in C#.net.


regards
......

推荐答案

不要在数据库中将日期存储为Varchar-始终将其存储为Date或DateTime.这样,日期是不依赖于区域性的,因此您可以对它进行数学运算:例如,按日期排序仅适用于Date或DateTime,因为它取决于区域性和varchar的愚蠢顺序,因为"Feb"在"Jan"之前例子.

当获取日期时,将其立即转换为C#中的DateTime,当您输出日期时,将其保留在最后一刻,以将其转换回字符串.这样,您的输入应该处于当前的用户区域性(这使他感到高兴),并且如果需要,您的输出可以具有不同的区域性格式(这也使该用户保持满意).
Do not store dates in your DB as Varchar - always store them as Date or DateTime. That way, the date is culture independent, and you can do math with it: sorting by date for example only works properly with Date or DateTime, it is culture dependant and silly orders for varchar, because "Feb" comes before "Jan" for example.

When you get a date convert it immediately to DateTime in C#, when you output dates, leave it to the last moment to convert it back to a string. That way your inputs should be in the current user culture (which keeps him happy) and your outputs can be in a different culture format if required (which keep sthat user happy as well).


这篇关于如何从C#.net检索SQL Server中的其他日期时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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