/// <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> /// Imports environment data from the specified dataset. Prior to call, you should /// first call <see cref="RemoveAll"/>. /// </summary> /// <param name="server">The server holding the data</param> /// <param name="ds">The dataset to import</param> public void Import(IDataServer server, BacksightDataSet ds) { using (IConnection ic = server.GetConnection()) { SqlConnection c = ic.Value; foreach (DataTable dt in ds.Tables) { if (dt.Rows.Count > 0) { SqlBulkCopy bc = new SqlBulkCopy(c); string tableName = "[ced].[" + GetTableName(dt) + "]"; bc.DestinationTableName = tableName; bc.BatchSize = dt.Rows.Count; bc.WriteToServer(dt); } } } }