public static FieldRule GetRule(int? id, int? userId)
 {
     FieldRule res = null;
     using (SqlConnectionHolder holder = SqlConnectionHelper.GetConnection(SqlConnectionHelper.ConnectionType.Naviam))
     {
         using (SqlCommand cmd = holder.Connection.CreateSPCommand("user_rules_get"))
         {
             cmd.Parameters.AddWithValue("@id_user", userId.ToDbValue());
             cmd.Parameters.AddWithValue("@id_rule", id);
             try
             {
                 using (SqlDataReader reader = cmd.ExecuteReader())
                 {
                     reader.Read();
                     if (reader.HasRows)
                     {
                         res = new FieldRule(reader);
                     }
                 }
             }
             catch (SqlException e)
             {
                 cmd.AddDetailsToException(e);
                 throw;
             }
         }
     }
     return res;
 }
 public static int InsertUpdate(FieldRule entity, int? userId, DbActionType action)
 {
     var res = -1;
     using (var holder = SqlConnectionHelper.GetConnection())
     {
         var commName = action == DbActionType.Insert ? "user_rule_create" : "user_rule_update";
         var command = holder.Connection.CreateSPCommand(commName);
         try
         {
             command.AddEntityParameters(entity, action);
             command.ExecuteNonQuery();
             if (action == DbActionType.Insert)
                 entity.Id = command.GetRowIdParameter();
             res = command.GetReturnParameter();
         }
         catch (SqlException e)
         {
             command.AddDetailsToException(e);
             throw;
         }
     }
     return res;
 }
 /// <summary>
 /// Appends Rule-specific parameters to the specificied SqlCommand. 
 /// </summary>
 /// <param name="command">SqlCommand to be executed.</param>
 /// <param name="alert">Instance of FieldRule class</param>
 /// <param name="action">Database action type (select, insert, update, delete).</param>
 public static void AddEntityParameters(this SqlCommand command, FieldRule rule, DbActionType action)
 {
     command.AddCommonParameters(rule.Id, action);
     command.Parameters.Add("@id_user", SqlDbType.Int).Value = rule.UserId.ToDbValue();
     command.Parameters.Add("@field_target", SqlDbType.NVarChar).Value = rule.FieldTarget.ToDbValue();
     command.Parameters.Add("@field_target_value", SqlDbType.NVarChar).Value = rule.FieldTargetValue.ToDbValue();
     command.Parameters.Add("@field", SqlDbType.NVarChar).Value = rule.Field.ToDbValue();
     command.Parameters.Add("@field_value", SqlDbType.NVarChar).Value = rule.FieldValue.ToDbValue();
     command.Parameters.Add("@priority", SqlDbType.Int).Value = rule.Priority.ToDbValue();
     command.Parameters.Add("@type_rule", SqlDbType.SmallInt).Value = (short)rule.RuleType;
 }
 private int InsertUpdate(FieldRule entity, int? userId, DbActionType action, bool intoCache)
 {
     var cache = new CacheWrapper();
     var res = RulesDataAdapter.InsertUpdate(entity, userId, action);
     if (res == 0)
     {
         //if ok - update cache
         if (intoCache)
         {
             if (action == DbActionType.Insert)
                 cache.AddToList(CacheKey, entity, userId);
             if (action == DbActionType.Update)
                 cache.UpdateList(CacheKey, entity, userId);
         }
     }
     return res;
 }
 public virtual int Update(FieldRule entity, int? userId)
 {
     return InsertUpdate(entity, userId, DbActionType.Update, true);
 }
 public virtual int Insert(FieldRule entity, int? userId, bool intoCache)
 {
     return InsertUpdate(entity, userId, DbActionType.Insert, intoCache);
 }
 public virtual int Insert(FieldRule entity, int? userId)
 {
     return Insert(entity, userId, true);
 }