You do not need to set anything to null or nothing to .NET. This is automatically handled by the Garbage Collection .
It is imperative that Conn.Dispose () be called in both your error handling and if everything goes according to plan. The Try Try Catch block is great for this. Another alternative is to use the Using keyword when declaring your connection, so that the Connection object will be properly deleted no matter what happens.
Do not worry about combining your connections. If you open a new one every time you need to use it, that's fine. ADO.NET links connect you backstage. There is little performance if you reuse the same open connection object, but to keep things simple, don't worry about it.
You can do the same with the following code. The result will be the same. Regardless of whether this is an error or not, Conn will be handled properly. Any error will seep up, as before.
public void ExecuteNonQuery(SqlCommand Cmd) { Using (SqlConnection Conn = new SqlConnection(strConStr)); {
As you can see, when the only error handling you want / need is to make sure your connection objects are removed properly, Using syntax can make things tidy.
source share