public DataTable Query(string queryString)
        {
            DataTable dt = null;

            using (DbConnection connection = sqlProvider.CreateConnection())
            {
                using (DbCommand command = connection.CreateCommand())
                {
                    command.CommandText = queryString;
                    try
                    {
                        connection.Open();
                        using (DbDataReader dr = command.ExecuteReader())
                        {
                            dt = new DataTable();
                            dt.Load(dr);
                        }
                    }
                    catch
                    {
                        throw;
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return(dt);
        }
Example #2
0
        public override void Save(ProductModel model)
        {
            SQLCaller caller = new SQLCaller(sqlFactory);

            DbConnection connection = sqlFactory.CreateConnection();

            DbParameter[] inputParameters = sqlFactory.CreateInputParameters(new Dictionary <string, object>()
            {
                { ID_COLUMN, model.Identity },
                { IMAGEFILENAME_COLUMN, model.ImageFilename },
                { NAME_COLUMN, model.Name },
                { BRAND_COLUMN, model.Brand },
                { DESCRIPTION_COLUMN, model.Description },
                { UNITPRICE_COLUMN, model.UnitPrice },
                { QUANTITY_COLUMN, model.Quantity },
            });

            DbCommand command = sqlFactory.CreateCommand(INSERT_STATEMENT, inputParameters);

            caller.ExecuteNonQuery(command);
        }
        public override void Save(UserModel model)
        {
            SQLCaller caller = new SQLCaller(sqlFactory);

            DbConnection connection = sqlFactory.CreateConnection();

            DbParameter[] inputParameters = sqlFactory.CreateInputParameters(new Dictionary <string, object>()
            {
                { ID_COLUMN, model.Identity },
                { FIRSTNAME_COLUMN, model.Firstname },
                { MIDDLENAME_COLUMN, model.Middlename },
                { LASTNAME_COLUMN, model.Lastname },
                { GENDER_COLUMN, model.Gender },
                { BIRTHDATE_COLUMN, model.BirthDate },
                { CONTACTNUMBER_COLUMN, model.ContactNumber },
                { EMAIL_COLUMN, model.Email },
                { ADDRESS_COLUMN, model.Address }
            });

            DbCommand command = sqlFactory.CreateCommand(INSERT_STATEMENT, inputParameters);

            caller.ExecuteNonQuery(command);
        }
        public override void Save(AccountModel model)
        {
            SQLCaller caller = new SQLCaller(sqlFactory);

            DbConnection connection = sqlFactory.CreateConnection();

            DbParameter[] inputParameters = sqlFactory.CreateInputParameters(new Dictionary <string, object>()
            {
                { USERNAME_COLUMN, model.Username },
                { HASHEDPASSWORD_COLUMN, model.Password.HashedPassword },
                { SALT_COLUMN, model.Password.Salt },
                { ACCESSTYPE_COLUMN, model.AccessType },
                { ACTIVE_COLUMN, model.IsActive }
            });

            DbParameter idParameter = sqlFactory.CreateOutputParameter(ID_COLUMN);

            DbCommand command = sqlFactory.CreateCommand(INSERT_STATEMENT, inputParameters, new DbParameter[] { idParameter });

            try
            {
                caller.ExecuteNonQuery(command);
                model.Identity = Convert.ToInt32(idParameter.Value);
            }
            catch (MySqlException e)
            {
                //Handles violation of unique constraint for username
                if (e.ErrorCode == 1062)
                {
                    throw new InvalidValueDuplicationException("Username", model.Username);
                }
                else
                {
                    throw;
                }
            }
        }