예제 #1
0
        /// <summary>
        /// Updates an Entity in the database, provided that none of the Primary
        /// keys were modified.
        /// </summary>
        /// <remarks>This method utilizes the <see cref="PreparedNonQuery"/> to speed things along.</remarks>
        /// <param name="obj">The <see cref="TEntity"/> object to update in the dataset</param>
        /// <returns>true if any records in the database were affected; false otherwise.</returns>
        public bool Update(TEntity obj)
        {
            if (UpdateQuery == null)
            {
                using (var updateQuery = new UpdateQueryBuilder(EntityTable.TableName, Context))
                {
                    // Generate the SQL
                    foreach (var attribute in EntityTable.Columns)
                    {
                        // Keys go in the WHERE statement, not the SET statement
                        if (EntityTable.PrimaryKeys.Contains(attribute.Key))
                        {
                            PropertyInfo info = attribute.Value.Property;
                            updateQuery.Where(attribute.Key, Comparison.Equals, new SqlLiteral($"@{attribute.Key}"));
                        }
                        else
                        {
                            updateQuery.Set(attribute.Key, new SqlLiteral($"@{attribute.Key}"));
                        }
                    }

                    UpdateQuery = new PreparedNonQuery(updateQuery.BuildCommand());
                }
            }

            lock (UpdateQuery)
            {
                // Update parameters and execute the SQL Command
                UpdateQuery.SetParameters(obj, EntityTable);
                return(UpdateQuery.Execute() == 1);
            }
        }
예제 #2
0
        public bool Update(Expression <Func <T, dynamic> > fieldUpdate, Expression <Func <T, bool> > whereClause, object source)
        {
            IDbCommand cmd = connection.CreateCommand();

            cmd.CommandType = CommandType.Text;
            bool result = false;
            var  Query  = new UpdateQuery(Entity);

            cmd.CommandText = Query.GetQueryWithParameter(fieldUpdate, whereClause, source);
            Query.SetParameters(cmd);


            try
            {
                if (cmd.ExecuteNonQuery() > 0)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw new System.Exception(ex.Message);
            }
            return(result);
        }