Unable to read transport connection data: Forced cancellation of an existing connection by remote host

Asked

Viewed 3,157 times

1

I am trying to make a file read in "csv" style. After reading I have to add it to the bank, however I get the following error = "Unable to read transport connection data: Forced cancellation of an existing connection by remote host.". I am reading the file directly from the url. Note: when I do a While of few repetitions it works and adds in the database normally.

            using (var reader = new StreamReader(stream))
            {
                int count = 0;

                while (!reader.EndOfStream)
                {
                    string linha = reader.ReadLine();
                    string[] valores = linha.Split('|');
                    if (count != 0)
                    {   
                        try{
                            DtoProduto produto = new DtoProduto();
                            char[] mychar = { 'B', 'R', 'L', '"' };
                            char[] contra = { '\\', '"', '>' };
                            valores[0] = valores[0].Trim(contra);
                            valores[1] = valores[1].Trim(contra);
                            valores[2] = valores[2].Trim(contra);
                            valores[3] = valores[3].Trim(contra);
                            valores[7] = valores[7].Trim(contra);
                            valores[9] = valores[9].Trim(contra);
                            valores[13] = valores[13].Trim(contra);
                            valores[4] = valores[4].Trim(mychar);
                            valores[4] = valores[4].Substring(0, valores[4].IndexOf('.') + 1);

                            decimal preco = decimal.Parse(valores[4]);
                            string categoria = valores[13].Substring(0, valores[13].IndexOf(">") - 1);
                            produto.Id = int.Parse(valores[1]);
                            produto.IdBrand = 4;
                            produto.IdProdutoIntegracao2 = valores[0];
                            produto.Nome = valores[2];
                            produto.Descricao = valores[3];
                            produto.UrlFoto = valores[7];
                            produto.UrlProduto = valores[9];
                            produto.Preco = preco;
                            produto.Situacao = EnSituacaoProduto.Ativo;
                            produto.Categorias = ObterCategorias(categoria);

                            List<int> genero = new List<int>();

                            if (valores[23].ToLower() != "adult")
                            {
                                genero.Add(3);
                            }
                            if (valores[22].ToLower() == "unisex")
                            {
                                genero.Add(1);
                                genero.Add(2);
                            }else if (valores[22].ToLower() == "mulher") {
                                genero.Add(2);
                            } else {
                                genero.Add(2);
                            }
                            produto.Generos = genero.ToArray();

                            Listproduto.Add(produto);
                        }
                        catch (Exception ex)
                        {
                            string nomeArquivo = AppDomain.CurrentDomain.BaseDirectory + @"uploads\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";
                            StreamWriter log = new StreamWriter(nomeArquivo);
                            log.WriteLine("Erro ao salvar produto " + linha);
                            log.Close();
                        }
                    }

                    count++;
                }
  • There is likely to be a block when accessing the police by firing several accesses to it, using which DBMS and which type of connection to the bank? try to create only one input connection insert all the data and then close it should solve

  • I am using Sql server running data persistence by Linq via connection by contextDB class.

  • 1

    Are you giving db. Dispose(); in your bank ? because each time the loop enters it must be opening a new connection and not Dispose() on its connection

  • remembering that db ai is your instance of Context that is using

  • 1

    Yes, I was doing just that. Thank you for your help.

No answers

Browser other questions tagged

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