Example #1
0
        /// <summary>
        /// Selects the data list by some conditions.
        /// </summary>
        /// <typeparam name="T">The type of data.</typeparam>
        /// <param name="tableName">Name of the table.</param>
        /// <param name="conditions">The conditions.</param>
        /// <returns></returns>
        public List <T> Select <T>(string tableName, Dictionary <string, object> conditions) where T : class, new()
        {
            try
            {
                using (m_db.Cube())
                {
                    string sql    = "from " + tableName + " where";
                    int    i      = 0;
                    int    length = conditions.Keys.Count;

                    foreach (string key in conditions.Keys)
                    {
                        sql += " " + key + "==?";

                        if (i < length - 1)
                        {
                            sql += " &";
                        }

                        i++;
                    }

                    List <object>    values = new List <object>(conditions.Values);
                    IBEnumerable <T> items  = m_db.Select <T>(sql, values.ToArray());
                    return(new List <T>(items));
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #2
0
        /// <summary>
        /// 保存多条数据
        /// </summary>
        public void SaveDatas <T>(Dictionary <T, object> dataMap) where T : class
        {
            Type type = typeof(T);

            string tableName = type.Name;
            string keyName   = _keyMap[type];

            IBox   box    = _autoBox.Cube();
            Binder binder = box.Bind(tableName);

            foreach (KeyValuePair <T, object> kv in dataMap)
            {
                T      dbObj    = kv.Key;
                object keyValue = kv.Value;

                if (_autoBox.SelectCount(string.Format("from {0} where {1} == ?", tableName, keyName), keyValue) <= 0)
                {
                    binder.Insert(dbObj);
                }
                else
                {
                    binder.Update(dbObj);
                }
            }

            box.Commit();
            box.Dispose();
        }