SqlCommand parameterized queries

Source: Internet
Author: User

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 of assigning a value directly? For example, the user enters "1 ' or ' 1 ' = ' 1 '" in the Tbxh (TextBox property name);

This will cause this SQL statement to always be established, such as delete from XSB where XH = ' 1 ' or ' 1 ' = ' 1 ' will result in the deletion of all records in the table

How to solve it?

Using parameterized Queries:

Com.commandtext = "Delete from XSB where XH = @XH";

Com. Parameters.Add (New SqlParameter ("@XH", Tbxh.text));

The following SQL statements are available as parameterized queries

"Delete from XSB where XH = @XH"

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

"Select.....where = @ ..."

"Update ... set age = @ ..."

SqlCommand parameterized queries

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.