private int DoLoadReferences(DbConnection conn, DataSetReference reference) { if (reference.ReferencedClass.AllInstances.Count == 0) return 0; var sb = new StringBuilder(); sb.AppendFormat("select * from [{0}] where [{1}] in (", reference.BaseClass.TableName, reference.BindingColumn); bool was = false; foreach (var inst in reference.ReferencedClass.AllInstances) { if (was) sb.Append(","); sb.Append("'"); sb.Append(inst.SimpleKeyValue); sb.Append("'"); was = true; } sb.Append(")"); //if (reference.BaseClass.AllInstaces.Count > 0) //{ // was = false; // sb.AppendFormat(" and [{0}] not in (", reference.BaseClass.IdentityColumn); // foreach (var inst in reference.BaseClass.AllInstaces) // { // if (was) sb.Append(","); // sb.Append(inst.IdentityValue); // was = true; // } // sb.Append(")"); //} List<DataSetInstance> loaded; using (DbCommand cmd = conn.CreateCommand()) { cmd.CommandText = sb.ToString(); cmd.CommandTimeout = 3600; using (ICdlReader reader = _dda.AdaptReader(cmd.ExecuteReader())) { loaded = DoLoadRows(reader, reference.BaseClass.TableName); } } return loaded.Count; }
public void InitializeClass() { foreach (var fk in _targetTable.ForeignKeys) { if (fk.Columns.Count > 1) continue; if (_targetTable != null && fk.RefTableFullName == _targetTable.FullName) continue; var target = _model.GetClass(fk.RefTableFullName); var r = new DataSetReference { BaseClass = this, ReferencedClass = target, BindingColumn = fk.Columns[0].Name, Mandatory = fk.Columns[0].RefColumn.NotNull, }; References.Add(r); } var autoInc = _targetTable.FindAutoIncrementColumn(); if (autoInc != null) { IdentityColumn = autoInc.Name; IdentityColumnOrdinal = _targetTable.Columns.IndexOf(autoInc); } Columns = _targetTable.Columns.Select(c => c.Name).ToArray(); for (int i = 0; i < _targetTable.ColumnCount; i++) { ColumnOrdinals[_targetTable.Columns[i].Name] = i; } if (_targetTable.PrimaryKey != null && _targetTable.PrimaryKey.Columns.Count > 1) { ComplexPkCols = _targetTable.PrimaryKey.Columns.Select(c => c.Name).ToArray(); } if (ComplexPkCols != null && Columns != null) { var idxs = new List<int>(); foreach (string col in ComplexPkCols) { idxs.Add(Array.IndexOf(Columns, col)); } ComplexPkColIndexes = idxs.ToArray(); } else { ComplexPkColIndexes = null; } if (_targetTable.PrimaryKey != null && _targetTable.PrimaryKey.Columns.Count == 1) { SimplePkCol = _targetTable.PrimaryKey.Columns[0].Name; SimplePkColIndex = Array.IndexOf(Columns, SimplePkCol); } }