Processing of text files

Asked

Viewed 102 times

0

I am processing files .txt, however I process more than 1 file at a time, but in the database only saves 1 file, debugging I see that it reads and processes the 2 files, or up to 3 depending on how many files I am processing.

Follow my controller and Negocio.

You’d have to be putting the files together .txt for me to generate a single report after.

Before processing it clears the data base with method removerSabemi(), but it processes 1 file and then goes back to the method removerSabemi(), and then process the other file.

I need you to go through the method removerSabemi() only at first execution.

I need to treat in some way that I’m not finding.

Controller

if ((arquivosSabemi != null) && (arquivosSabemi.Any(f => f != null)))
{
    foreach (var arquivo in arquivosSabemi.Where(s => s.ContentLength > 0))
    {
        InstanceResolverFor<IServicoFIDC>.Instance.SalvarSabemi(arquivo.InputStream);
    }
}

Service

public void SalvarSabemi(Stream dados)
{
     using (var arquivoSabemi = new StreamReader(dados))
     {
         var dal = InstanceResolverFor<IDadosFIDC>.Instance;
         var primeiraLinha = true;
         string linha;

         dal.RemoverSabemi();
         while (!arquivoSabemi.EndOfStream && !string.IsNullOrEmpty(linha = arquivoSabemi.ReadLine()))
         {
              if (primeiraLinha) { primeiraLinha = false; continue; }

              var colunas = linha.Split("\t".ToCharArray());

              dal.Inserir(new DadosSabemi
              {
                  CEDNTE_CEDN = ParseValueOrNull<long>(colunas[0]),
  • In your "controller", do something like: InstanceResolverFor<IDadosFIDC>.Instance.RemoverSabemi(); (before the foreach). And in "service" remove the dal.RemoverSabemi();. That should work.

  • Thanks for the help, I managed to settle with the method below.

1 answer

1


Just you pass a bool for your method.

Controller

if ((arquivosSabemi != null) && (arquivosSabemi.Any(f => f != null)))
{
    bool ckfim = true;
    foreach (var arquivo in arquivosSabemi.Where(s => s.ContentLength > 0))
    {
        InstanceResolverFor<IServicoFIDC>.Instance.SalvarSabemi(arquivo.InputStream, ckfim);
        ckfim = false;
    }
}

Service

public void SalvarSabemi(Stream dados, bool ckfim)
{
     using (var arquivoSabemi = new StreamReader(dados))
     {
         var dal = InstanceResolverFor<IDadosFIDC>.Instance;
         var primeiraLinha = true;
         string linha;

         if(ckfim)
            dal.RemoverSabemi();

         while (!arquivoSabemi.EndOfStream && !string.IsNullOrEmpty(linha = arquivoSabemi.ReadLine()))
         {
              if (primeiraLinha) { primeiraLinha = false; continue; }

              var colunas = linha.Split("\t".ToCharArray());

              dal.Inserir(new DadosSabemi
              {
  • I did so, using a variable of type bool to check the end of the Stream, but in the controller keeps complaining that the method cannot receive 2 arguments.

  • 1

    Solved here, I had forgotten to pass the variable on the interface too, and with that could only receive 1 argument. Thanks man, helped me a lot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.