SQL string filter detects dangerous characters

Source: Internet
Author: User
Tags bool chr

A C # SQL database string manipulation function that enables filtering of SQL strings, detecting SQL for dangerous characters, correcting escape characters in SQL statements, and ensuring that SQL is not injected:

SQL string Filter function:

01public static bool Processsqlstr (string Str)

229

BOOL ReturnValue = true;

Try

05 {

if (Str.trim ()!= "")

07 {

Sqlstr = "exec|insert+|select+|delete|update|count|chr|mid|master+

|truncate|char|declare|drop+|drop+table|creat+

|create|*|iframe|script| ";

Sqlstr = "Exec+|insert|delete+|update+|count (|count+|chr+|+mid

(|+mid+|+master+|truncate+|char+|

+char (|declare+|drop+table|creat+table ";

string[] Anysqlstr = sqlstr.split (' | ');

One foreach (string ss in Anysqlstr)

12 {

Str.tolower (). INDEXOF (ss) >= 0)

14 {

ReturnValue = false;

break;

17}

18}

19}

20}

Catch

22 {

ReturnValue = false;

24}

ReturnValue return;

26}

The following is the detection of an illegal dangerous character in an SQL statement:

View sourceprint?01///

02///Detect SQL Risk characters

03///

04///to judge a string

05///Judgment Results

06public static bool Issafesqlstring (String str)

279

Return! Regex.IsMatch (str, @ "[-|;|,|/| (|)] | [|]|}| {|%| @|*|!|']");

09}

10///

11///to correct escape characters in SQL statements

12///

13public static string Mashsql (String str)

14{

String str2;

if (str = null)

17 {

STR2 = "";

19}

Else

21 {

str = str. Replace ("'", "" ");

str2 = str;

24}

STR2 return;

}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.