日期:2014-05-20 浏览次数:20776 次
public class NewAccessCastle { /// <summary> /// 旧的更新语句 /// </summary> public void Update(Castle model) { SqlParameter[] parameters = GetUpdateParamter(model); DbHelperSQL.ExecuteSql(updateSQL, parameters); } const string updateSQL = @" UPDATE Castle SET [PlayerID]=@PlayerID,[CastleID]=@CastleID,[Resource]=@Resource,[Building]=@Building WHERE [ID]=@ID"; private static SqlParameter[] GetUpdateParamter(Castle model) { SqlParameter[] parameters = { new SqlParameter("@ID", SqlDbType.Int), new SqlParameter("@PlayerID", SqlDbType.Int), new SqlParameter("@CastleID", SqlDbType.Int), new SqlParameter("@Resource", SqlDbType.NVarChar), new SqlParameter("@Building", SqlDbType.NVarChar)}; parameters[0].Value = model.ID; parameters[1].Value = model.PlayerID; parameters[2].Value = model.CastleID; parameters[3].Value = model.Resource; parameters[4].Value = model.Building; return parameters; } /// <summary> /// 通过新旧参数判断来更新对应数据库字段 /// </summary> /// <param name="model"></param> public void Update1(Castle model) { List<SqlParameter> parameters = new List<SqlParameter>(); StringBuilder sql = new StringBuilder(); sql.Append("UPDATE Castle Set "); if (model.PlayerID != model.source_PlayerID) { sql.Append("[PlayerID]=@PlayerID,"); parameters.Add(new SqlParameter( "@PlayerID", model.PlayerID)); } if (model.CastleID != model.source_CastleID) { sql.Append("[CastleID]=@CastleID,"); parameters.Add(new SqlParameter("@CastleID", model.CastleID)); } if (model.Building != model.source_Building) { sql.Append("[Building]=@Building,"); parameters.Add(new SqlParameter("@Building", model.Building)); } if (model.Resource != model.source_Resource) { sql.Append("[Resource]=@Resource,"); parameters.Add(new SqlParameter("@Resource", model.Resource)); } if (parameters.Count > 0) { sql.Remove(sql.Length - 1, 1); sql.Append(" where [ID]=@ID"); parameters.Add(new SqlParameter("@ID", model.ID)); DbHelperSQL.ExecuteSql(sql.ToString(), parameters.ToArray()); } } public enum emUpdateParameter { PlayerID, CastleID, Building, Resource, } /// <summary> /// 通过更新接口参数来确定具体的更新数据 /// </summary> /// <param name="model"></param> /// <param name="changeParameter"></param> public void Update2(Castle model, params emUpdateParameter[] changeParameter) { if (changeParameter == null || changeParameter.Length == 0) { Update(model); return; } List<SqlParameter> parameters = new List<SqlParameter>(); StringBuilder sql = new StringBuilder(); sql.Append("UPDATE Castle Set "); foreach (var change in changeParameter) { switch (change) { case emUpdateParameter.PlayerID: sql.Append("[PlayerID]=@PlayerID,"); parameters.Add(new SqlParameter("@PlayerID", model.PlayerID)); break; case emUpdateParameter.CastleID: sql.Append("[CastleID]=@CastleID,"); parameters.Add(new SqlParameter("@CastleID", model.CastleID)); break; case emUpdateParameter.Building: sql.Append("[Building]=@Building,"); parameters.Add(new SqlParameter("@Building", model.Building)); break; case emUpdateParameter.Resource: sql.Append("[Resource]=@Resource,"); parameters.Add(new SqlParameter("@Resource", model.Resource)); break; default: break; } } sql.Remove(sql.Length - 1, 1); sql.Append(" where [ID]=@ID"); parameters.Add(new SqlParameter("@ID", model.ID)); DbHelperSQL.ExecuteSql(sql.ToString(), parameters.ToArray()); } }