SqlCommand parameterized query and SqlCommand parameterization

Source: Internet
Author: User

SqlCommand parameterized query and SqlCommand parameterization

String strcon = "Persist Security Info = False; User id = sa; pwd = lovemary; database = student; server = (local )";
SqlConnection SQL = new SqlConnection (strcon );
SQL. Open ();
SqlCommand com = new SqlCommand ();

Com. Connection = SQL;

Com. CommandText = "delete from XSB where XH = '" + tbXH. text + "'";

What is the problem caused by direct assignment? For example, you can enter "1" or "1" = "1" in tbXH (textbox attribute name).

This will cause this SQL statement to remain valid forever. For example, if delete from XSB where XH = '1' or '1' = '1', all records in the table will be deleted.

How can this problem be solved?

Parameterized query:

Com. CommandText = "delete from XSB where XH = @ XH ";

Com. Parameters. Add (new SqlParameter ("@ XH", tbXH. text ));

The following SQL statements can be parameterized for query:

"Delete from XSB where XH = @ XH"

"Insert into xsb (XH, XM, XB, CSRQ, ZY, ZXF) VALUES (@ Name, @ Age ,....)"

"Select... where = @.."

"Update... set Age = @.."

 

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.