1

Difference between connection.close and connection.dispose using Asp.net Connection String ? for ex:

Globals.dr.Dispose(); 
Globals.dr.Close(); 
Szymon
  • 42,577
  • 16
  • 96
  • 114
user3259635
  • 51
  • 1
  • 12

3 Answers3

0

Dispose() automatically calls .Close for SqlConnection. So you can only call Dispose or better use the using statement in C#.

using (var con = new SqlConnection()...)
{
  ....
}
0

When using Dispose() it automatically calls the Close() method on the Connection object

Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
0

Close actually closes the open connection to database. but dispose will remove the connection object from memory. since sqlconnection implements Finalize/Dispose pattern if you dispose connection before closing it, then close method is automaticly called.

Sql connection close method

basically dispose is about memory and close is about connection :D

SHM
  • 1,896
  • 19
  • 48
  • @Hassan-Thank u lot and lots – user3259635 Mar 09 '14 at 05:30
  • @Hassan-i have tried function in 2 redaers at the time of im getting this error" invalid attempt to call read when reader is closed" ? Thank in advance – user3259635 Mar 09 '14 at 05:32
  • take a look at this link: http://stackoverflow.com/questions/13455963/getting-invalid-attempt-to-call-read-when-reader-is-closed – SHM Mar 09 '14 at 05:36