예제 #1
0
        /// <summary>
        /// Updates data.
        /// </summary>
        /// <typeparam name="T">The type definition of data.</typeparam>
        /// <param name="tableName">Name of the table.</param>
        /// <param name="values">The list of data.</param>
        /// <returns><c>true</c> if update the list of data success, <c>false</c> otherwise.</returns>
        public bool Update <T>(string tableName, params T[] values) where T : class
        {
            try
            {
                if (database != null && values != null)
                {
                    using (IBox box = database.Cube())
                    {
                        for (int i = 0, length = values.Length; i < length; ++i)
                        {
                            T data = values[i];
                            box.Bind(tableName).Update <T>(data);
                        }

                        CommitResult result = box.Commit();

                        if (result.Equals(CommitResult.OK))
                        {
                            return(true);
                        }
                        else
                        {
                            Debug.LogError(result.GetErrorMsg(box));
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Debug.LogException(exception);
            }

            return(false);
        }
예제 #2
0
        /// <summary>
        /// Deletes data by the given primary key value.
        /// </summary>
        /// <param name="tableName">Name of the table.</param>
        /// <param name="primaryKeyValues">The primary key values.</param>
        /// <returns><c>true</c> if delete data success, <c>false</c> otherwise.</returns>
        public bool Delete(string tableName, params object[] primaryKeyValues)
        {
            try
            {
                if (database != null && primaryKeyValues != null)
                {
                    using (IBox box = database.Cube())
                    {
                        for (int i = 0, length = primaryKeyValues.Length; i < length; ++i)
                        {
                            object primaryKeyValue = primaryKeyValues[i];
                            box.Bind(tableName, primaryKeyValue).Delete();
                        }

                        CommitResult result = box.Commit();

                        if (result.Equals(CommitResult.OK))
                        {
                            return(true);
                        }
                        else
                        {
                            Debug.LogError(result.GetErrorMsg(box));
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Debug.LogException(exception);
            }

            return(false);
        }
예제 #3
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();
        }