//INSERT LOGIN TEMPORARIO public void InserirLoginTemp(Usuarios login) { string table = "LoginTemp"; var loginTemp = new TabelaTempDAO(); int count = loginTemp.Count(table); if (count < 0) { var strQuery = ""; strQuery += " CREATE TABLE LoginTemp ( "; strQuery += " Login varchar(30) NOT NULL, "; strQuery += " Senha varchar(50) NOT NULL, "; strQuery += " Perfil bit NOT NULL "; strQuery += " ) "; using (conexao = new Conexao()) { conexao.ExecutaComando(strQuery); } } else loginTemp.AtualizaTemp(table); seguranca = new Seguranca(); var strQuery1 = ""; strQuery1 += " INSERT INTO LoginTemp (Login, Senha, Perfil) "; strQuery1 += string.Format(" VALUES ('{0}','{1}','{2}') ", login.Email, seguranca.Criptografar(login.Senha), login.Perfil); using(conexao = new Conexao()) { conexao.ExecutaComando(strQuery1); } }
//INSERT INTO private void Inserir(Turmas turma) { string table = "TurmasTemp"; var turmasTemp = new TabelaTempDAO(); //Verifica se existe tabela, e se sim, se existe algum registro na mesma int count = turmasTemp.Count(table); if (count < 0) { var strQuery = ""; strQuery += " CREATE TABLE TurmasTemp ( "; strQuery += " TurmasId int NOT NULL "; strQuery += " ) "; using (conexao = new Conexao()) { conexao.ExecutaComando(strQuery); } } else { var strQueryTemp = ""; strQueryTemp = string.Format(" INSERT INTO TurmasTemp (TurmasID) VALUES({0}) ", turma.CursoID); var strQuery = ""; strQuery += " INSERT INTO Turmas (Nome, FKCursosId)"; strQuery += string.Format(" VALUES ('{0}',{1})", turma.Nome, turma.CursoID); using (conexao = new Conexao()) { conexao.ExecutaComando(strQuery); } } }
//DELETE public void Excluir(int id) { using (conexao = new Conexao()) { var strQuery = string.Format(" DELETE FROM Disciplina WHERE DisciplinaId = {0}", id); conexao.ExecutaComando(strQuery); } }
//SELECT * WHERE ID public Disciplina ListarPorId(int id) { using (conexao = new Conexao()) { var strQuery = string.Format(" SELECT * FROM Disciplina WHERE DisciplinaId = {0} ", id); var retornoDataReader = conexao.ExecutaSelect(strQuery); return ListaDeObjetos(retornoDataReader).FirstOrDefault(); } }
//SELECT * COM PERFIL public List<Usuarios> ListarTodosComPerfil() { using (conexao = new Conexao()) { var strQuery = " SELECT P.*, L.Perfil FROM Professores AS P INNER JOIN Login AS L ON P.FKLoginId = L.LoginId"; var retornoDataReader = conexao.ExecutaSelect(strQuery); return ListaDeObjetos(retornoDataReader); } }
//SELECT * WHERE ID public List<Cursos> ListarPorId(Cursos cursos) { using (conexao = new Conexao()) { var strQuery = string.Format(" SELECT * FROM Cursos WHERE CursosId = {0}", cursos.Id); var retornoDataReader = conexao.ExecutaSelect(strQuery); return ListaDeObjetos(retornoDataReader); } }
//SELECT * public List<Cursos> ListarTodos() { using (conexao = new Conexao()) { var strQuery = " SELECT * FROM Cursos"; var retornoDataReader = conexao.ExecutaSelect(strQuery); return ListaDeObjetos(retornoDataReader); } }
//ATUALIZA DADOS TABELA TEMPORARIA public void AtualizaTemp(string table) { var strQuery = ""; strQuery += string.Format(" DELETE FROM {0} ", table); using (conexao = new Conexao()) { conexao.ExecutaComando(strQuery); } }
//DELETE TABELA TEMPORARIA public void ExcluirTemp(string table) { var strQuery = ""; strQuery += string.Format(" DROP TABLE {0} ", table); using (conexao = new Conexao()) { conexao.ExecutaComando(strQuery); } }
//DELETE public void Exclui(Usuarios login) { var strQuery = ""; strQuery += string.Format(" DELETE FROM Login WHERE LoginId = {0} ", login.LoginId); using (conexao = new Conexao()) { conexao.ExecutaComando(strQuery); } }
//DELETE public void Excluir(Usuarios professor) { var strQuery = ""; strQuery += string.Format(" DELETE FROM Professores WHERE ProfessorId = {0} ", professor.ProfessorId); using (conexao = new Conexao()) { conexao.ExecutaComando(strQuery); } }
//DELETE public void Excluir(Cursos cursos) { var strQuery = ""; strQuery += string.Format(" DELETE FROM Cursos WHERE CursosId = {0}", cursos.Id); using (conexao = new Conexao()) { conexao.ExecutaComando(strQuery); } }
//DELETE FROM public void Excluir(int id) { var strQuery = ""; strQuery += string.Format(" DELETE FROM Turmas WHERE TurmasId = {0} ", id); using (conexao = new Conexao()) { conexao.ExecutaComando(strQuery); } }
//INSERT private void Inserir(Cursos cursos) { var strQuery = ""; strQuery += " INSERT INTO Curso (Nome) "; strQuery += string.Format(" VALUES ('{0}')", cursos.Nome); using (conexao = new Conexao()) { conexao.ExecutaComando(strQuery); } }
//UPDATE private void Atualizar(Disciplina disciplina) { var strQuery = ""; strQuery += " UPDATE Disciplina SET "; strQuery += string.Format(" Nome = '{0}', CargaHoraria = '{1}' ", disciplina.Nome, disciplina.CargaHoraria); strQuery += string.Format(" WHERE DisciplinaId = {0} ", disciplina.Id); using (conexao = new Conexao()) { conexao.ExecutaComando(strQuery); } }
//UPDATE private void Atualizar(Cursos cursos) { var strQuery = ""; strQuery += " UPDATE Cursos SET"; strQuery += string.Format(" Nome = '{0}'", cursos.Nome); using (conexao = new Conexao()) { conexao.ExecutaComando(strQuery); } }
//INSERT private void Inserir(Disciplina disciplina) { var strQuery = ""; strQuery += " INSERT INTO Disciplina (Nome, CargaHoraria) "; strQuery += string.Format(" VALUES ('{0}','{1}') ", disciplina.Nome, disciplina.CargaHoraria ); using (conexao = new Conexao()) { conexao.ExecutaComando(strQuery); } }
//UPDATE private void Atualizar(Turmas turma) { var strQuery = ""; strQuery += " UPDATE Turmas SET "; strQuery += string.Format(" Nome = '{0}' ", turma.Nome); strQuery += string.Format(" WHERE TurmasId = {1} ", turma.TurmasID); using (conexao = new Conexao()) { conexao.ExecutaComando(strQuery); } }
/*//INSERT private void Inserir(Usuarios login) { seguranca = new Seguranca(); var strQuery = ""; strQuery += " INSERT INTO Login (Login, Senha, Perfil) "; strQuery += string.Format(" VALUES ('{0}','{1}','{2}') ", login.Email, seguranca.Criptografar(login.Senha), login.Perfil); using (conexao = new Conexao()) { conexao.ExecutaComando(strQuery); } }*/ private void Inserir() { var strQuery = ""; strQuery += " INSERT INTO Login SELECT * FROM LoginTemp "; using (conexao = new Conexao()) { conexao.ExecutaComando(strQuery); } //Exclui Tabela temporária var loginTemp = new TabelaTempDAO(); loginTemp.ExcluirTemp("LoginTemp"); }
public void InserirProfessorDisciplina(int[] disciplinaId) { //Insere dados na tabela Professor_Disciplina var IdentProfessor = IdentCurrent("ProfessorId", "dbo.Professores"); for (int i = 0; i < disciplinaId.Length; i++) { var strQuery = ""; strQuery += " INSERT INTO PROFESSOR_DISCIPLINA (FKProfessoresId, FKDisciplinaId) "; strQuery += string.Format(" VALUES ({0}, {1})", IdentProfessor, disciplinaId[i]); using (conexao = new Conexao()) { conexao.ExecutaComando(strQuery); } } }
//TESTA SE EXISTE TABELA TEMPORARIA E QUANTOS REGISTRO ELA POSSUI public int Count(string table, string coluna, string valorcoluna) { var strQuery = ""; strQuery += string.Format(" SELECT COUNT(*) FROM {0} WHERE {1} = '{2}'", table, coluna, valorcoluna); try { using (conexao = new Conexao()) { var objeto = conexao.ExecutaSelectObj(strQuery); int count = int.Parse(objeto.ToString()); return count; } } catch (SqlException e) { return -1; } }
//SELECT CURRENT IDENTITY public int IdentCurrent(string camp, string tab) { try { using (conexao = new Conexao()) { var strQuery = string.Format(" SELECT {0} FROM {1} WHERE {0} = IDENT_CURRENT('{1}')", camp, tab); var objeto = conexao.ExecutaSelectObj(strQuery); int Id = int.Parse(objeto.ToString()); return Id; } } catch (SqlException e) { return 0; } catch (Exception e) { return -1; } }
//UPDATE private void Atualiza(Usuarios login) { var strQuery = ""; strQuery += " UPDATE Login SET "; strQuery += string.Format(" Login = '******', Senha = '{1}', Perfil = {2} ", login.login, login.Senha, login.Perfil); strQuery += string.Format(" WHERE LoginId = {0} ", login.LoginId); using (conexao = new Conexao()) { conexao.ExecutaComando(strQuery); } }
//SELECT CURRENT IDENTITY private int IdentCurrent(string camp, string tab) { using (conexao = new Conexao()) { var strQuery = string.Format(" SELECT {0} FROM {1} WHERE {0} = IDENT_CURRENT('{1}')", camp, tab); var objeto = conexao.ExecutaSelectObj(strQuery); int Id = int.Parse(objeto.ToString()); return Id; } }
public static List <MSugestao> Pesquisar(int tipo, bool opcao, string date) { List <MSugestao> retorno = null; if (Conexao.Abrir()) { SqlCommand command; if (opcao == true) { command = new SqlCommand { Connection = Conexao.Connection, CommandText = "select s.ID[ID], s.FKTipoSugestaoID[FKTipoSugestaoID], s.DataEnvio[DataEnvio], s.FKUsuarioID[FKUsuarioID], (select u.Nome from TBUsuario u where u.ID = s.FKUsuarioID)[NomeUsuario], t.Nome[NomeTipo] from TBSugestao s join TBTipoSugestao t on (t.ID = s.FKTipoSugestaoID) and s.FKTipoSugestaoID = @tipo and s.DataEnvio >= @data" }; } else { command = new SqlCommand { Connection = Conexao.Connection, CommandText = "select s.ID[ID], s.FKTipoSugestaoID[FKTipoSugestaoID], s.DataEnvio[DataEnvio], s.FKUsuarioID[FKUsuarioID], (select u.Nome from TBUsuario u where u.ID = s.FKUsuarioID)[NomeUsuario], t.Nome[NomeTipo] from TBSugestao s join TBTipoSugestao t on (t.ID = s.FKTipoSugestaoID) and s.FKTipoSugestaoID = @tipo and s.DataEnvio <= @data" }; } SqlParameter parameter; SqlDataReader reader = null; parameter = new SqlParameter("@tipo", SqlDbType.Int); parameter.Value = tipo; command.Parameters.Add(parameter); parameter = new SqlParameter("@data", SqlDbType.DateTime); parameter.Value = date; command.Parameters.Add(parameter); try { reader = command.ExecuteReader(); while (reader.Read()) { if (retorno == null) { retorno = new List <MSugestao>(); } MSugestao sugestao = new MSugestao(); sugestao.ID = (int)reader["ID"]; sugestao.FKTipoSugestaoID = (int)reader["FKTipoSugestaoID"]; sugestao.DataEnvio = (DateTime)reader["DataEnvio"]; sugestao.NomeTipoSugestao = (string)reader["NomeTipo"]; try { sugestao.FKUsuarioID = (int)reader["FKUsuarioID"]; sugestao.NomeUsuario = (string)reader["NomeUsuario"]; } catch { sugestao.FKUsuarioID = null; sugestao.NomeUsuario = ""; } retorno.Add(sugestao); } } finally { if (reader != null) { reader.Close(); } Conexao.Fechar(); } } return(retorno); }
public static List <MUsuario> Pesquisar(MUsuario u) { List <MUsuario> usuarios = null; try { Conexao.Abrir(); SqlCommand comando = new SqlCommand { CommandText = "SELECT ID, NOME, EMAIL, SITUACAO, FKTipoUsuarioID FROM TBUsuario ", Connection = Conexao.Connection }; //if(0 != u.ID) //{ // comando.CommandText += "AND ID LIKE %@ID% "; // SqlParameter param = new SqlParameter("@ID", SqlDbType.Int) { Value = u.ID }; // comando.Parameters.Add(param); //} if (u.Nome != "") { comando.CommandText += "AND NOME LIKE @NOME "; SqlParameter param = new SqlParameter("@NOME", SqlDbType.VarChar) { Value = u.Nome }; param.Value = u.Nome; comando.Parameters.Add(param); } if (u.Situacao != "") { comando.CommandText += "AND SITUACAO = @SITUACAO "; SqlParameter param = new SqlParameter("@SITUACAO", SqlDbType.Char) { Value = u.Situacao }; param.Value = u.Situacao; comando.Parameters.Add(param); } SqlDataReader reader = comando.ExecuteReader(); while (reader.Read()) { if (usuarios == null) { usuarios = new List <MUsuario>(); } MUsuario usuario = new MUsuario { ID = (int)reader["ID"], Nome = reader["NOME"].ToString(), Situacao = reader["SITUACAO"].ToString(), Email = reader["EMAIL"].ToString(), FKTipoUsuarioID = (int)reader["FKTipoUsuarioID"] }; usuarios.Add(usuario); } reader.Close(); } catch { Conexao.Fechar(); } return(usuarios); }
public static void Inserir(MUsuario u) { if (u == null) { throw new ArgumentNullException(nameof(u)); } try { Conexao.Abrir(); SqlCommand comando = new SqlCommand { CommandText = "" + "INSERT INTO " + "TBUSUARIO " + "VALUES " + "(@NOME, @EMAIL,CONVERT(CHAR(64), HASHBYTES('SHA2_256', '@SENHA'), 2), @SITUACAO, @FKTIPOUSUARIOID)", Connection = Conexao.Connection }; SqlParameter param = new SqlParameter("@NOME", SqlDbType.VarChar) { Value = u.Nome }; comando.Parameters.Add(param); param = new SqlParameter("@EMAIL", SqlDbType.VarChar) { Value = u.Email }; comando.Parameters.Add(param); param = new SqlParameter("@SITUACAO", SqlDbType.Char) { Value = u.Situacao }; comando.Parameters.Add(param); param = new SqlParameter("@FKTIPOUSUARIOID ", SqlDbType.Int) { Value = u.FKTipoUsuarioID }; comando.Parameters.Add(param); param = new SqlParameter("@SENHA ", SqlDbType.Int) { Value = u.Senha }; comando.Parameters.Add(param); if (0 < comando.ExecuteNonQuery()) { throw new Exception(Erros.ErroGeral); } } catch { Conexao.Fechar(); } }
//SELECT * WHERE public List<Turmas> ListarPorId(int id) { using (conexao = new Conexao()) { var strQuery = string.Format(" SELECT * FROM Turmas WHERE FKCursosId = {0} ", id); var retornoDataReader = conexao.ExecutaSelect(strQuery); return ListaDeObjetos(retornoDataReader); } }
//INSERT private void Inserir(Usuarios login) { var strQuery = ""; strQuery += " INSERT INTO Login (Login, Senha, Perfil) "; strQuery += string.Format(" VALUES ('{0}','{1}','{2}') ", login.Email, login.Senha, login.Perfil); using (conexao = new Conexao()) { conexao.ExecutaComando(strQuery); } }
//INSERT private void Inserir(Usuarios professor) { var identLogin = IdentCurrent("LoginId", "dbo.Login"); //Recupera último id inserido na tabela Login var strQuery = ""; strQuery += " INSERT INTO PROFESSORES (FKLoginId, Nome, Sobrenome, Endereco, CPF, Data_Nascimento, Email) "; strQuery += string.Format(" VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}') ", identLogin, professor.Nome, professor.Sobrenome, professor.Endereco, professor.CPF, professor.Data_Nascimento, professor.Email ); using (conexao = new Conexao()) { conexao.ExecutaComando(strQuery); } }
public static void EnviarSugestao(MSugestao sugestao) { if (sugestao.FKUsuarioID == null) { if (Conexao.Abrir()) { SqlCommand command = new SqlCommand { Connection = Conexao.Connection, CommandText = "INSERT into TBSugestao(FKTipoSugestaoID, Texto, DataEnvio) VALUES(@FKTipoSugestaoID, @Texto, @DataEnvio)" }; SqlParameter parameter; parameter = new SqlParameter("@FKTipoSugestaoID", SqlDbType.Int); parameter.Value = sugestao.FKTipoSugestaoID; command.Parameters.Add(parameter); parameter = new SqlParameter("@Texto", SqlDbType.Text); parameter.Value = sugestao.Texto; command.Parameters.Add(parameter); parameter = new SqlParameter("@DataEnvio", SqlDbType.DateTime); parameter.Value = sugestao.DataEnvio; command.Parameters.Add(parameter); try { command.ExecuteNonQuery(); } catch { } Conexao.Fechar(); } } else { if (Conexao.Abrir()) { SqlCommand command = new SqlCommand { Connection = Conexao.Connection, CommandText = "INSERT into TBSugestao(FKTipoSugestaoID, Texto, DataEnvio, FKUsuarioID) VALUES(@FKTipoSugestaoID, @Texto, @DataEnvio, @FKUsuarioID)" }; SqlParameter parameter; parameter = new SqlParameter("@FKTipoSugestaoID", SqlDbType.Int); parameter.Value = sugestao.FKTipoSugestaoID; command.Parameters.Add(parameter); parameter = new SqlParameter("@Texto", SqlDbType.Text); parameter.Value = sugestao.Texto; command.Parameters.Add(parameter); parameter = new SqlParameter("@DataEnvio", SqlDbType.DateTime); parameter.Value = sugestao.DataEnvio; command.Parameters.Add(parameter); parameter = new SqlParameter("@FKUsuarioID", SqlDbType.Int); parameter.Value = sugestao.FKUsuarioID; command.Parameters.Add(parameter); try { command.ExecuteNonQuery(); } catch { } Conexao.Fechar(); } } }
/*//UPDATE private void Atualiza(Usuarios login) { seguranca = new Seguranca(); var strQuery = ""; strQuery += " UPDATE Login SET "; strQuery += string.Format(" Login = '******', Senha = '{1}', Perfil = {2} ", login.login, seguranca.Criptografar(login.Senha), login.Perfil); strQuery += string.Format(" WHERE LoginId = {0} ", login.LoginId); using (conexao = new Conexao()) { conexao.ExecutaComando(strQuery); } }*/ //UPDATE private void Atualiza(Usuarios login) { var strQuery = ""; strQuery += " UPDATE Login SET "; strQuery += " Login.Login = LT.Login, Login.Perfil = LT.Perfil "; strQuery += " FROM Login INNER JOIN LoginTemp AS LT ON Login.Login = LT.Login "; strQuery += string.Format(" WHERE Login.LoginId = (SELECT FKLoginId FROM Professores WHERE ProfessorId = {0})", login.ProfessorId); using (conexao = new Conexao()) { conexao.ExecutaComando(strQuery); } //Exclui Tabela temporária var loginTemp = new TabelaTempDAO(); loginTemp.ExcluirTemp("LoginTemp"); }
//UPDATE private void Atualizar(Usuarios professor) { var strQuery = ""; strQuery += " UPDATE Professores SET"; strQuery += string.Format(" Nome = '{0}', Sobrenome = '{1}', Endereco = '{2}', CPF = '{3}', Data_Nascimento = '{4}', Email = '{5}'", professor.Nome, professor.Sobrenome, professor.Endereco, professor.CPF, professor.Data_Nascimento, professor.Email ); strQuery += string.Format(" WHERE ProfessorId = {0} ", professor.ProfessorId); using (conexao = new Conexao()) { conexao.ExecutaComando(strQuery); } }
public int BuscaCodProfessor(string Email) { using (conexao = new Conexao()) { var strQuery = string.Format(" SELECT ProfessorId FROM Professores WHERE Email = '{0}' ", Email); int codigo = int.Parse(conexao.ExecutaSelectObj(strQuery).ToString()); return codigo; } }
public static void Atualizar(MUsuario u) { if (u == null) { throw new ArgumentNullException(nameof(u)); } try { Conexao.Abrir(); SqlCommand comando = new SqlCommand { CommandText = "" + "UPDATE TBUSUARIO " + "SET" + "NOME = @NOME , EMAIL = @EMAIL, SITUACAO = @SITUACAO, FKTIPOUSUARIOID = @FKTIPOUSUARIOID, Senha = @SENHA WHERE ID = @ID", Connection = Conexao.Connection, }; SqlParameter param = new SqlParameter("@ID", SqlDbType.Int); param.Value = u.ID; comando.Parameters.Add(param); param = new SqlParameter("@NOME", SqlDbType.VarChar) { Value = u.Nome }; comando.Parameters.Add(param); param = new SqlParameter("@EMAIL", SqlDbType.VarChar) { Value = u.Email }; comando.Parameters.Add(param); param = new SqlParameter("@SITUACAO", SqlDbType.Char) { Value = u.Situacao }; comando.Parameters.Add(param); param = new SqlParameter("@FKTIPOUSUARIOID ", SqlDbType.Int) { Value = u.FKTipoUsuarioID }; comando.Parameters.Add(param); param = new SqlParameter("@SENHA", SqlDbType.VarChar) { Value = u.Senha }; comando.Parameters.Add(param); if (0 < comando.ExecuteNonQuery()) { throw new Exception(Erros.ErroGeral); } } catch { Conexao.Fechar(); } }