过滤非法字符集函数一定程度上避免输入非法字符引起SQL语句的错误
在程序公共类库建立类,粘贴如下函数代码
代码
/// <summary>过滤非法符串
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string GetSafeSQL(string value)
{
if (string.IsNullOrEmpty(value))
return string.Empty;
value = Regex.Replace(value, @";", string.Empty);
value = Regex.Replace(value, @"'", string.Empty);
value = Regex.Replace(value, @"&", string.Empty);
value = Regex.Replace(value, @"%20", string.Empty);
value = Regex.Replace(value, @"--", string.Empty);
value = Regex.Replace(value, @"==", string.Empty);
value = Regex.Replace(value, @"<", string.Empty);
value = Regex.Replace(value, @">", string.Empty);
value = Regex.Replace(value, @"%", string.Empty);
return value;
}
在其他类中引用 key关键字 = xh.shop.Utility.tool.GetSafeSQL(key关键字);
//能一定程度上有效避免用户输入非法字符引起SQL语句的错误