使用 DataReader 读取日期
我使用数据读取器使用这种格式读取字符串。如何使用类似的格式读取日期?
while (MyReader.Read())
{
TextBox1.Text = (string)MyReader["Note"];
}
I read a string using this format with a data reader. How can I read in a date using similar format?
while (MyReader.Read())
{
TextBox1.Text = (string)MyReader["Note"];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试如下所示:
在
ToString()
方法中,您可以根据您的要求更改数据格式。Try as given below:
in
ToString()
method you can change data format as per your requirement.如果查询的列具有适当的类型,则
如果查询的列实际上是字符串,则查看其他答案。
If the query's column has an appropriate type then
If the query's column is actually a string then see other answers.
或者
OR
这可能看起来有点偏离主题,但这是我在想知道当你在 C# 中将列读取为 dateTime 时会发生什么时遇到的帖子。这篇文章反映了我希望能够找到有关此机制的信息。 如果您担心 utc 和时区,请继续阅读
我做了更多研究,因为我总是对 DateTime 作为一个类非常警惕,因为它会自动假设您正在使用的时区,并且因为它是很容易混淆本地时间和UTC时间。
我在这里试图避免的是 DateTime '哦,看看我正在运行的计算机位于时区 x,因此这个时间也必须位于时区 x,当我被问及我的值时,我会回复就像我在那个时区一样”
我试图读取
datetime2
列。您将从 sql server 返回的日期时间最终将是 Kind.Unspecified 这似乎意味着它会像 UTC 一样对待,这正是我想要的。
当读取
date
列时,您还必须将其作为DateTime
读取,即使它没有时间,而且更容易被时区搞砸(因为它是在午夜) )。我当然认为这是读取 DateTime 的更安全的方法,因为我怀疑它可能可以通过 sql server 中的设置或 c# 中的静态设置进行修改:
从那里您可以获取组件(日、月、年)等并按照您喜欢的方式格式化。
如果您实际上拥有的是日期+时间,那么 Utc 可能不是您想要的 - 因为您在客户端上乱搞,您可能需要首先将其转换为本地时间(取决于时间的含义) )。然而,这会带来一大堆蠕虫。如果你需要这样做,我建议使用像 noda time。标准库中有
TimeZoneInfo
但经过简要调查后,它似乎没有 正确的时区集。您可以使用 TimeZoneInfo.GetSystemTimeZones(); 方法查看 TimeZoneInfo 提供的列表;我还发现 sql server management studio 在显示时间之前不会将时间转换为本地时间。这是一种解脱!
This may seem slightly off topic but this was the post I came across when wondering what happens when you read a column as a dateTime in c#. The post reflects the information I would have liked to be able to find about this mechanism. If you worry about utc and timezones then read on
I did a little more research as I'm always very wary of DateTime as a class because of its automatic assumptions about what timezone you are using and because it is way too easy to confuse local times and utc times.
What I'm trying to avoid here is DateTime going 'oh look the computer I'm being run on is in timezone x, therefore this time must also be in timezone x, when I get asked for my values I'll reply as if I'm in that timezone'
I was trying to read a
datetime2
column.The date time you will get back from sql server will end up being of
Kind.Unspecified
this seems to mean it gets treated like UTC, which is what I wanted.When reading a
date
column you also have to read it as aDateTime
even though it has no time and is even more prone to screwing up by timezones (as it is on midnight).I'd certainly consider this to be safer way of reading the DateTime as I suspect it can probably be modified by either settings in sql server or static settings in your c#:
From there you can get the components (Day, Month, Year) etc and format how you like.
If what you have is actually a date + a time then Utc might not be what you want there - since you are mucking around on the client you may need to convert it to a local time first (depending on what the meaning of the time is). However that opens up a whole can of worms.. If you need to do that I'd recommend using a library like noda time. There is
TimeZoneInfo
in the standard library but after briefly investigating it, it doesn't seem to have a proper set of timezones. You can see the list provided byTimeZoneInfo
by using the methodTimeZoneInfo.GetSystemTimeZones();
I also discovered sql server management studio doesn't convert times to local time before displaying them. Which is a relief!
我知道这是一个老问题,但令我惊讶的是没有答案提到
GetDateTime
:你可以这样使用:
I know that this is an old question, but I'm surprised that no answer mentions
GetDateTime
:Which you can use like:
或者
OR