I'm developing a service in C #.
When I run my service, the connection error already occurs.
So before calling each function I make a reader.Close();
and I do not open the connection in the other functions anymore. But I do not know if this is correct and if it would be the right way.
Step1
public int VerifyStatus30(List<Class.ReturnTableName> tableInfo)
{
try
{
var countDis = 0;
using (MySqlConnection conn = DB.DatabaseConnection.getHSDBConnection())
{
conn.Open();
MySqlCommand command = new MySqlCommand("SELECT DataObj, RecId FROM HS_REGISTRIES WHERE ErrorCode = 0 AND HandleStatus = 30 ORDER BY UpdateDate30 ASC LIMIT 1", conn);
using (MySqlDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
countDis = 1;
while (reader.Read())
{
int dataObj = reader.GetInt32(0);
int recId = reader.GetInt32(1);
reader.Close();
ClassTable.ReturnSapId returnSapId = new ClassTable.ReturnSapId();
returnSapId.GetSapId(recId, dataObj, tableInfo);
}
}
}
}
return countDis; // retorna a variável quantidade
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
Step2
public void GetSapId(int recId, int dataObj, List<Class.ReturnTableName> tableInfo)
{
try
{
Class.ReturnTableName result = tableInfo.Find(x => x.IdIntegraHardness == dataObj);
using (MySqlConnection conn = DB.DatabaseConnection.getHSDBConnection())
{
//conn.Open();
MySqlCommand command = new MySqlCommand("SELECT AbsEntry,u_D005_id FROM HS501_ONCM WHERE RecId = @recId", conn);
command.Parameters.AddWithValue("@tableName", result.TableIntegraHardness.Replace("'", ""));
command.Parameters.AddWithValue("@recId", recId);
using (MySqlDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
int idSAP = reader.GetInt32(0);//ID SAP
int idHardness = reader.GetInt32(1);//ID HARDNESS
Class.UpdateStatus updateStatus = new Class.UpdateStatus();
reader.Close();
updateStatus.Update40(recId, 1);
SaveIdSH(idSAP,idHardness, tableInfo, dataObj);
updateStatus.Update50(recId, 1);
}
}
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}