在C#Forms中使用DateTimePicker搜索数据库记录 [英] Search Database Record using DateTimePicker in C#Forms

查看:144
本文介绍了在C#Forms中使用DateTimePicker搜索数据库记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我已经将MS Access数据库链接到C#表单.我可以编辑,查看,更新每个条目,但是我需要使用DateTimePicker搜索数据库中的记录.例如,如果我选择一个日期,它将显示该日期在数据库中的记录.我试图自己解决这个问题,但是失败了.任何可以帮助我解决这个问题的人,我都会非常感激.谢谢.

公告

数据库中有一个列名"Date",其数据类型为Date/Time

Hi Guys, I have linked a MS Access Database to a C# Form. I can Edit, View, Update each entries But I need to search a record in the database using DateTimePicker. For an example if i select a date, it should display the records in database on that date. I tried to solve this by my self, but failed. Anyone who can help me to solve this, I''d be really grateful. Thanks.

Notice

There is a Column name "Date" in the Database and Its data type is Date/Time

推荐答案

Hm.也许您的问题是DateTime与Date的问题:列的值和DateTimePicker都还包含小时,分钟等.
尝试使用Date()函数在sql查询中删除这些多余的数据,例如
Hm. Maybe your problem is the DateTime vs. Date issue: both the value of your column and the DateTimePicker also contains hours, minutes, etc.
Try to cut away those extra data in your sql query with the Date() function, e.g.
SELECT *
FROM MyTable 
WHERE Date([Date])=Date(@DateTimePickerValue)


如果您使用参数化查询,则应该可以.

假设您的表的DATE列名称是datecolumn.

If you use parameterised query then it should work.

Let say the DATE column name for your table is datecolumn.

string mySQL = "SELECT * FROM table WHERE (DateColumn = @dateColumn)"

SqlCommand cmd = new SqlCommand (mySQL)
cmd.Parameters.Add("@dateColumn", dateFromDateTimePicker);

SqlDataReader reader = cmd.ExecuteReader();


只需使用以下简单查询:
Just use this simple query:
String query = "SELECT * FROM tblExpense WHERE date='" + date + "'";
                SqlConnection Conn = create_connection.CreateConnection();
                SqlCommand cmd = new SqlCommand(query, Conn);
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    myResult.Text=reader["yourSearch"].ToString();

                }
                else
                {
                    MessageBox.Show("Not Found!!");
                }


这篇关于在C#Forms中使用DateTimePicker搜索数据库记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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