C # parameterized execution of SQL statements to prevent vulnerability attacks this article takes MySql as an example [20151108 non-query operations ],

Source: Internet
Author: User

C # parameterized execution of SQL statements to prevent vulnerability attacks this article takes MySql as an example [20151108 non-query operations ],

Why do we need to execute SQL statements in parameters?

One function is to prevent user injection vulnerabilities.

Let's just give a column.

For example, if the account and password are used for logon,

Simply write the data from the database to find the data with the same id and pw as the user input.

SQL: select id, pw where id = 'inputtid' and pw = 'inputpw ';

Generally, there is no problem. However, if the user input id or PW tape ', this may cause a vulnerability and a bug.

For example, the id entered by the user is: 1 'or '1' = '1

This is the SQL statement execution: select id, pw where id = '1' or '1' = '1' and pw = 'inputpw ';

All the accounts and passwords in the database meet this condition.

In short, you can use 'to change the execution of your SQL statement.

Parameterization can avoid this problem.

 

 

 

/************************ Non-query operations ************ **************************** // It is too late today, write a non-query operation first, and write the query operation tomorrow. /***** Function ***** // <summary> // add, delete, and modify data, and the number of affected rows is returned, -1 // </summary> /// <param name = "SQL"> SQL statement </param> /// <param name = "ps"> parameter </param> /// <returns> Number of affected rows returned </returns> static string connStr = "server = IP; user Id = Account Name; password = password; Database = table name "; public static int ExecuteNonQuery (string SQL, params MySqlParameter [] ps) {using (MySqlConnection conn = new MySqlConnection (connStr) {using (MySqlCommand cmd = new MySqlCommand (SQL, conn) {cmd. parameters. addRange (ps); conn. open (); return cmd. executeNonQuery (); // returns the number of affected rows }}/ ***** application example *****/public void InsertData () {int cid = 1, aid = 2; string name = "hha"; string SQL = "insert into tb_compart (compartID, compartName, areaID) values (@ compartID, @ compartName, @ areaID );"; mySqlParameter [] ps = {new MySqlParameter ("@ compartID", (object) cid), new MySqlParameter ("@ compartName", name), new MySqlParameter ("@ areaID ", (object) aid)}; int r = Mysql. mySQLHelper. executeNonQuery (SQL, ps); // The r value is the number of affected rows. Execution failed r =-1 ;}}

 

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.