Do I have to Dispose the SQLiteCommand objects?

后端 未结 4 1918
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-18 08:33

How do I treat the SQLiteCommand object, do I have to call Dispose() after ExecuteScalar, ExecuteNonQuery

4条回答
  •  渐次进展
    2021-01-18 08:46

    Just do this:

    using(var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString))
    using(var command = connection.CreateCommand())
    {
       command.CommandText = "...";
       connection.Open();
       command.ExecuteNonQuery();
    }
    

    Not calling dispose on the command won't do anything too bad. However calling Dispose on it will supress the call to the finalizer, making calling dispose a performance enhancement.

提交回复
热议问题