I am pretty new to C#. I am a Borland c++ developer and is used to "newing" and "deleting" objects programmatically.
I understand that with C# the garbage collector is responsible for the deleting of objects.
It seems though that I have a memory leak. I am developing a SQLServerDBInterface class (which I can easily use / include in other future projects). What I notice is that when I call my custom DBConnect function the memory (in task manager for this C# process) increases with a couple of bytes everytime.
Could you please have a look at this code and tell me if I should me deleting the object isntance myself?
Thanks in advance.
This is the code in my interface class
class SqlServerDatabaseInterface
{
private const int SI_SUCCESS = 0;
private const int SI_EXCEPTION = -1000;
SqlConnection g_DbConnection = new SqlConnection();
SqlCommand g_SqlCommand = new SqlCommand();
String g_S_AdditionalInformation = "";
//---------------------------------------------------------------
public int ConnectToDB()
{
try
{
g_DbConnection.Close();
g_DbConnection.ConnectionString = @"user id=sa;" +
"password=password;server=mssqlserver2008;" +
"Trusted_Connection=yes;" +
"database=Test; " +
"connection timeout=30";
g_DbConnection.Open();
}
catch(Exception e)
{
g_S_AdditionalInformation = e.Message;
return SI_EXCEPTION;
}
return SI_SUCCESS;
}
this is the code in my test app that calls the DBinterface class
public partial class frmMain : Form
{
SqlServerDatabaseInterface l_SqlServerDatabaseInterface = null;
public frmMain()
{
InitializeComponent();
l_SqlServerDatabaseInterface = new SqlServerDatabaseInterface();
}
//------------------------------------------------------------------
private void button1_Click(object sender, EventArgs e)
{
int l_i_ReturnCode = 0;
l_i_ReturnCode = l_SqlServerDatabaseInterface.ConnectToDB();
if (l_i_ReturnCode < 0)
{
MessageBox.Show("An error occurred: " + l_SqlServerDatabaseInterface.GetLastReturnMessage(l_i_ReturnCode), "An error occurred", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}