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.