如何使用日期过滤数据库中的数据并填写组合框 [英] How to filter data from database using date and fill in combobox
本文介绍了如何使用日期过滤数据库中的数据并填写组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从数据库填充组合框中的特定值,使用日期过滤。
请帮我这样做。
我尝试了什么:
I need to fill a specific value on the combobox from the database which is to be filtered by using date.
Please help me to do this.
What I have tried:
OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Rd\\Documents\\Soundar1.accdb");
DataTable dt = new DataTable();
DataSet ds = new DataSet();
private void button1_Click(object sender, EventArgs e)
{
//try
//{
connection.Open();
string sql = "select Sno from Table1 where dt=" + dateTimePicker1.Value.ToString("MM/dd/yyyy");
OleDbDataAdapter da = new OleDbDataAdapter(sql, connection);
da.Fill(dt);
//da.Fill(ds,"Table1");
comboBox1.DisplayMember = "Sno";
comboBox1.DataSource = dt;
connection.Close();
//}
//catch (Exception ex)
//{
// MessageBox.Show(ex.ToString());
//}
}
我试过这个但是没有用。
I tried this but its not working.
推荐答案
试试这个
try this
string value = dateTimePicker1.Value.ToString("MM/dd/yyyy");
string sql = "select Sno from Table1 where dt= @dt";
OleDbDataAdapter da = new OleDbDataAdapter(sql, connection);
da.SelectCommand.Parameters.AddWithValue("@dt", value);
da.Fill(dt);
这篇关于如何使用日期过滤数据库中的数据并填写组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文