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)
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);
}