---
title: "SqlConnection SqlCommand SqlDataReader IDisposable"  
description: "SqlConnection SqlCommand SqlDataReader IDisposable"  
author: "Anonymous User"  
published: 2013-06-19  
updated: 2013-06-19  
canonical: https://www.mindstick.com/forum/1209/sqlconnection-sqlcommand-sqldatareader-idisposable  
category: "ado.net"  
tags: ["ado.net"]  
reading_time: 2 minutes  

---

# SqlConnection SqlCommand SqlDataReader IDisposable

Hi,\
\
[SqlConnection](https://www.mindstick.com/interview/33990/ado-dot-net-sqlconnection-class), [SqlCommand](https://www.mindstick.com/forum/91/difference-between-sqlcommand-and-sqlcommandbuilder) and SqlDataReader all implement the IDisposable interface. I read about a best [practice](https://yourviews.mindstick.com/story/1498/terrific-benefits-of-a-morning-meditation-practice) to always wrap IDisposables into a using block.\
\
So, my [common](https://www.mindstick.com/articles/23170/10-most-common-accounting-mistakes-of-small-business) [scenario](https://yourviews.mindstick.com/view/81221/sports-will-change-the-education-scenario) for querying data would look something like this (in greater [context](https://www.mindstick.com/forum/23245/what-is-context-in-android) of course a [mapping](https://www.mindstick.com/forum/34816/what-is-mapping-constraints-in-database-management-system) tool like linq2sql would be suitable, but lets just assume we want to use this [approach](https://www.mindstick.com/interview/985/what-are-the-approaches-that-you-will-follow-for-making-a-program-very-efficient) here):\
\
using (SqlConnection cn = new SqlConnection("myConnectionstring"))\
{\
using (SqlCommand cm = new SqlCommand("myQuery", cn))\
{\
// maybe add sql [parameters](https://www.mindstick.com/articles/87/passing-parameters-in-c-sharp)\
using (SqlDataReader reader = cm.ExecuteReader())\
{\
// read [values](https://www.mindstick.com/forum/327/sum-textbox-values) from reader object\
return myReadValues;\
}\
}\
}\
\
Is this the correct way or could that be considered overkill? I am a little unsure about this level of nesting using blocks, but of course i want to do it the correct way. Thanks!

## Replies

### Reply by Anonymous User

Hi Ida,\
Your way is correct. If a class leverages IDisposable it should be wrapped in a using statement to ensure that the Dispose() method is called. Further, communicating with an outside technology -unmanaged at that -like SQL Server shouldn't be taken lightly. The SqlCommand object implements IDisposable for a very good reason. The code below is the Dispose() method for the SqlCommand object:\
**protected override void Dispose(bool disposing)****{** **if (disposing)** **{** **this._cachedMetaData = null;** **}** **base.Dispose(disposing);****}****\**and as you can see, it's releasing a reference to the **_cachedMetaData** object so that it too can get cleaned up.\


---

Original Source: https://www.mindstick.com/forum/1209/sqlconnection-sqlcommand-sqldatareader-idisposable

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
