Пример #1
0
        public static UsuarioModel ValidarUser(String login, String senha)
        {
            UsuarioModel ret = null;

            using (var conexao = new SqlConnection())
            {
                conexao.ConnectionString = ConfigurationManager.ConnectionStrings["principal"].ConnectionString;
                conexao.Open();

                using (var comando = new SqlCommand())
                {
                    comando.Connection  = conexao; //associando o comando a nossa conexão
                    comando.CommandText = "select * from usuario where login = @login and senha = @senha";
                    //Adicionando os parametros login e senha
                    comando.Parameters.Add("@login", SqlDbType.VarChar).Value = login;
                    comando.Parameters.Add("@senha", SqlDbType.VarChar).Value = CriptoHelp.HashMD5(senha);
                    var read = comando.ExecuteReader(); //A variavel read conterá os valores retornados na consulta SQL

                    if (read.Read())
                    {
                        ret = new UsuarioModel
                        { //Criamos um usuario com os valores retornados pela consulta
                            Id    = (int)read["id"],
                            Nome  = (string)read["nome"],
                            Login = (string)read["login"],
                            Senha = (string)read["senha"]
                        };
                    }
                }
            }
            return(ret);
        }
Пример #2
0
        public int salvarUsuario()
        {
            int ret   = 0;
            var model = findUsuario(this.Id);

            using (var conexao = new SqlConnection())
            {
                conexao.ConnectionString = ConfigurationManager.ConnectionStrings["principal"].ConnectionString;
                conexao.Open();
                using (var comando = new SqlCommand())
                {
                    comando.Connection = conexao;
                    if (model == null)
                    {                                                                                                                                    //Case Inserção
                        comando.CommandText = "insert into usuario(nome,login,senha) values (@nome,@login,@senha);select convert(int,scope_identity())"; //scope_identity retorna o id do ultimo valor inserido na tabela
                        comando.Parameters.Add("@nome", SqlDbType.VarChar).Value  = this.Nome;
                        comando.Parameters.Add("@login", SqlDbType.VarChar).Value = this.Login;
                        comando.Parameters.Add("@senha", SqlDbType.VarChar).Value = CriptoHelp.HashMD5(this.Senha); //CriptoHelp classe que criamos para inserir a senha criptografada no banco através de md5
                        ret = (int)comando.ExecuteScalar();
                    }
                    else if (this.Senha != "")
                    { //Case Atualização
                        comando.CommandText = "update usuario set nome = @nome, login = @login, senha = @senha where id = @id";
                        comando.Parameters.Add("@nome", SqlDbType.VarChar).Value  = this.Nome;
                        comando.Parameters.Add("@login", SqlDbType.VarChar).Value = this.Login;
                        comando.Parameters.Add("@senha", SqlDbType.VarChar).Value = CriptoHelp.HashMD5(this.Senha);
                        comando.Parameters.Add("@id", SqlDbType.Int).Value        = this.Id;
                        if (comando.ExecuteNonQuery() > 0)
                        {
                            ret = this.Id;
                        }
                    }
                    else
                    {
                        comando.CommandText = "update usuario set nome = @nome, login = @login where id = @id";
                        comando.Parameters.Add("@nome", SqlDbType.VarChar).Value  = this.Nome;
                        comando.Parameters.Add("@login", SqlDbType.VarChar).Value = this.Login;
                        comando.Parameters.Add("@id", SqlDbType.Int).Value        = this.Id;
                        if (comando.ExecuteNonQuery() > 0)
                        {
                            ret = this.Id;
                        }
                    }
                }

                return(ret);
            }
        }