protected int DeleteRows(string table_name) { DataTable myTable = this.Tables[table_name]; DataRowCollection rows = myTable.Rows; int rowsCount = rows.Count; int rowsAffected = 0; if (rowsCount == 0) { return(0); } DeleteStatement cmd = new DeleteStatement(myTable.ExtendedProperties["Storage"].ToString()); cmd.ConnectionStringName = _connectionStringName; List <SqlParameter> prmlist = new List <SqlParameter>(); int colsCount = myTable.Columns.Count; int keys = 0; string identcol = String.Empty; for (int c = 0; c < colsCount; c++) { DataColumn col = myTable.Columns[c]; if (col.ExtendedProperties["PrimaryKey"] != null) { SqlParameter prm = new SqlParameter(col.ColumnName, col.DataType); prmlist.Add(prm); cmd.Where(Condition.IsEqual(col.ExtendedProperties["Storage"].ToString(), prm)); keys++; } } List <DataRow> toDelete = new List <DataRow>(); foreach (DataRow row in myTable.Rows) { if (row.RowState == DataRowState.Deleted) { for (int p = 0; p < keys; p++) { SqlParameter prm = prmlist[p]; prm.Value = row[prm.Name, DataRowVersion.Original]; } cmd.Execute(); toDelete.Add(row); rowsAffected++; } } toDelete.ForEach(delegate(DataRow row) { row.AcceptChanges(); }); return(rowsAffected); }
public virtual void Delete(ICondition filter) { if (filter == null) { return; } var delete = new DeleteStatement(_tableName) { ConnectionStringName = _connectionStringName }; delete.Where(filter); delete.Execute(); }
public virtual void Update(TEntity entity) { ICondition criteria = IdentityInsert ? Conditions.IsEqual(Entity.ID, new SqlNumber(entity.Id.ToString())) : Conditions.IsEqual(Entity.ID, new SqlString(entity.Id.ToString())); var current = GetOne(criteria); if (current == null) { return; } bool hasChanges = false; foreach (var item in _metadata.Columns) { if (current[item.Name] != entity[item.Name]) { hasChanges = true; break; } } if (hasChanges) { var update = SqlUtils.CreateUpdateStatement(_metadata); update.ConnectionStringName = _connectionStringName; foreach (var item in _metadata.Columns) { update.SetParamValue(item.Name, entity[item.Name]); } update.Execute(); } if (_hasManyAssociations.Count() == 0) { return; } foreach (IAssociationMapping hma in _hasManyAssociations) { var items = entity[hma.Name] as IEnumerable <ValueObject>; if (items == null) { continue; } var storedItems = current[hma.Name] as IEnumerable <ValueObject>; // if (storedItems == null) // continue; var omapping = MappingFactory.GetMapping(hma.OtherMappingType); //var primKeys = omapping.Columns.Where(o => o.IsPrimaryKey || o.IsDbGenerated).ToList(); DeleteStatement deleteStament = SqlUtils.CreateDeleteStatement(omapping, true); deleteStament.ConnectionStringName = _connectionStringName; InsertStatement insertStament = SqlUtils.CreateInsertStatement(omapping); insertStament.ConnectionStringName = _connectionStringName; if (storedItems != null) { // Delete non existent foreach (ValueObject storedItem in storedItems) { var itemFound = items.FirstOrDefault(ci => ci.Equals(storedItem)); if (itemFound == null) { foreach (ColumnMapping cm in omapping.Columns) { deleteStament.SetParamValue(cm.Name, storedItem[cm.Name]); } deleteStament.Execute(); } } // Insert non existent foreach (ValueObject item in items) { var itemFound = storedItems.FirstOrDefault(si => si.Equals(item)); if (itemFound == null) { foreach (ColumnMapping cm in omapping.Columns) { insertStament.SetParamValue(cm.Name, item[cm.Name]); } insertStament.Execute(); } } } else { foreach (ValueObject item in items) { foreach (ColumnMapping cm in omapping.Columns) { insertStament.SetParamValue(cm.Name, item[cm.Name]); } insertStament.Execute(); } } } }