Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
3
Reply
Closing an SqlConnection
Dy Oswald
Mar 14 2008 12:08 AM
3.4k
Reply
I don't know whether this function should (or could) be described as an 'optimized method'
Anyhow, need some advices regarding on how to close an SqlConnection without wasting too much resources
public object MethodName(){
object retVal=new object();
string sql="";
SqlConnection sConn=new SqlConnection();
SqlCommand sCmd=new SqlCommand();
try{
using (sConn=new SqlConnection(CONNECTION_STRING)){
sql="SELECT * FROM [TABLE_NAME]";
sCmd=new SqlCommand(sql, sConn);
sConn.Open();
try{
retVal=sCmd.ExecuteScalar();
}
catch{
}
}
return retVal;
}
catch{
throw;
}
finally{
if (sConn!=null)
sConn.Close();
}
}
Do I really have to put the code within the 'finally' scope?
My intention was to close the connection if there's any exception been thrown
Any advices/comments would be highly regarded
Upload Source Code
Select only zip and rar file.
Post
Reset
Cancel
Answers (
3
)
Next Recommended Forum
Creating Custom Forms in Visual Studio (To insert into database)
CurrencyManagers stay case sensitive