C # and SQL inject string attacks and prevent injection of word representable attacks

Source: Internet
Author: User

--in SQL--Create Ren's database and insert a messageCreate DatabaseRenGo UseRenGoCreate TableXinxi (Codenvarchar( -)Primary Key,--numberingNamenvarchar(50)--name)Insert  intoXinxiValues('1001','Zhangsan')
             for (; ; ) {                   BOOLb =false;//using intermediate variablesConsole.Write ("Please enter the number you want to modify:"); stringNo =Console.ReadLine (); //Search ShowSqlConnection zhancnn =NewSqlConnection ("server=.; Database=ren;user=sa;pwd=123");//Connection//Statement of OperationSqlCommand Zhancmd =zhancnn.                CreateCommand (); Zhancmd.commandtext="SELECT * from Xinxi where code= '"+ No +"'"; //statements that perform actionszhancnn.                Open (); SqlDataReader SS=Zhancmd.                ExecuteReader (); if(SS. HasRows)//whether there is data to be modified in the database{b=true; } zhancnn.                Close (); if(b = =true)//If you have the data you want to modify{Console.Write ("Find the ""+ No +"", please enter the name you want to modify:"); stringMingzi =Console.ReadLine (); Zhancmd.commandtext="Update Xinxi set name= '"+mingzi+"' where code= '"+no+"'"; Zhancnn.                    Open (); Zhancmd.                    ExecuteNonQuery (); Zhancnn.                    Close (); Console.WriteLine ("The changes are complete! ");  Break; }                Else//If you don't have the data you want to modify{Console.WriteLine ("The database does not have this message, please enter the correct code!! "); }} console.readline ();

When executing, note that I'm going to enter:

Then query the database, query all, become

To prevent this injection of text attacks, we need:

//in C # for (; ; ) {                   BOOLb =false;//using intermediate variablesConsole.Write ("Please enter the number you want to modify:"); stringNo =Console.ReadLine (); //Search ShowSqlConnection zhancnn =NewSqlConnection ("server=.; Database=ren;user=sa;pwd=123");//Connection//Statement of OperationSqlCommand Zhancmd =zhancnn.                CreateCommand (); Zhancmd.commandtext="SELECT * from Xinxi where code= '"+ No +"'"; //statements that perform actionszhancnn.                Open (); SqlDataReader SS=Zhancmd.                ExecuteReader (); if(SS. HasRows)//whether there is data to be modified in the database{b=true; } zhancnn.                Close (); if(b = =true)//If you have the data you want to modify{Console.Write ("Find the ""+ No +"", please enter the name you want to modify:"); stringMingzi =Console.ReadLine (); Zhancmd.commandtext = "update xinxi Set [email protected] where [email protected]; " ;//@ variable name: Placeholder. Note: [email protected] No quotes Zhancmd. Parameters.clear ();//must first empty all contents zhancmd. Parameters.Add ( "@mingzi", Mingzi);//similar to a hash table. The first value is arbitrarily taken, must be consistent with the above, and the second is the variable zhancmd. Parameters.Add ("@no"  , no); Zhancnn.                    Open (); Zhancmd.                    ExecuteNonQuery (); Zhancnn.                    Close (); Console.WriteLine ("The changes are complete! ");  Break; }                Else//If you don't have the data you want to modify{Console.WriteLine ("The database does not have this message, please enter the correct code!! "); }} console.readline ();
//If the same content was used in the execution window input, the result of the output is to process the "injected code" and "name to change" as a whole string.

C # and SQL inject string attacks and prevent injection of word representable attacks

Related Article

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.