How to solve issue of single quote in filer value using DataTable.Select()

后端 未结 3 996
慢半拍i
慢半拍i 2021-01-22 05:18

Assuming I want productCode variable below to be assigned the product code of a product named Cookie\'s

NOTE the \"\'\" in the the Prod

3条回答
  •  醉酒成梦
    2021-01-22 05:31

    Use LINQ, this will make your solution simplier and more general. Otherwise you will end up with replacing single quotes (and other special characters) in the filter with escape chars.

    DataTable dt=PullSomeDataFromProductTableDatabase();
    string filterValue="Cookie's";
    string productCode=dt.AsEnumerable()
                         .Where(row => row.Field("ProductName") == filterValue)
                         .FirstOrDefault(row => row.Field("ProductCode"));
    

提交回复
热议问题