示例#1
0
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();

            //Alimenta a variável das datas
            dataInicio = dateTimePickerInicio.Value.ToString("dd-MM-yyyy");
            dataFinal  = dateTimePickerFinal.Value.ToString("dd-MM-yyyy");

            SqlConnection conn = new ConnectionFactory().getConnection();

            SqlCommand cmd = new SqlCommand(@"select distinct(codpromotor) as codpromotor from movpromotores
                                              where data between @dataini and @datafim", conn);

            cmd.Parameters.AddWithValue("@dataini", dateTimePickerInicio.Value);
            cmd.Parameters.AddWithValue("@datafim", dateTimePickerFinal.Value);

            try
            {
                cxTotal.Text = getQuantidade().ToString();

                SqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    ReportDocument rpDocument =
                        new CarregaRelatorio().carregarRelatorio(montaQuery(reader["codpromotor"].ToString()), "T:\\rel\\relatorioDisparo.rpt");

                    //Cria um stream do pdf
                    System.IO.Stream st =
                        rpDocument.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);

                    //Faz a leitura do stream para byte
                    System.IO.BinaryReader br = new System.IO.BinaryReader(st);

                    byte[] pdfByte = new byte[st.Length];

                    for (int i = 0; i < st.Length; ++i)
                    {
                        pdfByte[i] = br.ReadByte();
                    }

                    //Fecha o stream e o byte reader
                    st.Close();
                    br.Close();

                    //Grava o email na fila de envio
                    if (gravaLogEMail(reader["codpromotor"].ToString(), pdfByte))
                    {
                        listBox1.Items.Add("Promotor " + reader["codpromotor"].ToString() + " -> Gerou com sucesso!");
                    }
                    else
                    {
                        listBox1.Items.Add("Promotor " + reader["codpromotor"].ToString() + " -> Falha ao gerar");
                    }

                    listBox1.SelectedIndex = listBox1.Items.Count - 1;

                    quantidade.Text = (listBox1.Items.Count).ToString();

                    if (listBox1.Items.Count == Convert.ToInt64(cxTotal.Text))
                    {
                        MessageBox.Show("O processo foi finalizado com sucesso!");
                    }

                    rpDocument.Close();

                    Application.DoEvents();
                }

                reader.Close();
            }catch (Exception exc)
            {
                MessageBox.Show("Erro ao gerar relatório\n" + exc);
            }
            finally
            {
                conn.Close();
            }
        }
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();

            //Alimenta a variável das datas
            dataInicio = dateTimePickerInicio.Value.ToString("dd-MM-yyyy");
            dataFinal = dateTimePickerFinal.Value.ToString("dd-MM-yyyy");

            SqlConnection conn = new ConnectionFactory().getConnection();

            SqlCommand cmd = new SqlCommand(@"select distinct(codpromotor) as codpromotor from movpromotores
                                              where data between @dataini and @datafim", conn);

            cmd.Parameters.AddWithValue("@dataini", dateTimePickerInicio.Value);
            cmd.Parameters.AddWithValue("@datafim", dateTimePickerFinal.Value);

            try
            {
                cxTotal.Text = getQuantidade().ToString();

                SqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    ReportDocument rpDocument =
                            new CarregaRelatorio().carregarRelatorio(montaQuery(reader["codpromotor"].ToString()), "T:\\rel\\relatorioDisparo.rpt");

                   //Cria um stream do pdf
                   System.IO.Stream st =
                       rpDocument.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);

                    //Faz a leitura do stream para byte
                   System.IO.BinaryReader br = new System.IO.BinaryReader(st);

                   byte[] pdfByte = new byte[st.Length];

                   for (int i = 0; i < st.Length; ++i)
                   {
                       pdfByte[i] = br.ReadByte();
                   }

                    //Fecha o stream e o byte reader
                   st.Close();
                   br.Close();

                   //Grava o email na fila de envio
                   if (gravaLogEMail(reader["codpromotor"].ToString(), pdfByte))
                   {
                       listBox1.Items.Add("Promotor "+ reader["codpromotor"].ToString() + " -> Gerou com sucesso!");
                   }
                   else
                   {
                       listBox1.Items.Add("Promotor " + reader["codpromotor"].ToString() + " -> Falha ao gerar");
                   }

                   listBox1.SelectedIndex = listBox1.Items.Count-1;

                   quantidade.Text = (listBox1.Items.Count).ToString();

                   if (listBox1.Items.Count == Convert.ToInt64(cxTotal.Text))
                       MessageBox.Show("O processo foi finalizado com sucesso!");

                   rpDocument.Close();

                    Application.DoEvents();

                }

                reader.Close();
            }catch(Exception exc)
            {
                MessageBox.Show("Erro ao gerar relatório\n"+ exc);
            }
            finally
            {
                conn.Close();
            }
        }