I use and OleDbDataReader inside using but the memory is never released.
I close the reader after use.
public void getSegmentData(string tag, string outputPath, string databaseName, Log log)
{
OleDbCommand command = null;
try
{
using (OleDbConnection connection = new OleDbConnection("Data Source=.\WINCC;User ID=Localadmin;Provider=WinCCOLEDBProvider.1;Persist Security Info=False;Catalog=" + databaseName + ";Mode=Read"))
{
command = new OleDbCommand("TAG:R," + "'" + tag + "'" + ",'2005-01-01 00:00:00.000','2035-12-31 23:59:59.000'", connection);
command.Connection.Open();
using (StreamWriter csvWriter = new StreamWriter($"{outputPath}.csv", false))
{
OleDbDataReader reader = command.ExecuteReader();
csvWriter.WriteLine("ValueID;Timestamp;RealValue;Quality;Flags");
//vérifie si le fichier est vide
if (!reader.HasRows)
{
log.AddLog(Log.LogType.TRACE, "Le fichier généré pour la variable " + tag + " est vide");
}
while (reader.Read())
{
string valueId = reader[0].ToString();
string timestamp = reader[1].ToString();
string realValue = reader[2].ToString();
string quality = reader[3].ToString();
string flags = reader[4].ToString();
csvWriter.WriteLine($"{valueId};{timestamp};{realValue};{quality};{flags}");
}
reader.Close();
if (reader.IsClosed != true)
{
log.AddLog(Log.LogType.ERROR, "READER is close != TRUE");
}
}
command.Connection.Close();
command.Dispose();
}
}
New contributor
Jules Bozouklian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.