Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>OK, I think I have this worked out, and just want to share the results on the off chance that there are people out there who are as insane as I am. </p> <p>Basically, all the magic happens using a couple of LINQ queries and reflection. For the purposes of this example, we will assume:</p> <ol> <li>There is a strongly-typed DataSet created using the DataSet Designer in Visual Studio 2008, called <strong>dsTest</strong>. A module-level variable holds an instance of this DataSet and is called (appropriately enough), <strong>m_DataSet</strong>.</li> <li>The tables themselves all follow a standard SQL Server naming convention, starting with "tbl".</li> <li>As a part of this wizard, a series of table adapters were created for each table inside a namespace called <strong>dsTestTableAdapters</strong>. </li> <li>Each adapter is named according to the table (so if we have "tblThingy", then an adapter named "tblThingyTableAdapter" would be created).</li> <li>The application is in a namespace called, for lack of anything better, <strong>MyNamespace</strong>.</li> </ol> <p>Here's the routine, called on Form Load:</p> <pre><code>Private Sub PopulateDataSet() ' Get our table adapters Dim adapters As List(Of Type) = (From t As Type In System.Reflection.Assembly.GetExecutingAssembly.GetTypes Where t.Namespace = "MyNameSpace.dsTestTableAdapters" And t.Name.StartsWith("tbl") Select t).ToList ' Initialize our dataset m_DataSet = New dsUtility ' Get our table names Dim tableNames as List(Of String) = (From dtbl As DataTable In m_DataSet.Tables Select dtbl.TableName).ToList ' Loop through each table name and fill the table with the corresponding adapter For Each iter As String In tableNames ' Grab the corresponding adapter name Dim tableName As String = iter ' Grab a copy of the table name to avoid LINQ issues with iteration variables Dim adapterType As Type = (From t As Type In adapters Where t.Name.StartsWith(tableName) Select t).First ' Given the adapter type name, use Reflection to create an instance Dim adapter As Object = Activator.CreateInstance(adapterType) ' Use the instance to fill the appropriate table adapter.Fill(m_DataSet.Tables(tableName)) Next End Sub </code></pre> <p>I tried that, and it worked like a charm. Thanks, everyone, for your help and I hope you find this useful! </p>
 

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