Can we add parameter in datatable.select in c#

坚强是说给别人听的谎言 提交于 2019-12-03 16:19:20

You can use String.Format, you need to escape single quotes with two:

string query = string.Format("Name='{0}'", name.Replace(@"'", "''"));
var rows = dt.Select(query);

or, if you want to use Like:

string query = string.Format("Name LIKE '%{0}%'", name.Replace(@"'", "''"));

(note that a DataTable is not vulnerable to sql-injection since it's an in-memory object)

You can pass only expression to Select method.

In case if you need to pass the parameter dynamically then you can try this.

string Exp = "Name ='" + variable + "'";

dt.select(Exp);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!