Maurizio
Maurizio

Reputation: 17

asp.net dispose inside a class

Before return dt i want destroy sda

How destroy an Object with Dispose - inside the class dbConnect ?

  public class dbConnect
    {

    // othee code 

    public DataTable SetQuery(string constr, DataTable dt, string sSql)
        {              
            using (MySqlConnection con = new MySqlConnection(constr))
            {
                //sda = null
                using (MySqlDataAdapter sda = new MySqlDataAdapter())
                    {
                        try
                        {
                            cmd.Connection = con;
                            sda.SelectCommand = cmd;
                            sda.Fill(dt);
                        }

                        finally
                        {
                            if (sda != null)
                                sda.Dispose();
                            //why the sda is not = null ?
                        }

                        return dt;
                    }             
            }              
        }

What is the correct procedure for destroy object (not class) ?

Upvotes: 1

Views: 54

Answers (2)

Haytam
Haytam

Reputation: 4733

Upvotes: 3

Christos
Christos

Reputation: 53958

Since you make use of the using statement for sda, the Dispose would be called under the hood once you don't need it anymore.

For further info please have look here.

Upvotes: 3

Related Questions