C# string 数组 每个元素 加上单引号,每一个都被包含在单引号内

在拼接SQL的时候经常会遇到此类问题,尤其是in查询的时候,内容是一段 单引号的 字符的时候

strWhere += " a.EC101_WRYBH  IN (" + string.Join(",",codes.ToArray()) + ")";
codes 是list,这样执行出来是 "a.EC101_WRYBH  IN (a,b,c)"
 
string strs = "1,2,3,4,5,6";
string s1= string.Format("‘{0}‘", strs.Replace(",", "‘,‘"));
技术分享
 
string s1 = "1,2,3,4,5,6";
string s1_1 = s1.Replace(",", "‘,‘"); //返回结果为:1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6
string s2 = string.Format("‘{0}‘", s1_1);//把 ‘{0}‘中的 {0}替换为 1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6 这样结果就是 想得到的结果了

 

 
文章来自:http://www.cnblogs.com/wangjunwei/p/4523739.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3