将参数传递给.fromsql

时间:2018-10-09 07:47:45

标签: c# entity-framework sql-like

我无法弄清楚这个问题,我感到沮丧。我想对数据库中的人进行基本搜索,以下工作正常

 IQueryable<VwSomeView> dbresult = db.vwSomeView.FromSql("select * from vwSomeView where firstname like '%" + searchfor + "%' or lastname like '%" + searchfor + "%'");

但这很糟糕,因为可能会进行SQL注入。因此,我尝试了此操作(因为我在调用存储过程之前就使用了它,并且运行良好)

IQueryable<VwSomeView> dbresult = db.vwSomeView.FromSql("select * from vwSomeView where firstname like '%{0]%' or lastname like '%{0}%'",searchfor);

无法正常工作。我尝试使用 SqlParameter

SqlParameter para = new SqlParameter("search", searchfor);
IQueryable<VwSomeView> dbresult = db.vwSomeView.FromSql("select * from vwSomeView where firstname like '%@search%' or lastname like '%@search%'",para);

那也不起作用。

有人可以告诉我我做错了什么吗? 谢谢

2 个答案:

答案 0 :(得分:3)

将@search用作SQL中的变量(而不是字符串中的变量),它应该可以工作。

UnhandledPromiseRejectionWarning

答案 1 :(得分:3)

当然,我在发布问题后仅10分钟就知道了。 这可以正常工作

with open()