Using a TableAdapter DirectlyWithout Drag-and-Drop?


Okay, so I'm not a fan of drag-and-drop (D&D). While the code generation behind the scenes is far more sophisticated than ever before, it often adds a lot of overhead to the Form that I simply don't need or want. So, how can you use these TableAdapter classes without dragging it on the Form and having to deal with all of the...ah, "stuff" that gets added to the form? Once you've created a TableAdapter, it's exposed there in the Data Sources window. If you look closely at the Data Sources window, you'll see two different icons, as shown in Figure 6.40.

Figure 6.40. A populated Data Sources window with two TableAdapters.


  • The named DataSet icon (in this case, named "dsBiblio") helps identify the database table or "source" of the data. It's a direct reference to the ConnectionString and the DataAdapter used to manage the TableAdapter classes it holds.

  • The named TableAdapter icon contains the query definition and methods used to populate the underlying strongly typed DataTable. In this case, there are two TableAdapter classes defined: Authors and Titles.

The reason I'm mentioning this is that these names can help you remember the right naming conventions to instantiate and reference the objects. Running the Data Sourcegenerated strongly typed TableAdapter Fill or GetData methods is easy if you know the object class hierarchy. The TableAdapter is a member of the DataSet.TableAdapters collection. Each specifically referenced TableAdapter exposes a Fill method, a GetData method, and/or any number of custom methods. To make things a bit more difficult (but more flexible), each of these methods can be named just about anything. You might have to revisit the Data Source Designer to see how they have been designed.

So, how does this look in code? Take a look at Figure 6.41. Notice how I instantiate a new instance of the named DataSet's AuthorsTableAdapter and execute its Fill and GetData methods to return or populate either an untyped DataTable instance or a strongly typed AuthorsDataTable instance.

Figure 6.41. Addressing strongly typed TableAdapter and DataTable instances.





Hitchhiker's Guide to Visual Studio and SQL Server(c) Best Practice Architectures and Examples
Hitchhikers Guide to Visual Studio and SQL Server: Best Practice Architectures and Examples, 7th Edition (Microsoft Windows Server System Series)
ISBN: 0321243625
EAN: 2147483647
Year: 2006
Pages: 227

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net