大家好,彼人遇到一个问题,就是用SqlCommand调用存储过程时候,传一个参数对象,该参数对象的值中间加了个单引号,结果却报语法错误.参数对象不是把这些注入式错误过滤掉的吗,为什么在使用存储过程的时候却不起作用,除了方法字符串Replace("'","''")还有什么方法可以解决这个问题呢?因为我要调用很多个存储过程!  在线等,解决即送分,谢谢各位大侠…………  :)

解决方案 »

  1.   

    传递参数的页面就要做过滤处理的。
    public static string InputText(string inputString, int maxLength) 
    {
    StringBuilder retVal = new StringBuilder(); // check incoming parameters for null or blank string
    if ((inputString != null) && (inputString != String.Empty)) 
    {
    inputString = inputString.Trim(); //chop the string incase the client-side max length
    //fields are bypassed to prevent buffer over-runs
    if (inputString.Length > maxLength)
    inputString = inputString.Substring(0, maxLength); //convert some harmful symbols incase the regular
    //expression validators are changed
    for (int i = 0; i < inputString.Length; i++) 
    {
    switch (inputString[i]) 
    {
    case '"':
    retVal.Append("&quot;");
    break;
    case '<':
    retVal.Append("&lt;");
    break;
    case '>':
    retVal.Append("&gt;");
    break;
    default:
    retVal.Append(inputString[i]);
    break;
    }
    } // Replace single quotes with white space
    retVal.Replace("'", " ");
    } return retVal.ToString();
    }
      

  2.   

    难道一定要做过滤处理吗?有没有方法不做呢?
    但是有一种情况,如果不用存储过程,直接在SqlCommand中写sql语句,再传参数对象(值包含非法的字符串)进去就不会出现这个问题,SqlParameter自动过滤掉这些注入式错误的,请问这个是怎么回事情呢?二楼的,谢谢 :)
      

  3.   

    cmd.Parameters.Add("@a",a);这样可以..
      

  4.   

    new SqlParameter prmValue=new SqlParameter("@参数名",SqlType.参数类型,参数类型大小)
    prmValue.Value=你传递的值