コード例 #1
0
ファイル: ChapaDAO.cs プロジェクト: brunostrik/Escola-Livre
        /*
         * public int VotarNulo()
         * {
         *  MySqlConnection conn = new MySqlConnection(CONNECTION_STRING);
         *  string cmdString = "UPDATE chapas SET votos = votos + 1 WHERE numero = -1";
         *  MySqlCommand cmd = new MySqlCommand(cmdString, conn);
         *  conn.Open();
         *  int rowsAffected = cmd.ExecuteNonQuery();
         *  conn.Close();
         *  return rowsAffected;
         * }
         * public int VotarBranco()
         * {
         *  MySqlConnection conn = new MySqlConnection(CONNECTION_STRING);
         *  string cmdString = "UPDATE chapas SET votos = votos + 1 WHERE numero = 0";
         *  MySqlCommand cmd = new MySqlCommand(cmdString, conn);
         *  conn.Open();
         *  int rowsAffected = cmd.ExecuteNonQuery();
         *  conn.Close();
         *  return rowsAffected;
         * }
         */
        public Chapa CarregarChapa(string num)
        {
            MySqlConnection conn      = new MySqlConnection(CONNECTION_STRING);
            string          cmdString = "SELECT nome, presidente, vice_presidente, relator, vice_relator, " +
                                        "foto_presidente, foto_vice_presidente, foto_relator, foto_vice_relator FROM chapas " +
                                        "WHERE numero = @Numero";
            MySqlCommand cmd = new MySqlCommand(cmdString, conn);

            cmd.Parameters.Add("@Numero", MySqlDbType.Int32);
            cmd.Parameters["@Numero"].Value = num;
            conn.Open();
            MySqlDataReader dr = cmd.ExecuteReader();
            Chapa           c  = null;

            if (dr.Read())
            {
                c                = new Chapa();
                c.Numero         = num;
                c.Nome           = dr.GetString("nome");
                c.Presidente     = dr.GetString("presidente");
                c.VicePresidente = dr.GetString("vice_presidente");
                c.Relator        = dr.GetString("relator");
                c.ViceRelator    = dr.GetString("vice_relator");
            }
            //OBTER FOTOS
            conn.Close();
            return(CarregarFotos(c));
        }
コード例 #2
0
        private void MontarTela(Chapa c)
        {
            chapaVotada = c;
            if (c != null)
            {
                lblChapa.Text          = c.Nome;
                lblPresidente.Text     = c.Presidente;
                lblVicePresidente.Text = c.VicePresidente;
                lblRelator.Text        = c.Relator;
                lblViceRelator.Text    = c.ViceRelator;

                //IMAGENS
                MemoryStream ms;
                if (c.FotoPresidente != null)
                {
                    ms = new MemoryStream(c.FotoPresidente);
                    picPresidente.Image = Image.FromStream(ms);
                }
                if (c.FotoVicePresidente != null)
                {
                    ms = new MemoryStream(c.FotoVicePresidente);
                    picVicePresidente.Image = Image.FromStream(ms);
                }
                if (c.FotoRelator != null)
                {
                    ms = new MemoryStream(c.FotoRelator);
                    picRelator.Image = Image.FromStream(ms);
                }
                if (c.FotoViceRelator != null)
                {
                    ms = new MemoryStream(c.FotoViceRelator);
                    picViceRelator.Image = Image.FromStream(ms);
                }
                if (c.Nome == "NULO")
                {
                    txtNumero.Text = "N";
                }
                if (c.Nome == "BRANCO")
                {
                    txtNumero.Text = "B";
                }
            }
            else
            {
                lblChapa.Text           = "";
                lblPresidente.Text      = "";
                lblVicePresidente.Text  = "";
                lblRelator.Text         = "";
                lblViceRelator.Text     = "";
                picPresidente.Image     = null;
                picVicePresidente.Image = null;
                picRelator.Image        = null;
                picViceRelator.Image    = null;
                txtNumero.Text          = string.Empty;
            }
        }
コード例 #3
0
        private void Branco()
        {
            if (lblPresidente.Text.Trim().Length != 0)
            {
                return;
            }
            Chapa ch = new Chapa();

            ch.Nome           = "BRANCO";
            ch.Presidente     = "BRANCO";
            ch.VicePresidente = "BRANCO";
            ch.Relator        = "BRANCO";
            ch.ViceRelator    = "BRANCO";
            ch.Numero         = "B";
            txtNumero.Text    = "B";
            MontarTela(ch);
        }
コード例 #4
0
        private void Numero(string num)
        {
            if (lblPresidente.Text.Trim().Length != 0)
            {
                return;
            }
            txtNumero.Text = num;
            Chapa ch = new ChapaDAO().CarregarChapa(num);

            if (ch == null)
            {
                //VOTOU NULO
                ch                = new Chapa();
                ch.Nome           = "NULO";
                ch.Presidente     = "NULO";
                ch.VicePresidente = "NULO";
                ch.Relator        = "NULO";
                ch.ViceRelator    = "NULO";
                ch.Numero         = "N";
            }
            MontarTela(ch);
        }
コード例 #5
0
ファイル: ChapaDAO.cs プロジェクト: brunostrik/Escola-Livre
 public Chapa CarregarFotos(Chapa c)
 {
     if (c != null)
     {
         MySqlConnection conn      = new MySqlConnection(CONNECTION_STRING);
         string          CmdString = "SELECT foto_presidente, foto_vice_presidente, " +
                                     "foto_relator, foto_vice_relator FROM chapas WHERE numero = @Numero";
         MySqlCommand cmd = new MySqlCommand(CmdString, conn);
         cmd.Parameters.Add("@Numero", MySqlDbType.VarChar);
         cmd.Parameters["@Numero"].Value = c.Numero;
         conn.Open();
         MySqlDataAdapter da = new MySqlDataAdapter(cmd);
         DataTable        dt = new DataTable();
         da.Fill(dt);
         c.FotoPresidente     = (byte[])dt.Rows[0][0];
         c.FotoVicePresidente = (byte[])dt.Rows[0][1];
         c.FotoRelator        = (byte[])dt.Rows[0][2];
         c.FotoViceRelator    = (byte[])dt.Rows[0][3];
         conn.Close();
     }
     return(c);
 }
コード例 #6
0
        private void btnSalvar_Click(object sender, EventArgs e)
        {
            //Validação dos Dados
            string msg = "";

            if (txtNome.Text.Trim().Length < 3)
            {
                msg += "Preencha o nome da chapa\n";
            }
            if (txtPresidente.Text.Trim().Length < 3)
            {
                msg += "Prencha o nome do candidato a presidente\n";
            }
            if (txtVicePresidente.Text.Trim().Length < 3)
            {
                msg += "Prencha o nome do candidato a vice presidente\n";
            }
            if (txtRelator.Text.Trim().Length < 3)
            {
                msg += "Prencha o nome do candidato a relator\n";
            }
            if (txtViceRelator.Text.Trim().Length < 3)
            {
                msg += "Prencha o nome do candidato a vice relator\n";
            }
            if (txtFotoPresidente.Text.Trim().Length < 3)
            {
                msg += "Adicione a foto do candidato a presidente\n";
            }
            if (txtFotoVicePresidente.Text.Trim().Length < 3)
            {
                msg += "Adicione a foto do candidato a vice presidente\n";
            }
            if (txtFotoRelator.Text.Trim().Length < 3)
            {
                msg += "Adicione a foto do candidato a relator\n";
            }
            if (txtFotoViceRelator.Text.Trim().Length < 3)
            {
                msg += "Adicione a foto do candidato a vice relator\n";
            }
            if (msg != "")
            {
                Program.Alerta(msg);
                return;
            }

            //Se chegou a este ponto validou tudo certo
            //Constroi a entidade
            Chapa c = new Chapa();

            c.Numero         = txtNumero.Value.ToString().Trim();
            c.Nome           = txtNome.Text.Trim();
            c.Presidente     = txtPresidente.Text.Trim();
            c.VicePresidente = txtVicePresidente.Text.Trim();
            c.Relator        = txtRelator.Text.Trim();
            c.ViceRelator    = txtViceRelator.Text.Trim();

            //Prepara o buffer para caregar array de bytes
            FileStream   fs;
            BinaryReader br;
            string       FileName;

            //Foto do PRESIDENTE
            FileName         = txtFotoPresidente.Text;
            fs               = new FileStream(FileName, FileMode.Open, FileAccess.Read);
            br               = new BinaryReader(fs);
            c.FotoPresidente = br.ReadBytes((int)fs.Length);
            br.Close();
            fs.Close();
            //Foto do VICE-PRESIDENTE
            FileName             = txtFotoVicePresidente.Text;
            fs                   = new FileStream(FileName, FileMode.Open, FileAccess.Read);
            br                   = new BinaryReader(fs);
            c.FotoVicePresidente = br.ReadBytes((int)fs.Length);
            br.Close();
            fs.Close();
            //Foto do RELATOR
            FileName      = txtFotoRelator.Text;
            fs            = new FileStream(FileName, FileMode.Open, FileAccess.Read);
            br            = new BinaryReader(fs);
            c.FotoRelator = br.ReadBytes((int)fs.Length);
            br.Close();
            fs.Close();
            //Foto do VICE RELATOR
            FileName          = txtFotoViceRelator.Text;
            fs                = new FileStream(FileName, FileMode.Open, FileAccess.Read);
            br                = new BinaryReader(fs);
            c.FotoViceRelator = br.ReadBytes((int)fs.Length);
            br.Close();
            fs.Close();

            if (!Program.Pergunta("Os dados estão corretos?"))
            {
                return;
            }
            //Zera os votos da chapa recém-criada
            c.Votos = 0;
            int res = new ChapaDAO().Cadastrar(c);

            if (res == 0)
            {
                Program.Erro("Falha ao salvar");
            }
            else
            {
                Program.Mensagem("Dados salvos com sucesso.\nPara votar nesta chapa use o número " + c.Numero);
                this.Close();
            }
        }
コード例 #7
0
ファイル: ChapaDAO.cs プロジェクト: brunostrik/Escola-Livre
        public int Cadastrar(Chapa c)
        {
            MySqlConnection conn      = new MySqlConnection(CONNECTION_STRING);
            string          CmdString = "INSERT INTO chapas " +
                                        "(numero, nome, presidente, vice_presidente, relator, vice_relator, votos) " +
                                        "VALUES(@Numero, @Nome, @Presidente, @VicePresidente, @Relator, @ViceRelator, @Votos)";
            MySqlCommand cmd = new MySqlCommand(CmdString, conn);

            cmd.Parameters.Add("@Numero", MySqlDbType.VarChar);
            cmd.Parameters.Add("@Nome", MySqlDbType.VarChar);
            cmd.Parameters.Add("@Presidente", MySqlDbType.VarChar);
            cmd.Parameters.Add("@VicePresidente", MySqlDbType.VarChar);
            cmd.Parameters.Add("@Relator", MySqlDbType.VarChar);
            cmd.Parameters.Add("@ViceRelator", MySqlDbType.VarChar);
            cmd.Parameters.Add("@Votos", MySqlDbType.Int32);
            cmd.Parameters["@Numero"].Value         = c.Numero;
            cmd.Parameters["@Nome"].Value           = c.Nome;
            cmd.Parameters["@Presidente"].Value     = c.Presidente;
            cmd.Parameters["@VicePresidente"].Value = c.VicePresidente;
            cmd.Parameters["@Relator"].Value        = c.Relator;
            cmd.Parameters["@ViceRelator"].Value    = c.ViceRelator;
            cmd.Parameters["@Votos"].Value          = c.Votos;
            conn.Open();
            int rowsAffected = cmd.ExecuteNonQuery();

            conn.Close();

            //AGORA INSERIR A FOTO DO PRESIDENTE
            conn      = new MySqlConnection(CONNECTION_STRING);
            CmdString = "UPDATE chapas SET foto_presidente = @Foto WHERE numero = @Numero";
            cmd       = new MySqlCommand(CmdString, conn);
            cmd.Parameters.Add("@Foto", MySqlDbType.MediumBlob);
            cmd.Parameters.Add("@Numero", MySqlDbType.VarChar);
            cmd.Parameters["@Numero"].Value = c.Numero;
            cmd.Parameters["@Foto"].Value   = c.FotoPresidente;
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

            //AGORA INSERIR A FOTO DO VICE PRESIDENTE
            conn      = new MySqlConnection(CONNECTION_STRING);
            CmdString = "UPDATE chapas SET foto_vice_presidente = @Foto WHERE numero = @Numero";
            cmd       = new MySqlCommand(CmdString, conn);
            cmd.Parameters.Add("@Foto", MySqlDbType.MediumBlob);
            cmd.Parameters.Add("@Numero", MySqlDbType.VarChar);
            cmd.Parameters["@Numero"].Value = c.Numero;
            cmd.Parameters["@Foto"].Value   = c.FotoVicePresidente;
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

            //AGORA INSERIR A FOTO DO RELATOR
            conn      = new MySqlConnection(CONNECTION_STRING);
            CmdString = "UPDATE chapas SET foto_relator = @Foto WHERE numero = @Numero";
            cmd       = new MySqlCommand(CmdString, conn);
            cmd.Parameters.Add("@Foto", MySqlDbType.MediumBlob);
            cmd.Parameters.Add("@Numero", MySqlDbType.VarChar);
            cmd.Parameters["@Numero"].Value = c.Numero;
            cmd.Parameters["@Foto"].Value   = c.FotoRelator;
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

            //AGORA INSERIR A FOTO DO VICE RELATOR
            conn      = new MySqlConnection(CONNECTION_STRING);
            CmdString = "UPDATE chapas SET foto_vice_relator = @Foto WHERE numero = @Numero";
            cmd       = new MySqlCommand(CmdString, conn);
            cmd.Parameters.Add("@Foto", MySqlDbType.MediumBlob);
            cmd.Parameters.Add("@Numero", MySqlDbType.VarChar);
            cmd.Parameters["@Numero"].Value = c.Numero;
            cmd.Parameters["@Foto"].Value   = c.FotoViceRelator;
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

            return(rowsAffected);
        }