SQL statement security and query efficiency

Source: Internet
Author: User

Insertstr = "insert into userinfo (name, password, email, phone, mobile, post, address)

Values (''";

Insertstr + = This. _ name. Trim ()

; + "'',''";

Insertstr + = This. _ password. Trim () + "'',''";

Insertstr + = This. _ email. Trim () + "'',''";

Insertstr + = This. _ phone. Trim () + "'',''";

Insertstr + = This. _ mobile. Trim () + "'',''";

Insertstr + = This. _ post. Trim () + "'',''";

Insertstr + = This. _ address. Trim () + "'')";

 

1. efficiency problems

First look at the above sectionCode, The efficiency is too low. The efficiency of so many string connections is low enough. In addition, there is no need to add such trim.

2. Security

Same as above, I can do a lot of things with single quotes, such as running an xp_cmd command, you will be miserable.

So, how to write it? The above code can be changed to this:

Reference content is as follows:

 

String strsql = "insert into sometable (C1, C2, C3,...) values (@ C1, @ C2,

@ C3 ,...)"

Sqlcommand mycommand = new sqlcommand (strsql, myconn)

Try

{

Mycommand. Parameters. Add (New sqlparameters ("@ C1", sqldatatype. varchar, 20)

Mycommand. Parameters ["@ C1"]. value = This. _ name;

....

 

}

Catch (...)

...

 

This method can avoid inefficient string connections and use sqlcommand parameter validity detection to avoid invalid characters. This parameter method is pre-compiled with high efficiency.

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.