471,321 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,321 software developers and data experts.

How to rewrite this snippet if you must implement IDisposable

How to rewrite this snippet if you must implement IDisposable
private static void OpenConnection()

{

string connectionString = GetConnectionString();

StringBuilder errorMessages = new StringBuilder();

SqlConnection connection = new
SqlConnection(connectionString);

try

{

connection.Open();

Console.WriteLine("ServerVersion: {0}",
connection.ServerVersion);

Console.WriteLine("State: {0}", connection.State);

}

catch (SqlException ex)

{

for (int i = 0; i < ex.Errors.Count; i++)

{

errorMessages.Append("Index #" + i +
"\n" +

"Message: " + ex.Errors[i].Message +
"\n" +

"LineNumber: " + ex.Errors[i].LineNumber +
"\n"
+

"Source: " + ex.Errors[i].Source +
"\n" +

"Procedure: " + ex.Errors[i].Procedure +
"\n");

}

Console.WriteLine(errorMessages.ToString());

connection.Close();

}

Feb 7 '06 #1
3 1810
Why do you need to implement IDisposable? You are not storing the
connection. Rather, I would move the call to close into a finally clause,
and then check for null before calling close.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ka*********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
How to rewrite this snippet if you must implement IDisposable
private static void OpenConnection()

{

string connectionString = GetConnectionString();

StringBuilder errorMessages = new StringBuilder();

SqlConnection connection = new
SqlConnection(connectionString);

try

{

connection.Open();

Console.WriteLine("ServerVersion: {0}",
connection.ServerVersion);

Console.WriteLine("State: {0}", connection.State);

}

catch (SqlException ex)

{

for (int i = 0; i < ex.Errors.Count; i++)

{

errorMessages.Append("Index #" + i +
"\n" +

"Message: " + ex.Errors[i].Message +
"\n" +

"LineNumber: " + ex.Errors[i].LineNumber +
"\n"
+

"Source: " + ex.Errors[i].Source +
"\n" +

"Procedure: " + ex.Errors[i].Procedure +
"\n");

}

Console.WriteLine(errorMessages.ToString());

connection.Close();

}

Feb 7 '06 #2
or - simpler - just be "using" the new SqlConnection (rest of code as-is):

using(SqlConnection connection = new SqlConnection(connectionString)) {
try {
connection.Open();
// blah...
}
// blah...
}

(might also want to be mindful of swallowing too many SqlExceptions and
missing something important...)

Marc
Feb 7 '06 #3
Hi Nicholas,
I am just asking to see if I can force Dispose on a sql connetion. I
like the finnaly approach cause I already went into a try catch
scenario, but I am stil ltrying to force dispose on it, Any help would
be appreciated.

Feb 7 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Billy Porter | last post: by
15 posts views Thread by Sam Sungshik Kong | last post: by
6 posts views Thread by greenxiar | last post: by
3 posts views Thread by =?ISO-8859-1?Q?=22Andr=E9s_G=2E_Aragoneses_=5B_kno | last post: by
4 posts views Thread by Harsh Trivedi | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.