/// <summary> /// Removes all data from all Backsight tables. This is done when a new environment /// is being imported. You will probably need to bracket this call with calls /// to <see cref="EnableForeignKeys"/> (disable foreign key constraints, then /// remove, then import, then re-enable constraints). /// </summary> /// <param name="db">The data server containing the tables</param> public void RemoveAll(IDataServer db) { BacksightDataSet ds = new BacksightDataSet(); foreach (DataTable dt in ds.Tables) { // Assume the "ced" schema, since I don't see any DataTable.SchemaName property. string tableName = GetTableName(dt); string sql = String.Format("DELETE FROM [ced].[{0}]", tableName); db.ExecuteNonQuery(sql); } /* * using (IConnection ic = ConnectionFactory.GetConnection()) * { * SqlConnection c = ic.Value; * * foreach (DataTable dt in ds.Tables) * { * // Assume the "ced" schema, since I don't see any DataTable.SchemaName property. * string tableName = GetTableName(dt); * string sql = String.Format("DELETE FROM [ced].[{0}]", tableName); * SqlCommand cmd = new SqlCommand(sql, c); * cmd.ExecuteNonQuery(); * } * } */ }
/// <summary> /// Enable or disable foreign key constraints for Backsight system tables. /// </summary> /// <param name="ds">The database containing the tables.</param> /// <param name="enable">Specify <c>false</c> to enable constraints, <c>true</c> /// to re-enable.</param> public void EnableForeignKeys(IDataServer ds, bool enable) { // Do it the heavy-handed way, since I'm still getting foreign key errors // after setting ForeignKey.IsEnabled to false. /* * if (enable) * AddForeignKeyConstraints(null); * else * DropForeignKeyConstraints(); */ /* * BacksightDataSet ds = new BacksightDataSet(); * * foreach (DataTable dt in ds.Tables) * { * Smo.Table t = m_Database.Tables[dt.TableName]; * if (t!=null) * { * foreach (Smo.ForeignKey fk in t.ForeignKeys) * fk.IsEnabled = enable; * } * } */ string[] tables = GetCedTableNames(); string checkClause = (enable ? "CHECK" : "NOCHECK"); foreach (string tableName in tables) { string sql = String.Format("ALTER TABLE [ced].[{0}] {1} CONSTRAINT ALL", tableName, checkClause); ds.ExecuteNonQuery(sql); } /* * using (IConnection ic = ConnectionFactory.GetConnection()) * { * SqlConnection c = ic.Value; * * foreach (string tableName in tables) * { * string sql = String.Format("ALTER TABLE [ced].[{0}] {1} CONSTRAINT ALL", tableName, checkClause); * SqlCommand cmd = new SqlCommand(sql, c); * cmd.ExecuteNonQuery(); * } * } */ }