前面一节中已经实现了简单的新增功能,下面来实现修改功能,代码如下1-1:
#region 将实体数据修改到数据库 public int Update<T>(T entity) { TableInfo tableInfo = DbEntityUtils.GetTableInfo(entity,DbOperateType.UPDATE); string strSql = DbEntityUtils.GetUpdateSql(tableInfo); IDbDataParameter[] parms = DbFactory.CreateDbParameters(tableInfo.Columns.Count); DbEntityUtils.SetParameters(tableInfo.Columns, parms); object val = AdoHelper.ExecuteNonQuery(transaction, CommandType.Text, strSql, parms); return Convert.ToInt32(val); } #endregion
public static string GetUpdateSql(TableInfo tableInfo) { StringBuilder sbBody = new StringBuilder(); foreach (string key in tableInfo.Columns.Keys) { object value = tableInfo.Columns[key]; sbBody.Append(key).Append("=").Append(AdoHelper.DbParmChar + key).Append(","); } sbBody.Remove(sbBody.ToString().Length - 1, 1); tableInfo.Columns.Put(tableInfo.Id.Key, tableInfo.Id.Value); string strSql = "update {0} set {1} where {2} =" + AdoHelper.DbParmChar + tableInfo.Id.Key; strSql = string.Format(strSql, tableInfo.TableName, sbBody.ToString(), tableInfo.Id.Key); return strSql; }
Technorati 标签: ORM