Пример #1
0
        /// <summary>
        /// Get all rows like collection in Table
        /// </summary>
        /// <param name="db"></param>
        /// <param name="table"></param>
        /// <returns>ICollection<Row></returns>
        public ICollection<Row> GetAll(string db, string table)
        {
            Element element;
            Row row;
            ICollection<Row> collection= new  List<Row>();
            IDictionary<object, string> d = null;
            this.GetColumn(db,table);
            Connection c = new MysqlConecction();
            IDbConnection cnn = c.Change(db);
            String query = @"Select * from " +table+" LIMIT 5;";
            try
            {
                var registro = SqlMapper.Query<object>(cnn, query, null, commandType: CommandType.Text);
                IDictionary<string, object> diccionary;
                foreach (var item in registro)
                {
                    diccionary = (IDictionary<string, object>)item;
                    row = new Row();
                    row.content = new List<Element>();
                    // Two collection in one foreach
                    //collection 1. diccionary.Keys (nw.item1)
                    //collection 2. diccionary.values (nw.item2)
                    foreach (var nw in diccionary.Keys.Zip(diccionary.Values, Tuple.Create))
                    {
                        element = new Element();
                        if (nw.Item1.Equals(column.Peek().name))
                        {
                            element.name = nw.Item1;
                            element.value = nw.Item2.ToString();
                            element.type = column.Peek().type;
                            element.isKey = column.Dequeue().isKey;
                            row.content.Add(element);
                        }
                    }
                    column = new Queue<Element>(columnBack);
                    collection.Add(row);
                }

            }
            catch (Exception e)
            {
            }
            finally
            {
                c.Close(cnn);
            }
            return collection;
        }
Пример #2
0
        public void Update(string db, string table)
        {
            Connection c = new MysqlConecction();
            IDbConnection cnn = c.Change(db);
            int indice = 1;
            Element key = new Element();
            StringBuilder update = new StringBuilder(@"UPDATE ");
            update.Append(table);
            update.Append(" SET ");
            foreach (Element item in content)
            {
                if(item.isKey==true) key = item;

                update.Append(item.name+"= ");
                if (indice < content.Count)
                {
                    if(item.type.Equals("String")|| item.type.Equals("char") || item.type.Equals("varchar") || item.type.Equals("datetime"))
                        update.Append("'"+item.value+"'" + ", ");
                    else
                        update.Append(item.value+ ", ");
                    indice++;
                }
                else
                {
                    if (item.type.Equals("String") || item.type.Equals("char") || item.type.Equals("varchar") || item.type.Equals("datetime"))
                        update.Append("'" + item.value + "'");
                    else
                        update.Append(item.value);
                    update.Append(" WHERE "+key.name+"= '"+key.value+"';");
                }
            }
            Debug.WriteLine(update.ToString());
            try
            {
                SqlMapper.Query(cnn, update.ToString(), null, commandType: CommandType.Text);
            }
            catch (Exception e)
            {
            }
            finally
            {
                c.Close(cnn);
            }
        }