我在这里尝试的是比较日期。运行此代码时不会显示错误,但同时代码也不会从数据库中获取任何数据。
代码如下所示:
string constr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Auto_Finance.accdb;Persist Security Info=False;";
OleDbConnection con = new OleDbConnection(constr);
con.Open();
string d1 = DateTime.Now.ToString("MM/dd/yyyy");
DateTime df6 = DateTime.Now;
int da1 = df6.Date.Month;
int yr = df6.Date.Year;
//DateTime df7 = new DateTime(df6.Year, df6.Month, df6.Day).AddYears(+1);
//string sql = @"select * from ins where mon<=[@mon] and yr<=[@yr]";
string sql = @"select * from ins where Insdate<=[@ddd]";
OleDbCommand cmd = new OleDbCommand(sql, con);
//cmd.Parameters.AddWithValue("@mon", da1);
//cmd.Parameters.AddWithValue("@yr", yr);
cmd.Parameters.AddWithValue("@ddd", d1);
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
对此问题的原因可能有任何澄清和指导。