带绑定变量的OleDbCommand

时间:2014-11-07 10:28:25

标签: c# database oracle oledbcommand

我正在尝试将绑定变量与OleDbCommand一起使用。我有以下代码

    var SQL = @"select * from table where order_no = '@ORDER_NO'";
    using (var cmd = new OleDbCommand(SQL, connection))
    {
        cmd.Parameters.Add(new OleDbParameter("@ORDER_NO", "1234"));
        using (var dr = cmd.ExecuteReader())
        {
            while (dr.Read())
            {
                // do something
            }
        }
    }

当我运行此代码时,不会从数据库返回任何内容。如果我将SQL更改为

var SQL = @"select * from table where order_no = '1234'";

返回结果。我做错了什么?

1 个答案:

答案 0 :(得分:3)

删除@ORDER_NO周围的撇号:

var SQL = @"select * from table where order_no = @ORDER_NO";

否则它不是参数而是字符串文字,而您正在寻找order_no = @ORDER_NO