Parameterized SQL statements to prevent SQL injection attacks

Source: Internet
Author: User
Tags ways to prevent sql injection

There are two ways to prevent SQL injection attacks:

1) The first is that all SQL statements are stored in the stored procedure, which not only avoids SQL injection, but also improves performance. In addition, the stored procedure can have a dedicated database administrator (DBA) writing and centralized management; however, this method sometimes has different query conditions for the same tables, and SQL statements may be different, so that a large number of stored procedures will be written. So there is the second query method,

2) SQL statements for parameterized Query


Example:

// Instantiate the connection object sqlconnection con = new sqlconnection ("database connection string "); // Add a database connection string // instantiate the command object sqlcommand cmd = new sqlcommand ("select * From userinfo where sex = @ sex and age = @ age", con ); // sqlcommand cmd = con. createcommand (); // cmd. commandtext = "select * From userinfo where sex = @ sex and age = @ age"; // The first example is cmd. parameters. addwithvalue ("@ sex", true); // example of sqlparameter parameter = new sqlparameter ("@ age", sqldbtype. INT); parameter. value = 30; cmd. parameters. add (parameter); // Add parameters // instantiate dataadapter sqldataadapter adapter = new sqldataadapter (CMD); datatable DATA = new datatable ();


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.