Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've been using this. It would need some optimizations though. This also takes care of updating the tables in correct order depending on the relations in dataset (in case there are no self-references, which can be handled by sorting the rows, but for simplicity I'm not posting it here).</p> <pre><code> public static void Save(DataSet data, SqlConnection connection) { /// Dictionary for associating adapters to tables. Dictionary&lt;DataTable, SqlDataAdapter&gt; adapters = new Dictionary&lt;DataTable, SqlDataAdapter&gt;(); foreach (DataTable table in data.Tables) { /// Find the table adapter using Reflection. Type adapterType = GetTableAdapterType(table); SqlDataAdapter adapter = SetupTableAdapter(adapterType, connection, validityEnd); adapters.Add(table, adapter); } /// Save the data. Save(data, adapters); } static Type GetTableAdapterType(DataTable table) { /// Find the adapter type for the table using the namespace conventions generated by dataset code generator. string nameSpace = table.GetType().Namespace; string adapterTypeName = nameSpace + "." + table.DataSet.DataSetName + "TableAdapters." + table.TableName + "TableAdapter"; Type adapterType = Type.GetType(adapterTypeName); return adapterType; } static SqlDataAdapter SetupTableAdapter(Type adapterType, SqlConnection connection) { /// Set connection to TableAdapter and extract SqlDataAdapter (which is private anyway). object adapterObj = Activator.CreateInstance(adapterType); SqlDataAdapter sqlAdapter = (SqlDataAdapter)GetPropertyValue(adapterType, adapterObj, "Adapter"); SetPropertyValue(adapterType, adapterObj, "Connection", connection); return sqlAdapter; } static object GetPropertyValue(Type type, object instance, string propertyName) { return type.GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.GetProperty | BindingFlags.Instance).GetValue(instance, null); } static void SetPropertyValue(Type type, object instance, string propertyName, object propertyValue) { type.GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance).SetValue(instance, propertyValue, null); } static void Save(DataSet data, Dictionary&lt;DataTable, SqlDataAdapter&gt; adapters) { if (data == null) throw new ArgumentNullException("data"); if (adapters == null) throw new ArgumentNullException("adapters"); Dictionary&lt;DataTable, bool&gt; procesedTables = new Dictionary&lt;DataTable, bool&gt;(); List&lt;DataTable&gt; sortedTables = new List&lt;DataTable&gt;(); while (true) { DataTable rootTable = GetRootTable(data, procesedTables); if (rootTable == null) break; sortedTables.Add(rootTable); } /// Updating Deleted rows in Child -&gt; Parent order. for (int i = sortedTables.Count - 1; i &gt;= 0; i--) { Update(adapters, sortedTables[i], DataViewRowState.Deleted); } /// Updating Added / Modified rows in Parent -&gt; Child order. for (int i = 0; i &lt; sortedTables.Count; i++) { Update(adapters, sortedTables[i], DataViewRowState.Added | DataViewRowState.ModifiedCurrent); } } static void Update(Dictionary&lt;DataTable, SqlDataAdapter&gt; adapters, DataTable table, DataViewRowState states) { SqlDataAdapter adapter = null; if (adapters.ContainsKey(table)) adapter = adapters[table]; if (adapter != null) { DataRow[] rowsToUpdate = table.Select("", "", states); if (rowsToUpdate.Length &gt; 0) adapter.Update(rowsToUpdate); } } static DataTable GetRootTable(DataSet data, Dictionary&lt;DataTable, bool&gt; procesedTables) { foreach (DataTable table in data.Tables) { if (!procesedTables.ContainsKey(table)) { if (IsRootTable(table, procesedTables)) { procesedTables.Add(table, false); return table; } } } return null; } static bool IsRootTable(DataTable table, Dictionary&lt;DataTable, bool&gt; procesedTables) { foreach (DataRelation relation in table.ParentRelations) { DataTable parentTable = relation.ParentTable; if (parentTable != table &amp;&amp; !procesedTables.ContainsKey(parentTable)) return false; } return true; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload