Performance Study in Microsoft. applicationdatablock. sqlhelper

Source: Internet
Author: User

Supported by Nova Outsourcing

I recently got interested in Microsoft. applicationdatablock. sqlhelper. executenonquery as it was mentioned by an umbraco developer in my team. I find the method allows programmers to assign parameters in a quite flexible way. it is defined as following.

 
Public static intExecutenonquery (StringConnectionstring,StringSpname,Params object[] Parametervalues );

As the parameter parametervalues is defined with Params key word, the number of the parameter value is dynamic according to the parameter number defined in the stored procedure.

 

it is like a proxy to stored procedure in C # code. the parameters in parametervalues array shoshould has the same sequence as that in stored procedure. let's say, we have a stored procedure defined as following.

  alter procedure   [DBO]  .   [insertt1] @ ID   int  ,  @ description   varchar   ( 50 )   asbegin insert into   t1   (  id  ,   description  )   values   (  @ ID  ,   @ description  )   end  

The following code invoke sqlhelper. executenonquery to invoke the stored procedure eventually.

 
Static voidExecutesqlhelperincorrectorder (){Sqlhelper. Executenonquery (_ connstr,"Insertt1", 1,"Executesqlhelperincorrectorder");}

The code above looks quite concise and flexible, right? We name it concise because it has the same sequence of parameters as that defined in stored procedure. we name it flexible because no matter how many parameters defined in the stored procedure, sqlhelper. executenonquery can always contain them. however, there is no free lunch in the world. it brings us big convenience, but in the meanwhile, it steals performance.

 

I wrote a little helper function for invoking stored procedure in devlib3. the code goes as following. it is not as concise as sqlhelper. executenonquery. the parameters are passed to the stored procedure by invoking addparameterwithvalue method.

 
Static voidExecutefromsphelper (){Using(SqlsphelperSP =NewSqlsphelper(_ Connstr,"Insertt1") {Sp. addparameterwithvalue ("ID", 1); sp. addparameterwithvalue ("Description","Executefromsphelper"); Sp. executewithnonquery ();}}

Here is a complete code sample to compare the performance.

 Using System; Using System. configuration; Using Microsoft. applicationblocks. Data; Using System. diagnostics; Using Dev3lib. SQL; Namespace Testproject { Class  Program { Static string _ Connstr = Configurationmanager . Connectionstrings [0]. connectionstring; Static void Main ( String [] ARGs ){Stopwatch Watch = New  Stopwatch (); Watch. Start (); executefromsphelper (); watch. Stop (); Console . Writeline ( String . Format ( "Executefromsphelper executes for {0} ticks" , Watch. elapsedticks); watch. Restart (); executefromsphelper (); watch. Stop (); Console . Writeline ( String . Format ( "Executefromsphelper executes for {0} ticks" , Watch. elapsedticks); watch. Restart (); executesqlhelperincorrectorder (); watch. Stop (); Console . Writeline ( String . Format ( "Executesqlhelperincorrectorder executes for {0} ticks" , Watch. elapsedticks); watch. Restart (); executesqlhelperincorrectorder (); watch. Stop (); Console . Writeline ( String . Format ( "Executesqlhelperincorrectorder executes for {0} ticks" , Watch. elapsedticks )); Console . Read ();}Static void Executesqlhelperincorrectorder (){ Sqlhelper . Executenonquery (_ connstr, "Insertt1" , 1, "Executesqlhelperincorrectorder" );} Static void Executesqlhelperinwrongorder (){ Sqlhelper . Executenonquery (_ connstr, "Insertt1" , "Executesqlhelperinwrongorder" , 1 );} Static void Executefromsphelper (){Using ( Sqlsphelper SP = New  Sqlsphelper (_ Connstr, "Insertt1" ) {Sp. addparameterwithvalue ( "ID" , 1); sp. addparameterwithvalue ( "Description" , "Executefromsphelper" ); Sp. executewithnonquery ();}}}}
 
 
 
 

The result goes as following.

Executefromsphelper executes for 394664 ticks
Executefromsphelper executes for 6973 ticks
Executesqlhelperincorrectorder executes for 75625 ticks
Executesqlhelperincorrectorder executes for 10209 ticks

 

From the result, we can judge that the performance of sqlsphelper is better than that of sqlhelper's. However, the invoking code of sqlhelper is more concise than that of sqlsphelper's.

Note: executefromsphelper is invoked two times here because the connection pool will take time to be initialized in the first invoking. executesqlhelperincorrectorder is invoked two times here because the parameters information of the stored procedure will take time to be initialized in the first invoking.

 

I will appreciate if you have any feedback on the discussion.

 

supported by Nova outsourcing

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.