Exemplo n.º 1
0
        public T Create(T model)
        {
            MySqlParameter[] parameters = GetStoreValues(model);
            if (parameters.Length == 0)
            {
                throw new Exception("The array cannot be empty");
            }
            string columns = parameters[0].ParameterName,
                   values  = String.Concat("@", parameters[0].ParameterName);

            for (int i = 1; i < parameters.Length; i++)
            {
                columns = string.Concat(columns, ", ", parameters[i].ParameterName);
                values  = string.Concat(values, ", @", parameters[i].ParameterName);
            }

            string query = String.Format("INSERT INTO {0} ({1}) VALUES ({2})",
                                         TableName, columns, values);

            var result = MyDbConnection.Query(query, parameters) == 1;
            var id     = Convert.ToInt32(MyDbConnection.ResponseQuery("SELECT LAST_INSERT_ID() as id").Rows[0][0]);

            if (SetId(model, id))
            {
                return(model);
            }
            else
            {
                throw new Exception("Invalid primary key");
            }
        }
Exemplo n.º 2
0
        public IEnumerable <T> ExecuteSelectQuery(string query, DbParameter[] parameters)
        {
            var result = MyDbConnection.ResponseQuery(query, parameters);

            return(CreateModelCollection(result.Rows));
        }