public static UpdateStatement CreateUpdateStatement(IMapping mapping) { UpdateStatement statement = new UpdateStatement(mapping.TableName); foreach (var item in mapping.Columns) { var pinfo = (PropertyInfo)item.Property; SqlParameter prm; if (!item.IsDbGenerated && !item.IsPrimaryKey) { if (pinfo.PropertyType.IsGenericType && pinfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { prm = statement.CreateParameter(item.Name, pinfo.PropertyType.GetGenericArguments()[0]); } else { prm = statement.CreateParameter(item.Name, pinfo.PropertyType); } statement.Column(item.ColumnName, prm); } else { prm = statement.CreateParameter(item.Name, pinfo.PropertyType); statement.Where(new Condition(item.ColumnName, Comparisons.Equals, prm)); } } return(statement); }
protected int UpdateRows(string table_name) { DataTable myTable = this.Tables[table_name]; DataRowCollection rows = myTable.Rows; int rowsCount = rows.Count; int rowsAffected = 0; if (rowsCount > 0) { var cmd = new UpdateStatement(myTable.ExtendedProperties["Storage"].ToString()); cmd.ConnectionStringName = _connectionStringName; var prmlist = new List <SqlParameter>(); int colsCount = myTable.Columns.Count; string identcol = String.Empty; for (int c = 0; c < colsCount; c++) { DataColumn col = myTable.Columns[c]; SqlParameter prm = new SqlParameter(col.ColumnName, col.DataType); prmlist.Add(prm); if (col.ExtendedProperties["PrimaryKey"] != null) { cmd.Where(Condition.IsEqual(col.ExtendedProperties["Storage"].ToString(), prm)); } else if (col.ExtendedProperties["NonStorable"] == null) { cmd.Column(col.ExtendedProperties["Storage"].ToString(), prm); } } IEnumerator rowEnum = rows.GetEnumerator(); while (rowEnum.MoveNext()) { DataRow row = (DataRow)rowEnum.Current; if (row.RowState == DataRowState.Modified) { for (int c = 0; c < colsCount; c++) { SqlParameter prm = prmlist[c]; prm.Value = row[prm.Name]; } cmd.Execute(); row.AcceptChanges(); rowsAffected++; } } } return(rowsAffected); }
public virtual void Remove(ICondition filter) { if (filter == null) { return; } var update = new UpdateStatement(_tableName) { ConnectionStringName = _connectionStringName }; var item = _metadata.Columns.First(x => x.Name == Entity.IS_REMOVED); if (item != null) { update.Column(item.ColumnName, SqlBoolean.TRUE); update.Where(filter); update.Execute(); } }