Use of sqlcommand. Prepare Method

Source: Internet
Author: User

This method can improve the execution efficiency of statements to be executed repeatedly. When using this method, you must name the three parameters of parameters. Otherwise, an exception occurs.

See the followingCode:

 

Code Private   Void Button#click ( Object Sender, eventargs E)
{
Stopwatch SW =   New Stopwatch ();
Sw. Start ();
Using (Sqlconnection con = Getcon ())
{
Con. open ();
Sqlcommand com =   New Sqlcommand ( Null , Con );
Com. commandtext =   " Insert into tb_user ([username], [Password]) values (@ username, @ password) " ;
Com. Parameters. Add ( " @ Username " , Sqldbtype. nchar, 20 );
Com. Parameters. Add ( " @ Password " , Sqldbtype. nchar, 20 );
Com. Prepare ();
For ( Int I =   0 ; I <   1 ; I ++ )
{
Com. Parameters [ 0 ]. Value =   " Username "   + I;
Com. Parameters [ 1 ]. Value =   " Password "   + I;
Com. executenonquery ();
}
}
Sw. Stop ();
This . Label1.text = Sw. elapsedmilliseconds. tostring ();
}
Private Sqlconnection getcon ()
{
Sqlconnection con =   New Sqlconnection ( " Data Source = itsw; initial catalog = cmsdemo; user id = sa; Password = xxsecr " );
Return Con;
}

 

After running for ten times, the average result obtained on my computer is about 4150.

If this method is not used, the average result is about 4500.

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.