【发布时间】:2019-11-12 12:03:11
【问题描述】:
我正在编写 C# 代码,我使用 OLEDB 连接字符串连接 MS 访问数据库。 我有一个表单,我在其中根据某些标准在 datagridview 中显示数据库中的数据。 以下是标准:
a) 人(来自数据库中的
b) 进程(来自文本框中的数据库)
c) 从日期(日期时间选择器)
d) 迄今为止(日期时间选择器)
得到我想要的结果:首先我选择人员,然后处理,然后从日期,然后到日期,然后单击查看按钮。它应该根据我选择的上述标准显示来自 MS-Access 的数据。
我尝试了以下方法:
代码: 1. 对于人员和流程过滤器:
DataView DV = new DataView(dt1);
DV.RowFilter = string.Format("[Person] LIKE '%{0}%'", textBox5.Text);
dataGridView1.DataSource = DV;
- 对于两个日期之间的日期时间,我尝试了很多和谷歌很多但没有找到答案。我在下面尝试过:
一)
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
string queryString = "";
queryString = "SELECT * FROM Table1 WHERE dob BETWEEN @startdate AND @enddate";
System.Data.SqlClient.SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand(queryString, con);
sqlCmd.Parameters.Add("@startdate", System.Data.SqlDbType.Date).Value = textBox7.Text;
sqlCmd.Parameters.Add("@enddate", System.Data.SqlDbType.Date).Value = textBox8.Text;
System.Data.SqlClient.SqlDataAdapter dataAdapter = new System.Data.SqlClient.SqlDataAdapter(sqlCmd);
System.Data.DataSet dataSet = new System.Data.DataSet();
dataAdapter.Fill(dataSet);
GridView1.DataSource = dataSet;
GridView1.DataBind();
b)
string FD = "";
FD = dateTimePicker4.Value.ToString("dd-MM-yyyy");
string TD = "";
TD = dateTimePicker5.Value.ToString("dd-MM-yyyy");
connection.Close();
connection.Open();
OleDbCommand command123 = new OleDbCommand();
command123.Connection = connection;
string query123 = "select * from Table1 where [P Date] between date '"# + dateTimePicker4.Text.ToString() + #"' and date '"# + dateTimePicker5.Text.ToString() + #"'"
command123.CommandText = query123;
OleDbDataAdapter da123 = new OleDbDataAdapter(command123);
DataTable dt123 = new DataTable();
da123.Fill(dt123);
dataGridView1.DataSource = dt123;
c)
DataTable dt = new DataTable();
da.Fill(dt);
DataView DV = new DataView(dt1);
DV.RowFilter = string.Format("[P Date] >=" + textBox7.Text + " and <" + textBox8.Text + "");
dataGridView1.DataSource = DV;
d)
DataTable dt1 = new DataTable();
DataView DV = new DataView(dt1);
//DV.RowFilter = "[P Date] IN (#11/01/2019#, #11/11/2019#)";
//DV.RowFilter = "[P Date] >=#"+dateTimePicker4.Text+"# and [P Date] <=#"+dateTimePicker5.Text+"#";
//dataView.RowFilter = "Date IN (#12/31/2008#, #1/1/2009#)" // date time values
//DV.RowFilter ="([P Date] >=CDate('dateTimePicker4.Text')) and ([P Date] <=CDate('dateTimePicker5.Text'))";
//DV.RowFilter = string.Format(CultureInfo.InvariantCulture.DateTimeFormat, "([P Date]>=#{dateTimePicker4.text}#) and ([P Date] <=#{dateTimePicker5.Text}#)");
dataGridView1.DataSource = DV;
For connection I am trying OLEDB connection.
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=DatabasePath.accdb;
Jet OLEDB:Database Password=password";
connection.Close();
connection.Open();
OleDbCommand command1 = new OleDbCommand();
command1.Connection = connection;
//Select all column use belw query
string query = "select * from Table1";
command1.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(command1);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
connection.Close();
this code is working but problem is that it shows all data not based on criteria based data.
I expect the output in Datagridview based on all criteria. Show only data which falls under all criteria.
【问题讨论】:
-
dob列在访问的是字符串还是日期时间对象?
-
@jdweng:不,它是 DdMmyyy 格式。首先它是在文本中,但没有得到结果,所以改为 ddmmyyy。但没有得到结果。
-
如何回答“否”的 A 或 B 问题?我不知道你最后的回应!!!
-
抱歉混淆..在数据库日期列格式是文本,但我没有得到结果,所以我将其更改为 DDMMYyy 但不幸的是我没有得到结果。所以目前的日期格式是 MMddyyy。
-
更改格式只是在单元格是 DateTime 对象(不是字符串)时更改显示格式。如果数据库中的列是字符串,则需要将字符串与字符串进行比较(不是带有 DateTime 的字符串,或带有字符串的 DateTime)。你需要比较苹果和苹果,而不是苹果和橙子。
标签: c# datagridview ms-access-2010 oledbconnection oledbcommand