如何在Windows应用程序中使用csharp从日历中选择日期时从数据库中检查日期 [英] how to check the date from the database when select the date from the calendar using csharp in windows application

查看:189
本文介绍了如何在Windows应用程序中使用csharp从日历中选择日期时从数据库中检查日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意它是windows应用程序。



Ms Access中的数据库结构如下;



< pre lang =text>字段名称数据类型

假日日期文本
原因文本







在Access中的数据库记录如下;



 Holidaydate Reason 

14/11/2013儿童节
15/08/2013独立日
2/2/213日
28/2/2013假期



设计如下;



我有一个日历如下。



在该日历中,当用户选择星期日日期时,我验证该代码的星期日日期如下;



  private   void  Facavailcal_DateChanged( object  sender,DateRangeEventArgs e)
{

if (e.Start.DayOfWeek == DayOfWeek.Sunday)MessageBox.Show( Don '选择星期日 Not Allowed,MessageBoxButtons.OK,MessageBoxIcon.Information );

return ;

}





以上代码工作正常。





我必须检查另一个,当用户从日历中选择数据库日期时,我想显示弹出消息选择日期是不允许的。



在数据库记录中如下;



 14/11/2013儿童节
15/08/2013独立日
2/2/213日
28/2/2013假期





如果用户从日历中选择任何日期,我想显示弹出消息。



我该怎么办。



请帮帮我。

注意它是windows应用程序。



问候,

Narasiman P





[edit]已添加代码块 - OriginalGriff [/ edit]

解决方案

最简单的方法是查询数据库中的日期:

  SELECT  COUNT(*) FROM  MyTable  WHERE  [Holiday 日期] = '  15/08/2013' 

如果您使用ExecuteScalar方法,它将返回匹配记录的计数 - 如果它不为零,则日期已经存在。



但是为了你自己,不要存储你的数据。

使用日期字段而不是文本字段 - 它更小,可以更容易地排序,比较和一般工作。例如,如果您以后想要查找2013年1月1日至2013年8月18日范围内是否有任何日期,您可以使用日期字段轻松完成此操作 - 但如果它们是文本,则它是PITA(因为作为文本字段,它们会得到一个文本比较,这与日期的工作效果不太好)


Note it is windows application.

Database structure as follows in Ms Access;

Field Name     Data Type

Holiday Date   Text
Reason         Text




In the Database records as follows in Ms Access;

Holidaydate	Reason	

14/11/2013	Children Day
15/08/2013	Indepence Day
2/2/213         Day
28/2/2013       Holiday


Design as follows;

I have one calendar as follows.

In that Calendar when user select the Sunday date, i validate sunday date is not allowed for that code as follows;

private void Facavailcal_DateChanged(object sender, DateRangeEventArgs e)
       {

        if (e.Start.DayOfWeek == DayOfWeek.Sunday) MessageBox.Show("Don't Select Sunday", "Not Allowed", MessageBoxButtons.OK, MessageBoxIcon.Information);

               return;

       }



The above code is working fine.


I have to check another, when user select the database date from the calendar, i want to show the pop message selected date is not allowed.

In the database records as follows;

14/11/2013	Children Day
15/08/2013	Indepence Day
2/2/213         Day
28/2/2013       Holiday



if the user select any of the date from the calendar, i want to show the pop up message.

for that how can i do.

please help me.
Note it is windows application.

Regards,
Narasiman P


[edit]Code block added - OriginalGriff[/edit]

解决方案

The easiest way is to query your database for that date:

SELECT COUNT(*) FROM MyTable WHERE [Holiday Date] = '15/08/2013'

If you use the ExecuteScalar method it will return a count of the record that match - if it is not zero, then the date is already there.

But for your own sake, don''t store your data like that.
Use a Date field instead of a text field - it is smaller, and it can be much more easily sorted, compared and generally worked with. For example, if you later want to find if there are any dates in the range 1/08/2013 to 18/08/2013 you can do that very easily with a Date field - but it''s a PITA if they are text (because as text fields they get a text comparison which doesn''t work too well with dates)


这篇关于如何在Windows应用程序中使用csharp从日历中选择日期时从数据库中检查日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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