Reputation: 17
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
Reputation: 4733
Dispose()
doesn't mean your variable will become null
, it's
you who chooses wheither your variable should stop referencing
something (so it's you who must set it to null
).Dispose()
when you use using
on a variable,
it will be called automatically at the end (see: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement).Upvotes: 3