Charles Baldo
asked on
SQL Server C# using over try/catch block advatages
Hello,
I am performing some SQL procedures in C#. I am wondering what is the best approach as far as memory and frameworks management.
I have two approaches using() and try Catch finally. My questions are what does using() have encapsulated in it to dispose
of connections, commands, readers etc. Am I just as good using try catch annd cleaning up and disposing of my object smyself?
is the only hit on performance codeing size or is there other advantages using using() over other methods?
I have attached some code snippets. If anyone has any suggestions to improve performance, readablility I would appreciate it.
I am performing some SQL procedures in C#. I am wondering what is the best approach as far as memory and frameworks management.
I have two approaches using() and try Catch finally. My questions are what does using() have encapsulated in it to dispose
of connections, commands, readers etc. Am I just as good using try catch annd cleaning up and disposing of my object smyself?
is the only hit on performance codeing size or is there other advantages using using() over other methods?
I have attached some code snippets. If anyone has any suggestions to improve performance, readablility I would appreciate it.
ConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=MetaData;Integrated Security = SSPI;";
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("select aFileName from dbo.asset_Table", conn))
{
SqlDataReader objRead;
conn.Open();
objRead = cmd.ExecuteReader();
while (objRead.Read())
{
Console.WriteLine(objRead["aFileName"]);
}
}
conn.Close();
}
************************ Try Catch finally method
string ConnectionString;
ConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=MetaData;Integrated Security = SSPI;";
SqlConnection conn = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand("select aFileName from dbo.asset_Table", conn);
SqlDataReader objRead;
try
{
conn.Open();
objRead = cmd.ExecuteReader();
while (objRead.Read())
{
Console.WriteLine(objRead["aFileName"]);
}
objRead.Close();
objRead.Dispose();
}
catch (SqlException sqlEx)
{
Console.WriteLine("SqlException Handle :{0}", sqlEx.ToString());
}
finally
{
conn.Close();
conn.Dispose();
cmd.Dispose();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER