今天太丢人了,别人都知道加上防sql注入的方法,唯有我没想到,数据赤裸裸的放进了数据库。
bll层的StringClear.cs文件
 using System;
using System;
 using System.Collections.Generic;
using System.Collections.Generic;
 using System.Text;
using System.Text;
 using System.Text.RegularExpressions;
using System.Text.RegularExpressions;

 namespace sc.BLL
namespace sc.BLL
 {
{
 public class StringClear
    public class StringClear
 {
    {
 public StringClear()
        public StringClear()
 {}
        {}
 //
        //
 //    定义错误常量
        //    定义错误常量
 //
        //
 public const String OverMax = "长度超过预定数值,将被截取。";
        public const String OverMax = "长度超过预定数值,将被截取。";

 /// <summary>
        /// <summary>
 /// 判断是否为数字
        /// 判断是否为数字
 /// </summary>
        /// </summary>
 /// <param name="str"></param>
        /// <param name="str"></param>
 /// <returns></returns>
        /// <returns></returns>
 public static bool IsNum(string str)
        public static bool IsNum(string str)
 {
        {
 str = InputString(str, str.Length);
            str = InputString(str, str.Length);
 Regex reg = new Regex(@"^[1-9]\d*$");
            Regex reg = new Regex(@"^[1-9]\d*$");
 if (reg.IsMatch(str, 0))
            if (reg.IsMatch(str, 0))
 {
            {
 return true;
                return true;
 }
            }
 else
            else
 {
            {
 return false;
                return false;
 }
            }
 }
        }

 /// <summary>
        /// <summary>
 /// 判断是否为Email
        /// 判断是否为Email
 /// </summary>
        /// </summary>
 /// <param name="str"></param>
        /// <param name="str"></param>
 /// <returns></returns>
        /// <returns></returns>
 public static bool checkInput(string str, string userName)
        public static bool checkInput(string str, string userName)
 {
        {
 str = InputString(str, str.Length);
            str = InputString(str, str.Length);
 Regex reg = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
            Regex reg = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
 if (str != "" && reg.IsMatch(str, 0) && userName != "")
            if (str != "" && reg.IsMatch(str, 0) && userName != "")
 {
            {
 return true;
                return true;
 }
            }
 else
            else
 {
            {
 return false;
                return false;
 }
            }
 }
        }


 /// <summary>
        /// <summary>
 /// 截取字符串
        /// 截取字符串
 /// </summary>
        /// </summary>
 /// <param name="inputtext"></param>
        /// <param name="inputtext"></param>
 /// <param name="maxl"></param>
        /// <param name="maxl"></param>
 /// <returns></returns>
        /// <returns></returns>
 public static string CutString(string inputtext,int maxl)
        public static string CutString(string inputtext,int maxl)
 {
        {
 string strr = "";
            string strr = "";
 if ((inputtext != null) && (inputtext != string.Empty))
            if ((inputtext != null) && (inputtext != string.Empty))
 {
            {
 inputtext = inputtext.Trim();
                inputtext = inputtext.Trim();
 if (inputtext.Length > maxl)
                if (inputtext.Length > maxl)
 {
                {
 strr = inputtext.Substring(0, maxl);
                    strr = inputtext.Substring(0, maxl);
 }
                }
 else
                else
 {
                {
 strr = inputtext.ToString();
                    strr = inputtext.ToString();
 }
                }
 
                
 }
                }
 return strr;
            return strr;
 }
        }
 /// <summary>
        /// <summary>
 /// 防SQL注入方法
        /// 防SQL注入方法
 /// </summary>
        /// </summary>
 /// <param name="inputText">输入字符</param>
        /// <param name="inputText">输入字符</param>
 /// <param name="MaxLength">字符长度</param>
        /// <param name="MaxLength">字符长度</param>
 /// <returns></returns>
        /// <returns></returns>
 public static string InputString(string inputText, int MaxLength)
        public static string InputString(string inputText, int MaxLength)
 {
        {
 StringBuilder retVal = new StringBuilder();
            StringBuilder retVal = new StringBuilder();
 if ((inputText != null) && (inputText != string.Empty))
            if ((inputText != null) && (inputText != string.Empty))
 {
            {
 inputText = inputText.Trim();
                inputText = inputText.Trim();
 if (inputText.Length > MaxLength)
                if (inputText.Length > MaxLength)
 {
                {
 inputText.Substring(0, MaxLength);
                    inputText.Substring(0, MaxLength);
 }
                }
 for (int i = 0; i < inputText.Length; i++)
                for (int i = 0; i < inputText.Length; i++)
 {
                {
 switch (inputText[i])
                    switch (inputText[i])
 {
                    {
 case '"':
                        case '"':
 retVal.Append("""); break;
                            retVal.Append("""); break;
 case '<':
                        case '<':
 retVal.Append(""); break;
                            retVal.Append(""); break;
 case '>':
                        case '>':
 retVal.Append(">"); break;
                            retVal.Append(">"); break;
 case ')':
                        case ')':
 retVal.Append(""); break;
                            retVal.Append(""); break;
 case '(':
                        case '(':
 retVal.Append(""); break;
                            retVal.Append(""); break;
 case '/':
                        case '/':
 retVal.Append(""); break;
                            retVal.Append(""); break;
 case '\\':
                        case '\\':
 retVal.Append(""); break;
                            retVal.Append(""); break;
 default:
                        default:
 retVal.Append(inputText[i]); break;
                            retVal.Append(inputText[i]); break;
 }
                    }
 }
                }
 retVal.Replace(" ", "");
                retVal.Replace(" ", "");
 }
            }
 return retVal.ToString();
            return retVal.ToString();
 }
        }
 }
    }
 
    
 }
}
 
在text的值上加sc.BLL.StringClear.InputString(this.txtFaren.Text.ToString(),10) ;
进行了字符转换。
bll层的StringClear.cs文件
 using System;
using System; using System.Collections.Generic;
using System.Collections.Generic; using System.Text;
using System.Text; using System.Text.RegularExpressions;
using System.Text.RegularExpressions;
 namespace sc.BLL
namespace sc.BLL {
{ public class StringClear
    public class StringClear {
    { public StringClear()
        public StringClear() {}
        {} //
        // //    定义错误常量
        //    定义错误常量 //
        // public const String OverMax = "长度超过预定数值,将被截取。";
        public const String OverMax = "长度超过预定数值,将被截取。";
 /// <summary>
        /// <summary> /// 判断是否为数字
        /// 判断是否为数字 /// </summary>
        /// </summary> /// <param name="str"></param>
        /// <param name="str"></param> /// <returns></returns>
        /// <returns></returns> public static bool IsNum(string str)
        public static bool IsNum(string str) {
        { str = InputString(str, str.Length);
            str = InputString(str, str.Length); Regex reg = new Regex(@"^[1-9]\d*$");
            Regex reg = new Regex(@"^[1-9]\d*$"); if (reg.IsMatch(str, 0))
            if (reg.IsMatch(str, 0)) {
            { return true;
                return true; }
            } else
            else {
            { return false;
                return false; }
            } }
        }
 /// <summary>
        /// <summary> /// 判断是否为Email
        /// 判断是否为Email /// </summary>
        /// </summary> /// <param name="str"></param>
        /// <param name="str"></param> /// <returns></returns>
        /// <returns></returns> public static bool checkInput(string str, string userName)
        public static bool checkInput(string str, string userName) {
        { str = InputString(str, str.Length);
            str = InputString(str, str.Length); Regex reg = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
            Regex reg = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"); if (str != "" && reg.IsMatch(str, 0) && userName != "")
            if (str != "" && reg.IsMatch(str, 0) && userName != "") {
            { return true;
                return true; }
            } else
            else {
            { return false;
                return false; }
            } }
        }

 /// <summary>
        /// <summary> /// 截取字符串
        /// 截取字符串 /// </summary>
        /// </summary> /// <param name="inputtext"></param>
        /// <param name="inputtext"></param> /// <param name="maxl"></param>
        /// <param name="maxl"></param> /// <returns></returns>
        /// <returns></returns> public static string CutString(string inputtext,int maxl)
        public static string CutString(string inputtext,int maxl) {
        { string strr = "";
            string strr = ""; if ((inputtext != null) && (inputtext != string.Empty))
            if ((inputtext != null) && (inputtext != string.Empty)) {
            { inputtext = inputtext.Trim();
                inputtext = inputtext.Trim(); if (inputtext.Length > maxl)
                if (inputtext.Length > maxl) {
                { strr = inputtext.Substring(0, maxl);
                    strr = inputtext.Substring(0, maxl); }
                } else
                else {
                { strr = inputtext.ToString();
                    strr = inputtext.ToString(); }
                } 
                 }
                } return strr;
            return strr; }
        } /// <summary>
        /// <summary> /// 防SQL注入方法
        /// 防SQL注入方法 /// </summary>
        /// </summary> /// <param name="inputText">输入字符</param>
        /// <param name="inputText">输入字符</param> /// <param name="MaxLength">字符长度</param>
        /// <param name="MaxLength">字符长度</param> /// <returns></returns>
        /// <returns></returns> public static string InputString(string inputText, int MaxLength)
        public static string InputString(string inputText, int MaxLength) {
        { StringBuilder retVal = new StringBuilder();
            StringBuilder retVal = new StringBuilder(); if ((inputText != null) && (inputText != string.Empty))
            if ((inputText != null) && (inputText != string.Empty)) {
            { inputText = inputText.Trim();
                inputText = inputText.Trim(); if (inputText.Length > MaxLength)
                if (inputText.Length > MaxLength) {
                { inputText.Substring(0, MaxLength);
                    inputText.Substring(0, MaxLength); }
                } for (int i = 0; i < inputText.Length; i++)
                for (int i = 0; i < inputText.Length; i++) {
                { switch (inputText[i])
                    switch (inputText[i]) {
                    { case '"':
                        case '"': retVal.Append("""); break;
                            retVal.Append("""); break; case '<':
                        case '<': retVal.Append(""); break;
                            retVal.Append(""); break; case '>':
                        case '>': retVal.Append(">"); break;
                            retVal.Append(">"); break; case ')':
                        case ')': retVal.Append(""); break;
                            retVal.Append(""); break; case '(':
                        case '(': retVal.Append(""); break;
                            retVal.Append(""); break; case '/':
                        case '/': retVal.Append(""); break;
                            retVal.Append(""); break; case '\\':
                        case '\\': retVal.Append(""); break;
                            retVal.Append(""); break; default:
                        default: retVal.Append(inputText[i]); break;
                            retVal.Append(inputText[i]); break; }
                    } }
                } retVal.Replace(" ", "");
                retVal.Replace(" ", ""); }
            } return retVal.ToString();
            return retVal.ToString(); }
        } }
    } 
     }
}
在text的值上加sc.BLL.StringClear.InputString(this.txtFaren.Text.ToString(),10) ;
进行了字符转换。
 
                     
                    
                 
                    
                 
 
        

 
     
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号