Click here to Skip to main content
15,885,878 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys

i want to close my Data Base connection automatically when i close my Data reader.
Is there any property of Data Reader with the help of which i can achieve that.

I dont want to call Connection.Close Method()


Thanks
Posted
Updated 17-Jul-22 22:00pm

1 solution

How about the using statement?

To ensure that connections are always closed, open the connection inside of a using block, as shown in the following code fragment. Doing so ensures that the connection is automatically closed when the code exits the block.

C#
using (SqlConnection connection = new SqlConnection(
               connectionString))
{
        SqlCommand command = new SqlCommand(queryString, connection);
        command.Connection.Open();
        command.ExecuteNonQuery();
}



Cheers
 
Share this answer
 
Comments
Mahendra.p25 24-Sep-11 2:29am    
Thanks for a reply but iam using 3 tier architecture and connection property is in data layer and iam accessing Data Reader in business layer so requirement is different...
Mehdi Gholam 24-Sep-11 2:31am    
Just use the same "using" statement in your data layer.
Mario Majčica 24-Sep-11 2:37am    
Once you return the DataReader from your DAL, close the connection (or let using doing it)


///
/// Retrieves all articles
///

public override List<articledetails> GetArticles(int pageIndex, int pageSize)
{
using (SqlConnection cn = new SqlConnection(this.ConnectionString))
{
SqlCommand cmd = new SqlCommand("tbh_Articles_GetArticles", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@PageIndex", SqlDbType.Int).Value = pageIndex;
cmd.Parameters.Add("@PageSize", SqlDbType.Int).Value = pageSize;
cn.Open();
return GetArticleCollectionFromReader(ExecuteReader(cmd), false);
}
}

where

///
/// Returns a new ArticleDetails instance filled with the DataReader's current record data
///

protected virtual ArticleDetails GetArticleFromReader(IDataReader reader)
{
return GetArticleFromReader(reader, true);
}
protected virtual ArticleDetails GetArticleFromReader(IDataReader reader, bool readBody)
{
ArticleDetails article = new ArticleDetails(
(int)reader["ArticleID"],
(DateTime)reader["AddedDate"],
reader["AddedBy"].ToString(),
(int)reader["CategoryID"],
reader["CategoryTitle"].ToString(),
reader["Title"].ToString(),
reader["Abstract"].ToString(),
null,
reader["Country"].ToString(),
reader["State"].ToString(),
reader["City"].ToString(),
(reader["ReleaseDate"] == DBNull.Value ? DateTime.Now : (DateTime)reader["ReleaseDate"]),
(reader["ExpireDate"] == DBNull.Value ? DateTime.MaxValue : (DateTime)reader["ExpireDate"]),
(bool)reader["Approved"],
(bool)reader["Listed"],
(bool)reader["CommentsEnabled"],
(bool)reader["OnlyForMembers"],
(int)reader["ViewCount"],
(int)reader["Votes"],
(int)reader["TotalRating"]);

if (readBody)
article.Body = reader["Body"].ToString();

return article;
}
Mario Majčica 24-Sep-11 2:38am    
Also you can find an valid example here

http://www.wrox.com/WileyCDA/WroxTitle/productCd-0764584642,descCd-DOWNLOAD.html

Cheers
Mahendra.p25 24-Sep-11 2:33am    
Anyway got an answer via google (CommandBehavior.CloseConnection)property thanks i was searching for this

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900