如何使用params运行访问查询

时间:2011-08-01 14:04:44

标签: c# ms-access

我有使用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();

如何在两个日期之间找到的运行查询中更改此内容?

类似于:select * from ConfTbl where Tdate >= #MyFrom# and Tdate <= #MyTo#

我试试这个:

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.CommandText = SQL;
        Cmd.ExecuteNonQuery();
        adp = new OleDbDataAdapter(SQL, Conn);
        adp.Fill(dsView, "ReturnConfTbl");
        adp.Dispose();
        this.dataGridView3.DataSource = dsView.Tables[0].DefaultView;

并收到此错误:Data type mismatch in criteria expression

2 个答案:

答案 0 :(得分:3)

您可以使用BETWEEN关键字。例如

SELECT *
FROM ConfTbl
WHERE 
    Tdate BETWEEN #MyFrom# AND #MyTo#

答案 1 :(得分:0)

只需查看语句之间的SQL .. http://msdn.microsoft.com/en-us/library/ms187922.aspx