c# - using block for command object close connection? -


i bit confused on below coding. both same? if not, 1 best , why?

//two using blocks command , connection using (var cn = new oracleconnection(connectionstring)) using (var cmd = new oraclecommand(sql, cn)) { }   //one using block command , connection using (var cmd = new oraclecommand(sql, new oracleconnection(connectionstring))) { } 

first assuming accidently added ) (otherwise neither compile) first example dispose of both command , connection second dispose of command.

here equivalent code first.

oracleconnection cn = new oracleconnection(connectionstring); try {     oraclecommand cmd = new oraclecommand(sql, cn);     try     {         // use cmd , cn here.     }         {         if(cmd != null)             ((idisposable)cmd).dispose();     } } {     if(cn != null)         ((idisposable)cn).dispose(); } 

and second.

oraclecommand cmd = new oraclecommand(sql, new oracleconnection(connectionstring)); try {     // use cmd here. } {     if(cmd != null)         ((idisposable)cmd).dispose(); } 

also note can create multiple instances disposed in 1 using if same type. following example msdn page using shows.

using (font font3 = new font("arial", 10.0f),             font4 = new font("arial", 10.0f)) {     // use font3 , font4. } 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -