100分求按,号分割字符串并给每段加上单引号的代码...string sql = "select * from 表A where TestID in ('" + id + "')";
当id为一个ID的时候 查询语句正确查询 可是当id为多个的时候 比如001,002 这样查询语句就变成了
string sql = "select * from 表A where TestID in ('001,002')"; 这样查询不出数据。所以我想在传ID这个值之前先把ID写成'001','002'这样的  比如说 string a="001,002"  我想让a的值变成变成 string a="'001','002'"; 这个样子的 请问如何写啊??
同样a="001" → a="'001'"    a="001,002,003,004" → a="'001','002','003','004'"

解决方案 »

  1.   


    string a="001,002";
    a=a.replace(",","','");
      

  2.   

    顶2楼  replace完了以后别忘记了 前后都加一个单引号~
      

  3.   

    string a = "001,002"; 
    string[] arr = a.Split(','); 
    string tempa = ""; 
    if(arr.Length == 0)
    {
       tempa = "'"+a+"',";
    }
    else{
    foreach (string ch in arr) 

       tempa += "'"+ch+"',";; 

    }
    tempa = tempa.Remove(tempa.Length-1);
      

  4.   

    private void NewMethod()
        {
            string str = "001,002,003";
            string conStr = "";
            char[] ch = new char[] { ',' };
            string[] strArr = str.Split(ch);
            foreach (string s in strArr)
            {
                conStr += "'" + s + "',";
            }
            conStr = conStr.Remove(conStr.Length - 1, 1);
            Response.Write(conStr);
        }已经验证通过了