Esempio n. 1
0
        public IClient GetClientById(Guid id)
        {
            IClient client = null;

            using (IDbConnection connection = _DbService.CreateDbConnection())
            {
                connection.Open();
                using (IDbCommand command = connection.CreateCommand())
                {
                    command.AddCommand("SELECT * FROM Client WHERE Id = @id");
                    command.Parameters.Add(command.CreateParameter("@id", id));

                    using (IDataReader dataReader = command.ExecuteReader(CommandBehavior.SingleRow))
                    {
                        if (dataReader.Read())
                        {
                            client = MapClient(dataReader);
                        }
                        else
                        {
                            throw new KeyNotFoundException("Client not found");
                        }
                    }
                }
            }

            return(client);
        }
Esempio n. 2
0
        public IEnumerable <ICountry> GetCountries()
        {
            List <ICountry> countryList = new List <ICountry>()
            {
            };

            using (IDbConnection connection = _DbService.CreateDbConnection())
            {
                connection.Open();
                using (IDbCommand command = connection.CreateCommand())
                {
                    command.AddCommand("SELECT * FROM Country");

                    using (IDataReader dataReader = command.ExecuteReader())
                    {
                        while (dataReader.Read())
                        {
                            countryList.Add(new Country(dataReader.GetSafeGuid(0), dataReader.GetSafeString(1)));
                        }
                    }
                }
            }

            return(countryList);
        }
 private static void ExecuteQuery(string sql)
 {
     using (IDbConnection connection = new DBService(new DbConnectionService("Connection")).CreateDbConnection())
     {
         connection.Open();
         using (IDbCommand command = connection.CreateCommand())
         {
             command.AddCommand(sql);
             command.ExecuteNonQuery();
         }
     }
 }
Esempio n. 4
0
 private void ExecuteQuery(string sql)
 {
     using (IDbConnection connection = _dbService.CreateDbConnection())
     {
         connection.Open();
         using (IDbCommand command = connection.CreateCommand())
         {
             command.AddCommand(sql);
             command.ExecuteNonQuery();
         }
     }
 }
Esempio n. 5
0
 public bool RemoveClientById(Guid id)
 {
     using (IDbConnection connection = _DbService.CreateDbConnection())
     {
         connection.Open();
         using (IDbCommand command = connection.CreateCommand())
         {
             command.AddCommand("DELETE FROM Client WHERE Id=@id;");
             command.Parameters.Add(command.CreateParameter("@id", id));
             return(command.ExecuteNonQuery() > 0);
         }
     }
 }
Esempio n. 6
0
        public void UpdateClientById(IClient client)
        {
            using (IDbConnection connection = _DbService.CreateDbConnection())
            {
                connection.Open();
                using (IDbCommand command = connection.CreateCommand())
                {
                    command.AddCommand("Update Client SET Name=@name, Address=@address, City=@city, ZipCode=@zipCode, CountryId=@countryId WHERE id=@id");
                    AddParameters(command, client);

                    if (command.ExecuteNonQuery() == 0)
                    {
                        throw new KeyNotFoundException("Client not found");
                    }
                }
            }
        }
Esempio n. 7
0
 public void AddClient(IClient newClient)
 {
     using (IDbConnection connection = _DbService.CreateDbConnection())
     {
         connection.Open();
         using (IDbCommand command = connection.CreateCommand())
         {
             command.AddCommand("INSERT INTO Client (Id, Name, Address, City, ZipCode, CountryId) VALUES (@id, @name, @address, @city, @zipCode, @countryId)");
             AddParameters(command, newClient);
             try
             {
                 command.ExecuteNonQuery();
             }
             catch (SqlException ex)
             {
                 throw new ConstraintException(ex.Message);
             }
         }
     }
 }
Esempio n. 8
0
        public IEnumerable <IClient> FilterClientsByName(string clientName)
        {
            List <IClient> clientList = new List <IClient>();

            using (IDbConnection connection = _DbService.CreateDbConnection())
            {
                connection.Open();
                using (IDbCommand command = connection.CreateCommand())
                {
                    command.AddCommand("SELECT * FROM Client WHERE lower(Name) LIKE @name;");
                    command.Parameters.Add(command.CreateParameter("@name", $"%{clientName.ToLower()}%"));
                    using (IDataReader dataReader = command.ExecuteReader())
                    {
                        while (dataReader.Read())
                        {
                            clientList.Add(MapClient(dataReader));
                        }
                    }
                }
            }
            return(clientList);
        }
Esempio n. 9
0
        public IEnumerable <IClient> GetClients()
        {
            List <IClient> clientList = new List <IClient>()
            {
            };

            using (IDbConnection connection = _DbService.CreateDbConnection())
            {
                connection.Open();
                using (IDbCommand command = connection.CreateCommand())
                {
                    command.AddCommand("SELECT * FROM Client");

                    using (IDataReader dataReader = command.ExecuteReader())
                    {
                        while (dataReader.Read())
                        {
                            clientList.Add(MapClient(dataReader));
                        }
                    }
                }
            }
            return(clientList);
        }
Esempio n. 10
0
        public IEnumerable <IClient> GetClientsByPaging(int offset, int rowsCount)
        {
            List <IClient> clientList = new List <IClient>();

            using (IDbConnection connection = _DbService.CreateDbConnection())
            {
                connection.Open();
                using (IDbCommand command = connection.CreateCommand())
                {
                    command.AddCommand("SELECT * FROM Client ORDER BY Name OFFSET @offset ROWS FETCH NEXT @rowsCount ROWS ONLY;");
                    command.Parameters.Add(command.CreateParameter("@offset", offset));
                    command.Parameters.Add(command.CreateParameter("@rowsCount", rowsCount));

                    using (IDataReader dataReader = command.ExecuteReader())
                    {
                        while (dataReader.Read())
                        {
                            clientList.Add(MapClient(dataReader));
                        }
                    }
                }
            }
            return(clientList);
        }