如何以int格式链接月份日历到日期? C# [英] How do I link a month calendar to date in the int format? C#

查看:117
本文介绍了如何以int格式链接月份日历到日期? C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在sql server中有一个字段,它将日期存储为int(20160914)。

我需要一种方法将它链接到月历在visual studio 2010中,当我在月历上选择日期时,它将显示sql表中相应日期的记录。



我是什么尝试过:



我还在学习C#所以不知道从哪里开始。

Hi,
I have a field in sql server which stores the date as an int (20160914).
I need a way to link it to a month calendar in visual studio 2010 so that it will show the record with the corresponding date from the sql table when I pick a date on the month calendar.

What I have tried:

I am still learning C# so have no idea where to start.

推荐答案

使用ADO.net从表中获取值,然后将其转换为C#datetime为



Get the Value from table using ADO.net and then convert it to C# datetime as

int date = 20160914;
       DateTime dt = DateTime.ParseExact(date.ToString(), "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);


最好的方法不是将日期存储为整数:更改数据库,使它们存储为DATE或DATETIME - 它将通过整个系统正常工作,不会给你带来麻烦。始终将值存储在正确的数据类型中:使用错误的数据似乎是一个好主意,但它总是会给你带来问题。

如果你不这样做,那么你每次都需要对数据感到满意想要使用它:

The best approach is not to store dates as integers: change your DB so they are stored as DATE or DATETIME - that way it will "just work" through the whole system and won't give you problems. Always store values in the correct data type: using the wrong one may seem like a good idea but it always gives you problems.
If you don't then you have to faff with the data every time you want to use it:
SELECT DATEFROMPARTS(myDateInAStupidFormatColumn / 10000, (myDateInAStupidFormatColumn/100) % 100, myDateInAStupidFormatColumn %100)

并希望数据库中的数据始终是有效日期...



然后执行类似的操作,以便在使用日历值时撤消此过程。



相信我,在问题变得太大之前改变你的数据库是一个更好,更好的解决方案!

And hope that the data in your DB is always a valid date...

Then do something similar to reverse the process when you use the calendar value.

Trust me, changing your DB now before the problem gets too big is a much, much better solution!


这篇关于如何以int格式链接月份日历到日期? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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