How to close a dataReader called from another clas

Results 1 to 2 of 2

Thread: How to close a dataReader called from another clas

  1. #1
    Join Date
    Dec 1969

    Default How to close a dataReader called from another clas

    I&#039;m a beginner to C#, so there are some basic here that I&#039;m still wrestling with:<BR><BR>--------<BR> public class aircraftList : System.Web.UI.Page<BR> {<BR> private DateTime sDate;<BR> private int bPosted;<BR> private SqlDataReader aircraftData;<BR> private string connstring = ConfigurationSettings.AppSettings["ConnectionString"];<BR><BR> public DateTime statusDate<BR> {<BR> get<BR> {<BR> return sDate;<BR> }<BR> set<BR> {<BR> sDate = value;<BR> }<BR> }<BR><BR> public int isPosted<BR> {<BR> get<BR> {<BR> return bPosted;<BR> }<BR> set<BR> {<BR> bPosted = value;<BR> }<BR><BR> }<BR><BR> public SqlDataReader data<BR> {<BR> get<BR> {<BR> SqlConnection conn = new SqlConnection(connstring);<BR> SqlCommand cmd = new SqlCommand("dbo.spAircraftStatus", conn);<BR><BR> cmd.CommandType = CommandType.StoredProcedure;<BR><BR> cmd.Parameters.Add(new SqlParameter("@statusDate", SqlDbType.Char, 24, ParameterDirection.Input.ToString())); <BR> cmd.Parameters["@statusDate"].Value = sDate;<BR> cmd.Parameters.Add(new SqlParameter("@posted", SqlDbType.Bit, 0, ParameterDirection.Input.ToString())); <BR> cmd.Parameters["@posted"].Value = bPosted;<BR><BR> conn.Open();<BR> return cmd.ExecuteReader();<BR> }<BR> }<BR><BR> }<BR>--------<BR><BR>aircraftList oAircraft = new aircraftList()<BR>oAircraft.statusDate = dtNow;<BR>oAircraft.isPosted = iPosted;<BR>SqlDataReader rAircraft =;<BR><BR>---------<BR><BR>The thing is, I&#039;d like to be able to ensure that the connection to the database and the sqlReader object are both closed and disposed of, but how? Is there something I put within the class to call, or do I just dispose of the oAircraft object and the database connection will automatically be closed?<BR><BR>What is the preferred way to do what I&#039;m attempting above?

  2. #2
    Join Date
    Dec 1969

    Default RE: How to close a dataReader called from another

    theres a lot of ways to do what you are trying to accomplish....<BR><BR>personally, i&#039;d use a DataSet instead of a DataReader since that&#039;s already a "disconnected" object as is.....<BR><BR>then you don&#039;t have to worry about it :)<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts