Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie question : Do I need to release objects in this code?

Status
Not open for further replies.

chriscboy

Programmer
Apr 23, 2002
150
GB
Hi All,

I'm fairly new to C#.NET and I have a question regarding when (and if) I should be releasing used objects in .NET. Below is a method that I have created that gets used in a CLR Assembly in SQL2005. Its a simple method that uses OLEDB to fire an SQL statement at a Visual FoxPro database. My question is, should I be releasing or doing anything the olddb objects in the example below after I have used them or does the GC deal with that ?
(my experience with using Dispose() and Finalize() is none, so some explaination my help me)
Code:
/// <summary>
        /// Executes a SQL query against the FoxPro database and returns the result
        /// The query should only return a single value
        /// </summary>
        /// <param name="source">File location of the foxpro database to use</param>
        /// <param name="sql">SQL query that will return a resultset of one record, with one field</param>
        /// <returns>Result of query or an error message that begins with #ERROR#</returns>
        [SqlFunction(DataAccess = DataAccessKind.Read)]
        public static string GetSQLScalarValue(string source, string sql)
        {
            object query;
            string retvalue = "";
            string vfpconfig = "";

            // Create foxpro configuration to use
            vfpconfig = "SET REPROCESS TO 5" + Convert.ToChar(13).ToString()
                + "SET NULL OFF" + Convert.ToChar(13).ToString()                
                +"SET MULTILOCKS ON" + Convert.ToChar(13).ToString()
                +"SET DATE TO BRITISH" + Convert.ToChar(13).ToString()
                +"SET CENTURY ON" + Convert.ToChar(13).ToString()
                +"SET CENTURY TO 19 ROLLOVER 50" + Convert.ToChar(13).ToString()
                +"SET DELETED ON";

            try
            {
                // Create OLEDB connection                
                using (new TransactionScope(TransactionScopeOption.Suppress))
                {
                    OleDbConnection cn = new OleDbConnection("Provider=VFPOLEDB.1;Data Source=" + source + ";"
                        + "Mode=Share Deny None;Extended Properties=\"\";Mask Password=False;Cache Authentication=False;"
                        + "Encrypt Password=False;Collating Sequence=MACHINE;Exclusive=OFF;DSN=\"\"");
                    //OLE DB Services=-4;BackgroundFetch=No

                    // Send configuration commands to the connection
                    cn.Open();
                    OleDbCommand cmd = new OleDbCommand("EXECSCRIPT(\"" + vfpconfig + "\")", cn);
                    cmd.ExecuteNonQuery();

                    // Send sql query and get scalar result
                    cmd = new OleDbCommand(sql, cn);
                    query = cmd.ExecuteScalar();
                    if (query!=null)
                        retvalue = query.ToString();                    
                    cn.Close();

                }
            }
            catch (Exception e)
            {
                // Catch any errors and return as a string to the calling function
                retvalue = "#ERROR#";
                retvalue += "Error Message : " + e.Message + "\r\n" + "Source : " + e.Source + "\r\n" + "Command : " + sql;
                retvalue += "\r\nUser : " + Environment.UserDomainName + "\\" + Environment.UserName;
                retvalue += "\r\nDataSource : " + source;
            }
            
            return (retvalue);
        }

 
a good rule of thumb, if the object implements IDisposable then you should dispose of it properly. call dispose in the finally block of a try/catch
Code:
myobject disposable = null;
try
{
   disposable = new myobject();
   disposable.dosomething();
}
finally
{
   if(disposable != null)
   {
      disposable.Dispose();
   }
}
or
Code:
using(var disposable = new myobject())
{
   disposable.Dispose();
}

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top