how run access query with params
i have this code for update datetime fileds in access using params
SQL = "update ConfTbl set D_from = @MyFrom , D_to = @MyTo where Code = " + Convert.ToInt32(TmpCode1) + "";
OleDbCommand Cmd = Conn.CreateCommand();
OleDbParameter dateparam1 = Cmd.Parameters.AddWithValue("@MyFrom", DbType.DateTime);
dateparam1.Value = dt_From.Value;
OleDbParameter dateparam2 = Cmd.Parameters.AddWithValue("@MyTo", DbType.DateTime);
dateparam2.Value = dt_To.Value;
Cmd.CommandText = SQL;
Cmd.ExecuteNonQuery();
how to change this for run query that find between two dates ?
somthing like: select * from ConfTbl where Tdate >= #MyFrom# and Tdate <= #MyTo#
i try this:
SQL = @"select * from ReturnConfTbl where Tdate >= @MyFrom and Tdate <= @MyTo";
OleDbCommand Cmd = Conn.CreateCommand();
OleDbParameter dateparam1 = Cmd.Parameters.AddWithValue("@MyFrom", DbType.DateTime);
dateparam1.Value = dt_from_A.Value;
OleDbParameter dateparam2 = Cmd.Parameters.AddWithValue("@MyTo", DbType.DateTime);
dateparam2.Value = dt_to_A.Value;
Cmd开发者_Go百科.CommandText = SQL;
Cmd.ExecuteNonQuery();
adp = new OleDbDataAdapter(SQL, Conn);
adp.Fill(dsView, "ReturnConfTbl");
adp.Dispose();
this.dataGridView3.DataSource = dsView.Tables[0].DefaultView;
and got this error: Data type mismatch in criteria expression
.
You can use the BETWEEN
keyword. e.g.
SELECT *
FROM ConfTbl
WHERE
Tdate BETWEEN #MyFrom# AND #MyTo#
just have a look at the TSQL between statement.. http://msdn.microsoft.com/en-us/library/ms187922.aspx
精彩评论