How can I not freeze / crash the application when querying data via SQL Server and creating a "progress bar"?

0

The code below as an example brings me great information from the database. Every time I click on the "Search" option of my program, my application freezes / hangs for a while until the select "pull" everything. I would like to know how best to consult this data without crashing the program. Of course, to make it easier for the user, I'd like to put a progress bar.

    private void button1_Click(object sender, EventArgs e)
        {
                SqlCommand myCommand = new SqlCommand("SELECT CONVERT(datetime, SF2010.F2_EMISSAO) AS [DATA DE EMISSÃO], " +
          "SF2010.F2_DOC AS [NF], " +
          "SD2010.D2_PEDIDO AS [PEDIDO TUPAN], " +
          "SC5010.C5_PEDCLI AS [PEDIDO CLIENTE], " +
          "SA1010.A1_CGC AS [CNPJ], " +
          "SA1010.A1_NOME AS [CLIENTE], " +
          "SA1010.A1_MUN AS [MUNICÍPIO], " +
          "SF2010.F2_EST AS [ESTADO]," +
          "SA4010.A4_NOME AS [TRANSPORTADORA], " +
          "SF2010.F2_TPFRETE AS [FRETE], " +
          "SF2010.F2_VOLUME1 AS [VOLUME], " +
          "cast(SF2010.F2_PBRUTO AS DECIMAL(15,2)) AS [PESO BRUTO], " +
          //  "cast(sum( SD2010.D2_TOTAL )AS DECIMAL (15,2)) AS [TOTAL DA NOTA (R$)]" + //ACABA O SELECT, FROM ABAIXO
          "SF2010.F2_VALBRUT AS [TOTAL DA NOTA (R$)], " +
          "SF2010.F2_UDTAGEN as [DATA DE AGENDAMENTO]," +
"SF2010.F2_UHORAGE as [HORA DO AGENDAMENTO]," +
"SF2010.F2_UDTSOLI as [DATA DA SOLICIT. DE COLETA]," +
          "SF2010.F2_UNUMCOL as [NÚMERO DA COLETA]," +
"SF2010.F2_DTSAIEN as [DATA DE SAÍDA]," +
"SF2010.F2_DTENTRE as [DATA DE ENTREGA]" +
                    "FROM SA4010 " +
          "RIGHT JOIN(SA3010 AS VCAD RIGHT JOIN(SA3010 AS VFAT " +
          "RIGHT JOIN(SC5010 RIGHT JOIN(SB1010 RIGHT JOIN(SD2010 LEFT JOIN(SA1010 RIGHT JOIN SF2010 " +
          "ON(SA1010.A1_LOJA = SF2010.F2_LOJA) AND (SA1010.A1_COD = SF2010.F2_CLIENTE)) ON (SD2010.D2_FILIAL = SF2010.F2_FILIAL) " +
          "AND(SD2010.D2_SERIE = SF2010.F2_SERIE) AND(SD2010.D2_DOC = SF2010.F2_DOC)) ON SB1010.B1_COD = SD2010.D2_COD) " +
          "ON SC5010.C5_NUM = SD2010.D2_PEDIDO) ON VFAT.A3_COD = SC5010.C5_VEND1) ON VCAD.A3_COD = SA1010.A1_VEND) " +
          "ON SA4010.A4_COD = SF2010.F2_TRANSP WHERE(((SD2010.D2_FILIAL) = ([SF2010].[F2_FILIAL])) " +
          "AND((SC5010.C5_FILIAL) = ([SF2010].[F2_FILIAL]))) " +
          "AND SF2010.D_E_L_E_T_ <> '*' AND SD2010.D_E_L_E_T_ <> '*' AND SA1010.D_E_L_E_T_ <> '*' " +
          "AND SC5010.D_E_L_E_T_ <> '*' " + "AND F2_EMISSAO >='" + TextBoxAno.Text + TextBoxMes.Text + TextBoxDia.Text + "'" +
     "GROUP BY SF2010.F2_EMISSAO, SF2010.F2_DOC, SD2010.D2_PEDIDO, SC5010.C5_PEDCLI, SA1010.A1_CGC, " +
          "SA1010.A1_NOME, SA1010.A1_MUN, SF2010.F2_EST, SA4010.A4_NOME, SF2010.F2_TPFRETE, " +
          "SF2010.F2_VOLUME1, SF2010.F2_PBRUTO, SF2010.F2_VALBRUT, SF2010.F2_UDTSOLI, SF2010.F2_DTSAIEN, SF2010.F2_DTENTRE, SF2010.F2_UDTAGEN, SF2010.F2_UHORAGE, SF2010.F2_UNUMCOL     " + //ADICIONADO F2_VALBRUT " +
     "ORDER BY [DATA DE EMISSÃO] DESC, [NF] DESC;", Conexção.ConexaoConfig);
            myCommand.CommandTimeout = 240;
            SqlDataAdapter adp = new SqlDataAdapter(myCommand);
                DataTable dt = new DataTable();
                adp.Fill(dt);
                //MessageBox.Show(dt.ToString());
                GradedeDados.DataSource = dt;
                GradedeDados.Sort(GradedeDados.Columns[0], ListSortDirection.Ascending);
    
asked by anonymous 22.01.2018 / 13:58

0 answers