public void limpioInconsistencia(Cliente clienteActualizado) { //PRIMERO LIMPIO LAS IDENTIDADES DUPLICADAS RepositorioIdentidad repoIdentidad = new RepositorioIdentidad(); List <Identidad> identidadesDuplicadas = new List <Identidad>(); //Configuraciones de la consulta String connectionString = ConfigurationManager.AppSettings["BaseLocal"]; SqlConnection sqlConnection = new SqlConnection(connectionString); SqlCommand sqlCommand = new SqlCommand(); SqlDataReader reader; sqlCommand.Parameters.AddWithValue("@idIdentidad", clienteActualizado.getIdentidad().getIdIdentidad()); sqlCommand.Parameters.AddWithValue("@Mail", clienteActualizado.getIdentidad().getMail()); sqlCommand.Parameters.AddWithValue("@Num", clienteActualizado.getIdentidad().getNumeroDocumento()); sqlCommand.Parameters.AddWithValue("@Tipo", clienteActualizado.getIdentidad().getTipoDocumento()); sqlCommand.CommandType = CommandType.Text; sqlCommand.Connection = sqlConnection; sqlCommand.CommandText = @" SELECT identidad.idIdentidad FROM LOS_BORBOTONES.Identidad identidad WHERE identidad.idIdentidad <> @idIdentidad AND (identidad.TipoDocumento = @Tipo AND identidad.NumeroDocumento = @Num) UNION SELECT identidad.idIdentidad FROM LOS_BORBOTONES.Identidad identidad WHERE identidad.idIdentidad <> @idIdentidad AND (identidad.Mail = @Mail) "; sqlConnection.Open(); reader = sqlCommand.ExecuteReader(); while (reader.Read()) { identidadesDuplicadas.Add(repoIdentidad.getById(reader.GetInt32(reader.GetOrdinal("idIdentidad")))); } //Cierro Primera Consulta sqlConnection.Close(); identidadesDuplicadas.ForEach(identDup => repoIdentidad.limpiarDuplicadoMarcarInconsistente(identDup)); }
override public void update(Cliente cliente) { RepositorioIdentidad repoIdentidad = new RepositorioIdentidad(); //AGREGO VALIDACIONES AL UPDATE if (repoIdentidad.yaExisteOtraIdentidadMismoMailOTipoYNumDoc(cliente.getIdentidad())) { throw new ElementoYaExisteException("Ya existe un cliente o un usuario con el mismo mail o tipo y numero de documento."); } if (this.exists(cliente)) { String connectionString = ConfigurationManager.AppSettings["BaseLocal"]; SqlConnection sqlConnection = new SqlConnection(connectionString); SqlCommand sqlCommand = new SqlCommand(); SqlDataReader reader; sqlCommand.CommandType = CommandType.Text; sqlCommand.Connection = sqlConnection; //PARAMETERS DE LA DIRECCION sqlCommand.Parameters.AddWithValue("@Pais", cliente.getIdentidad().getDireccion().getPais()); sqlCommand.Parameters.AddWithValue("@Ciudad", cliente.getIdentidad().getDireccion().getCiudad()); sqlCommand.Parameters.AddWithValue("@Calle", cliente.getIdentidad().getDireccion().getCalle()); sqlCommand.Parameters.AddWithValue("@NumeroCalle", cliente.getIdentidad().getDireccion().getNumeroCalle()); sqlCommand.Parameters.AddWithValue("@Piso", cliente.getIdentidad().getDireccion().getPiso()); sqlCommand.Parameters.AddWithValue("@Departamento", cliente.getIdentidad().getDireccion().getDepartamento()); sqlCommand.Parameters.AddWithValue("@idDireccion", cliente.getIdentidad().getDireccion().getIdDireccion()); //PARAMETERS DE LA IDENTIDAD sqlCommand.Parameters.AddWithValue("@TipoIdent", cliente.getIdentidad().getTipoIdentidad()); sqlCommand.Parameters.AddWithValue("@Nombre", cliente.getIdentidad().getNombre()); sqlCommand.Parameters.AddWithValue("@Apellido", cliente.getIdentidad().getApellido()); sqlCommand.Parameters.AddWithValue("@TipoDoc", cliente.getIdentidad().getTipoDocumento()); sqlCommand.Parameters.AddWithValue("@NroDoc", cliente.getIdentidad().getNumeroDocumento()); sqlCommand.Parameters.AddWithValue("@Mail", cliente.getIdentidad().getMail()); sqlCommand.Parameters.AddWithValue("@FecNac", cliente.getIdentidad().getFechaNacimiento()); sqlCommand.Parameters.AddWithValue("@Nacion", cliente.getIdentidad().getNacionalidad()); sqlCommand.Parameters.AddWithValue("@Tel", cliente.getIdentidad().getTelefono()); sqlCommand.Parameters.AddWithValue("@idIdentidad", cliente.getIdentidad().getIdIdentidad()); //PARAMETERS DEL CLIENTE ///////////////////////// //POR EL MOMENTO NO CONSIDERAMOS LAS RESERVAS EN EL CLIENTE EN EL UPDATE ///////////////////////// sqlCommand.Parameters.AddWithValue("@Activo", cliente.getActivo()); sqlCommand.Parameters.AddWithValue("@idCliente", cliente.getIdCliente()); StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.Append(@" BEGIN TRY BEGIN TRANSACTION UPDATE LOS_BORBOTONES.Direccion SET Pais = @Pais, Ciudad = @Ciudad, Calle = @Calle, NumeroCalle = @NumeroCalle, Piso = @Piso, Depto = @Departamento WHERE idDireccion = @idDireccion; UPDATE LOS_BORBOTONES.Identidad SET TipoIdentidad = @TipoIdent, Nombre = @Nombre, Apellido = @Apellido, TipoDocumento = @TipoDoc, NumeroDocumento = @NroDoc, Mail = @Mail, FechaNacimiento = @FecNac, Nacionalidad = @Nacion, Telefono = @Tel WHERE idIdentidad = @idIdentidad; UPDATE LOS_BORBOTONES.Cliente SET Activo = @Activo, idIdentidad = @idIdentidad, Inconsistente = 0 WHERE idCliente = @idCliente; COMMIT END TRY BEGIN CATCH ROLLBACK END CATCH "); //LA LOGICA EN PONER 'INCONSISTENTE = 0' EN TODOS LOS UPDATE //ES QUE POR GUI YO AVISO QUE EL CLIENTE ESTA INCONSISTENTE //SI EL USUARIO DEL SISTEMA DECIDE CONTINUAR EDITANDOLO DEBE GARANTIZAR QUE //VERIFICA LA IDENTIDAD DEL CLIENTE MEDIANTE DOCUMENTO Y VALIDA SU MAIL sqlCommand.CommandText = sqlBuilder.ToString(); sqlConnection.Open(); reader = sqlCommand.ExecuteReader(); sqlConnection.Close(); } else { throw new NoExisteIDException("No existe el cliente que intenta actualizar"); } }
override public int create(Cliente cliente) { int idCliente = 0; RepositorioIdentidad repoIdentidad = new RepositorioIdentidad(); if (this.exists(cliente) || repoIdentidad.exists(cliente.getIdentidad())) { throw new ElementoYaExisteException("Ya existe un cliente o un usuario con el mismo documento o el mismo mail."); } else { //llamo a crear la identidad y traigo el IdIdentidad int idIdentidad = repoIdentidad.create(cliente.getIdentidad()); //llamo a crear la direccion y traigo el IdDireccion, le seteo el idIdentidad que lo necesita cliente.getIdentidad().getDireccion().setIdIdentidad(idIdentidad); RepositorioDireccion repoDireccion = new RepositorioDireccion(); int idDireccion = repoDireccion.create(cliente.getIdentidad().getDireccion()); //ahora ya tengo todo para crear el cliente String connectionString = ConfigurationManager.AppSettings["BaseLocal"]; SqlConnection sqlConnection = new SqlConnection(connectionString); SqlCommand sqlCommand = new SqlCommand(); SqlDataReader reader; sqlCommand.CommandType = CommandType.Text; sqlCommand.Connection = sqlConnection; sqlCommand.Parameters.AddWithValue("@Activo", cliente.getActivo()); sqlCommand.Parameters.AddWithValue("@idIdentidad", idIdentidad); StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.Append(@" BEGIN TRY BEGIN TRANSACTION INSERT INTO LOS_BORBOTONES.Cliente(Activo,idIdentidad) OUTPUT INSERTED.idCliente VALUES(@Activo,@idIdentidad); DECLARE @idCliente int; SET @idCliente = SCOPE_IDENTITY(); "); sqlBuilder.Append(@" COMMIT END TRY BEGIN CATCH ROLLBACK END CATCH "); sqlCommand.CommandText = sqlBuilder.ToString(); sqlConnection.Open(); reader = sqlCommand.ExecuteReader(); if (reader.Read()) { idCliente = reader.GetInt32(reader.GetOrdinal("idCliente")); } sqlConnection.Close(); } return(idCliente); }
override public Cliente getById(int idCliente) { //Elementos del Cliente a devolver Cliente cliente; int idIdentidad = 0; Identidad identidad = null; RepositorioIdentidad repoIdentidad = new RepositorioIdentidad(); Boolean activo = false; List <Reserva> reservas = new List <Reserva>(); Boolean inconsistente = false; //Configuraciones de la consulta String connectionString = ConfigurationManager.AppSettings["BaseLocal"]; SqlConnection sqlConnection = new SqlConnection(connectionString); SqlCommand sqlCommand = new SqlCommand(); SqlDataReader reader; //Primera Consulta sqlCommand.Parameters.AddWithValue("@idCliente", idCliente); sqlCommand.CommandType = CommandType.Text; sqlCommand.Connection = sqlConnection; sqlCommand.CommandText = "SELECT * FROM LOS_BORBOTONES.Cliente WHERE idCliente = @idCliente"; sqlConnection.Open(); reader = sqlCommand.ExecuteReader(); while (reader.Read()) { idIdentidad = reader.GetInt32(reader.GetOrdinal("IdIdentidad")); identidad = repoIdentidad.getById(idIdentidad); activo = reader.GetBoolean(reader.GetOrdinal("Activo")); inconsistente = reader.GetBoolean(reader.GetOrdinal("Inconsistente")); } //Cierro Primera Consulta sqlConnection.Close(); //Si no encuentro elemento con ese ID tiro una excepción if (identidad.Equals(null)) { throw new NoExisteIDException("No existe cliente con el ID asociado"); } //Segunda Consulta sqlCommand.CommandText = @" SELECT r.idReserva FROM LOS_BORBOTONES.Reserva_X_Habitacion_X_Cliente rhc INNER JOIN LOS_BORBOTONES.Reserva r ON r.idReserva = rhc.idReserva WHERE rhc.idCliente = @idCliente "; sqlConnection.Open(); reader = sqlCommand.ExecuteReader(); //Colecto las reservas while (reader.Read()) { int idReserva = reader.GetInt32(reader.GetOrdinal("idReserva")); //RepositorioReserva repoReserva = new RepositorioReserva(); //reservas.Add(repoReserva.getById(idReserva)); } sqlConnection.Close(); //Armo el cliente completo cliente = new Cliente(idCliente, identidad, activo, reservas, inconsistente); return(cliente); }
override public void update(Usuario usuario) { RepositorioIdentidad repoIdentidad = new RepositorioIdentidad(); //AGREGO VALIDACIONES AL UPDATE if (repoIdentidad.yaExisteOtraIdentidadMismoMailOTipoYNumDoc(usuario.getIdentidad())) { throw new ElementoYaExisteException("Ya existe un cliente o un usuario con el mismo mail o tipo y numero de documento."); } /* * if (this.yaExisteMismoMailDistintoUsuario(usuario)) * { * throw new ElementoYaExisteException("Ya existe un usuario o cliente con el mismo mail."); * } * * if (this.yaExisteMismoTipoYDocDistintoUsuario(usuario)) * { * throw new ElementoYaExisteException("Ya existe un usuario o cliente con el mismo documento."); * } */ if (this.yaExisteMismoUsername(usuario)) { throw new ElementoYaExisteException("Ya existe un usuario con el mismo nombre de usuario."); } if (this.exists(usuario)) { String connectionString = ConfigurationManager.AppSettings["BaseLocal"]; SqlConnection sqlConnection = new SqlConnection(connectionString); SqlCommand sqlCommand = new SqlCommand(); SqlDataReader reader; sqlCommand.CommandType = CommandType.Text; sqlCommand.Connection = sqlConnection; //PARAMETERS DE LA DIRECCION sqlCommand.Parameters.AddWithValue("@Pais", usuario.getIdentidad().getDireccion().getPais()); sqlCommand.Parameters.AddWithValue("@Ciudad", usuario.getIdentidad().getDireccion().getCiudad()); sqlCommand.Parameters.AddWithValue("@Calle", usuario.getIdentidad().getDireccion().getCalle()); sqlCommand.Parameters.AddWithValue("@NumeroCalle", usuario.getIdentidad().getDireccion().getNumeroCalle()); sqlCommand.Parameters.AddWithValue("@Piso", usuario.getIdentidad().getDireccion().getPiso()); sqlCommand.Parameters.AddWithValue("@Departamento", usuario.getIdentidad().getDireccion().getDepartamento()); sqlCommand.Parameters.AddWithValue("@idDireccion", usuario.getIdentidad().getDireccion().getIdDireccion()); //PARAMETERS DE LA IDENTIDAD sqlCommand.Parameters.AddWithValue("@TipoIdent", usuario.getIdentidad().getTipoIdentidad()); sqlCommand.Parameters.AddWithValue("@Nombre", usuario.getIdentidad().getNombre()); sqlCommand.Parameters.AddWithValue("@Apellido", usuario.getIdentidad().getApellido()); sqlCommand.Parameters.AddWithValue("@TipoDoc", usuario.getIdentidad().getTipoDocumento()); sqlCommand.Parameters.AddWithValue("@NroDoc", usuario.getIdentidad().getNumeroDocumento()); sqlCommand.Parameters.AddWithValue("@Mail", usuario.getIdentidad().getMail()); sqlCommand.Parameters.AddWithValue("@FecNac", usuario.getIdentidad().getFechaNacimiento()); sqlCommand.Parameters.AddWithValue("@Nacion", usuario.getIdentidad().getNacionalidad()); sqlCommand.Parameters.AddWithValue("@Tel", usuario.getIdentidad().getTelefono()); sqlCommand.Parameters.AddWithValue("@idIdentidad", usuario.getIdentidad().getIdIdentidad()); //PARAMETERS DEL USUARIO sqlCommand.Parameters.AddWithValue("@Username", usuario.getUsername()); sqlCommand.Parameters.AddWithValue("@Activo", usuario.getActivo()); sqlCommand.Parameters.AddWithValue("@Password", usuario.getPassword()); //SI CAMBIO EL ESTADO DEL USUARIO //RESETEO LA CANTIDAD DE INTENTOS FALLIDOS Boolean usuarioActivoEnBase = this.getById(usuario.getIdUsuario()).getActivo(); Boolean usuarioActivoEnModelo = usuario.getActivo(); if (!usuarioActivoEnBase && usuarioActivoEnModelo) { sqlCommand.Parameters.AddWithValue("@IntentosFallidosLogin", 0); } else { sqlCommand.Parameters.AddWithValue("@IntentosFallidosLogin", usuario.getIntentosFallidosLogin()); } sqlCommand.Parameters.AddWithValue("@idUsuario", usuario.getIdUsuario()); //HABRÍA QUE ANALIZAR PROS Y CONTRAS DE ACTUALIZAR/CREAR UN USUARIO TODO EN LA MISMA CONSULTA COMO EN ESTE METODO //O ACTUALIZARLO/CREARLO POR SEPARADO CON LOS METODOS DEL REPOSITORIO (USANDO LOS METODOS DEL REPO HAY QUE DEFINIR COMO SE MANEJA EL ROLLBACK SI UN UPDATE O CREATE FALLA) StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.Append(@" BEGIN TRY BEGIN TRANSACTION UPDATE LOS_BORBOTONES.Direccion SET Pais = @Pais, Ciudad = @Ciudad, Calle = @Calle, NumeroCalle = @NumeroCalle, Piso = @Piso, Depto = @Departamento WHERE idDireccion = @idDireccion; UPDATE LOS_BORBOTONES.Identidad SET TipoIdentidad = @TipoIdent, Nombre = @Nombre, Apellido = @Apellido, TipoDocumento = @TipoDoc, NumeroDocumento = @NroDoc, Mail = @Mail, FechaNacimiento = @FecNac, Nacionalidad = @Nacion, Telefono = @Tel WHERE idIdentidad = @idIdentidad; UPDATE LOS_BORBOTONES.Usuario SET Username = @Username, Password = @Password, IntentosFallidosLogin = @IntentosFallidosLogin, Activo = @Activo, idIdentidad = @idIdentidad WHERE idUsuario = @idUsuario; "); //TENGO QUE BORRAR TODAS LAS RELACIONES QUE TENGO CON LOS ROLES sqlBuilder.Append("DELETE FROM LOS_BORBOTONES.Rol_X_Usuario WHERE idUsuario = @idUsuario;"); //TENGO QUE BORRAR TODAS LAS RELACIONES QUE TENGO CON LOS HOTELES sqlBuilder.Append("DELETE FROM LOS_BORBOTONES.Hotel_X_Usuario WHERE idUsuario = @idUsuario;"); //AGREGO DINAMICAMENTE LOS ROLES A LA CONSULTA int i = 1; foreach (Rol r in usuario.getRoles()) { String paramName = "@idRol" + i.ToString(); sqlBuilder.AppendFormat("INSERT INTO LOS_BORBOTONES.Rol_X_Usuario(idRol,idUsuario) VALUES ({0}, @idUsuario)", paramName); sqlCommand.Parameters.AddWithValue(paramName, r.getIdRol()); i++; } //AGREGO DINAMICAMENTE LOS HOTELES A LA CONSULTA int k = 1; foreach (Hotel h in usuario.getHoteles()) { String paramName = "@idHotel" + k.ToString(); sqlBuilder.AppendFormat("INSERT INTO LOS_BORBOTONES.Hotel_X_Usuario(idHotel,idUsuario) VALUES ({0}, @idUsuario)", paramName); sqlCommand.Parameters.AddWithValue(paramName, h.getIdHotel()); k++; } sqlBuilder.Append(@" COMMIT END TRY BEGIN CATCH ROLLBACK END CATCH "); sqlCommand.CommandText = sqlBuilder.ToString(); sqlConnection.Open(); reader = sqlCommand.ExecuteReader(); sqlConnection.Close(); } else { throw new NoExisteIDException("No existe el usuario que intenta actualizar"); } }
override public Usuario getById(int idUsuario) { //Elementos del usuario a devolver Usuario usuario; Boolean activo = false; int idIdentidad = 0; Identidad identidad = null; RepositorioIdentidad repoIdentidad = new RepositorioIdentidad(); String username = ""; String password = ""; int intentosFallidosLogin = 0; List <Rol> roles = new List <Rol>(); List <Hotel> hoteles = new List <Hotel>(); //Configuraciones de la consulta String connectionString = ConfigurationManager.AppSettings["BaseLocal"]; SqlConnection sqlConnection = new SqlConnection(connectionString); SqlCommand sqlCommand = new SqlCommand(); SqlDataReader reader; //Primera Consulta sqlCommand.Parameters.AddWithValue("@idUsuario", idUsuario); sqlCommand.CommandType = CommandType.Text; sqlCommand.Connection = sqlConnection; sqlCommand.CommandText = "SELECT * FROM LOS_BORBOTONES.Usuario WHERE idUsuario = @idUsuario"; sqlConnection.Open(); reader = sqlCommand.ExecuteReader(); while (reader.Read()) { username = reader.GetString(reader.GetOrdinal("Username")); password = reader.GetString(reader.GetOrdinal("Password")); activo = reader.GetBoolean(reader.GetOrdinal("Activo")); intentosFallidosLogin = reader.GetInt32(reader.GetOrdinal("IntentosFallidosLogin")); idIdentidad = reader.GetInt32(reader.GetOrdinal("idIdentidad")); identidad = repoIdentidad.getById(idIdentidad); } //Cierro Primera Consulta sqlConnection.Close(); //Si no encuentro elemento con ese ID tiro una excepción if (username.Equals("")) { throw new NoExisteIDException("No existe usuario con el ID asociado"); } //Segunda Consulta para llenar los roles sqlCommand.CommandText = @" SELECT r.idRol FROM LOS_BORBOTONES.Rol_X_Usuario ru INNER JOIN LOS_BORBOTONES.Rol r ON r.idRol = ru.idRol WHERE ru.idUsuario = @idUsuario "; sqlConnection.Open(); reader = sqlCommand.ExecuteReader(); //Colecto los roles while (reader.Read()) { //lleno el rol con el getbyID int idRol = reader.GetInt32(reader.GetOrdinal("idRol")); RepositorioRol repoRol = new RepositorioRol(); roles.Add(repoRol.getById(idRol)); } sqlConnection.Close(); //Tercera Consulta para llenar los hoteles sqlCommand.CommandText = @" SELECT h.idHotel FROM LOS_BORBOTONES.Hotel_X_Usuario hu INNER JOIN LOS_BORBOTONES.Hotel h ON h.idHotel = hu.idHotel WHERE hu.idUsuario = @idUsuario "; sqlConnection.Open(); reader = sqlCommand.ExecuteReader(); //Colecto los hoteles while (reader.Read()) { //lleno el hotel con el getbyID int idHotel = reader.GetInt32(reader.GetOrdinal("idHotel")); RepositorioHotel repoHotel = new RepositorioHotel(); hoteles.Add(repoHotel.getById(idHotel)); } sqlConnection.Close(); //Armo el usuario completo usuario = new Usuario(idUsuario, identidad, username, password, intentosFallidosLogin, activo, roles, hoteles); return(usuario); }
override public int create(Usuario usuario) { int idUsuario = 0; RepositorioIdentidad repoIdentidad = new RepositorioIdentidad(); if (this.exists(usuario) || repoIdentidad.exists(usuario.getIdentidad())) { throw new ElementoYaExisteException("Ya existe un usuario con el mismo nombre, o su Mail/Tipo y Numero de documento está repetido."); } else { //llamo a crear la identidad y traigo el IdIdentidad //RepositorioIdentidad repoIdentidad = new RepositorioIdentidad(); int idIdentidad = repoIdentidad.create(usuario.getIdentidad()); //llamo a crear la direccion y traigo el IdDireccion, le seteo el idIdentidad que lo necesita usuario.getIdentidad().getDireccion().setIdIdentidad(idIdentidad); RepositorioDireccion repoDireccion = new RepositorioDireccion(); int idDireccion = repoDireccion.create(usuario.getIdentidad().getDireccion()); //ahora ya tengo todo para crear el usuario String connectionString = ConfigurationManager.AppSettings["BaseLocal"]; SqlConnection sqlConnection = new SqlConnection(connectionString); SqlCommand sqlCommand = new SqlCommand(); SqlDataReader reader; //encripto clave string passwordEncriptada = this.EncriptarSHA256(usuario.getPassword()); sqlCommand.CommandType = CommandType.Text; sqlCommand.Connection = sqlConnection; sqlCommand.Parameters.AddWithValue("@Username", usuario.getUsername()); sqlCommand.Parameters.AddWithValue("@Activo", usuario.getActivo()); sqlCommand.Parameters.AddWithValue("@Password", passwordEncriptada); sqlCommand.Parameters.AddWithValue("@IntentosFallidosLogin", usuario.getIntentosFallidosLogin()); sqlCommand.Parameters.AddWithValue("@idIdentidad", idIdentidad); StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.Append(@" BEGIN TRY BEGIN TRANSACTION INSERT INTO LOS_BORBOTONES.Usuario(Username,Password,IntentosFallidosLogin,Activo,idIdentidad) OUTPUT INSERTED.idUsuario VALUES(@Username,@Password,@IntentosFallidosLogin,@Activo,@idIdentidad); DECLARE @idUsuario int; SET @idUsuario = SCOPE_IDENTITY(); "); //AGREGO DINAMICAMENTE LOS ROLES A LA CONSULTA int i = 1; foreach (Rol r in usuario.getRoles()) { String paramName = "@idRol" + i.ToString(); sqlBuilder.AppendFormat("INSERT INTO LOS_BORBOTONES.Rol_X_Usuario(idRol,idUsuario) VALUES ({0}, @idUsuario)", paramName); sqlCommand.Parameters.AddWithValue(paramName, r.getIdRol()); i++; } //AGREGO DINAMICAMENTE LOS HOTELES A LA CONSULTA int k = 1; foreach (Hotel h in usuario.getHoteles()) { String paramName = "@idHotel" + k.ToString(); sqlBuilder.AppendFormat("INSERT INTO LOS_BORBOTONES.Hotel_X_Usuario(idHotel,idUsuario) VALUES ({0}, @idUsuario)", paramName); sqlCommand.Parameters.AddWithValue(paramName, h.getIdHotel()); k++; } sqlBuilder.Append(@" COMMIT END TRY BEGIN CATCH ROLLBACK END CATCH "); sqlCommand.CommandText = sqlBuilder.ToString(); sqlConnection.Open(); reader = sqlCommand.ExecuteReader(); if (reader.Read()) { idUsuario = reader.GetInt32(reader.GetOrdinal("idUsuario")); } sqlConnection.Close(); } return(idUsuario); }