{"id":7413,"date":"2004-07-29T11:06:00","date_gmt":"2004-07-29T11:06:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vbteam\/2004\/07\/29\/updating-a-joined-query\/"},"modified":"2024-07-05T15:03:32","modified_gmt":"2024-07-05T22:03:32","slug":"updating-a-joined-query","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/vbteam\/updating-a-joined-query\/","title":{"rendered":"Updating a Joined Query"},"content":{"rendered":"<p class=\"MsoNormal\"><span>A statement we hear a lot is:<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><i><span>&ldquo;The Typed DataSet is great, but the DataAdapters don&rsquo;t support joined queries.<span>&nbsp; <\/span>I need a relational model in the database, but I need my application model to flatten things out:&rdquo;<\/p>\n<p><\/span><\/i><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>Using the <b><i>Data Component Wizard<\/i><\/b>, which is essentially the 2003 DataAdapter Wizard, developers can create an updatable joined select statement.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>Using Visual Studio 2005 Beta 1 you can use the following steps:<\/p>\n<p><\/span><\/p>\n<ol type=\"1\">\n<li class=\"MsoNormal\"><span>Create a Windows Forms Project\n<\/p>\n<p><\/span><\/li>\n<li class=\"MsoNormal\"><span>Open the Data Sources Window\n<\/p>\n<p><\/span><\/li>\n<li class=\"MsoNormal\"><span>Select Add New DataSource\n<\/p>\n<p><\/span><\/li>\n<li class=\"MsoNormal\"><span>Select Database as the Data Source Type\n<\/p>\n<p><\/span><\/li>\n<li class=\"MsoNormal\"><span>Choose a connection to your Database.<span>&nbsp; <\/span>For this example, we&rsquo;ll use Northwind.<span>&nbsp; <\/span><br \/><i>(Note there are some bugs in beta 1 regarding SQL Authenticated connections.<span>&nbsp; <\/span>To avoid these bugs, use Windows Integrated Security.<span>&nbsp; <\/span>This has been fixed and will be available in the next community technology preview)\n<\/p>\n<p><\/i><\/span><\/li>\n<li class=\"MsoNormal\"><span>Click next on the Save Connection String step and click [Next]\n<\/p>\n<p><\/span><\/li>\n<li class=\"MsoNormal\"><span>In the Choose your Database Object step select the table you wish to update, we&rsquo;ll use <b>Order Details<\/b>, and click finish.<span>&nbsp; <\/span><br \/>Don&rsquo;t worry about the joined table just yet.<span>&nbsp; <\/span>We first want to generate the Insert, Update and Delete Commands which we can only do if we have just 1 command.<span>&nbsp; <\/span><br \/>(We hope to make this better, see end of this blog)\n<\/p>\n<p><\/span><\/li>\n<li class=\"MsoNormal\"><span>In the Data Sources Window select the 2<sup>nd<\/sup> toolbar named <b>Edit Data Source in Designer<\/b> (Note: this should say edit DataSet in designer).<span>&nbsp; <\/span>\n<\/p>\n<p><\/span><\/li>\n<li class=\"MsoNormal\"><span>At this point you&rsquo;re looking at the new DataSet Designer.<span>&nbsp; <\/span>In addition to being a designer, <i>designed for DataSets<\/i>, rather then using the XSD Schema editor for DataSets, we&rsquo;ve also incorporated the DataAdapters.<span>&nbsp; <\/span>In 2005 we now generate typed DataAdapters called TableAdapters.<span>&nbsp; <\/span>I hope to write a blog on these as well.\n<\/p>\n<p><\/span><\/li>\n<li class=\"MsoNormal\"><span>Select the OrderDetailsTableAdapter.<span>&nbsp; <\/span>Be careful to select the header of the TableAdapter to see all the associated properties, including the SelectCommand, InsertCommand, UpdateCommand, DeleteCommand, &hellip;<span>&nbsp; <\/span>If you select the Fill, GetData() you&rsquo;ll only see the properties for the SelectCommand which is ok for what we&rsquo;re actually going to change, but I want to show the other commands as well.\n<\/p>\n<p><\/span><\/li>\n<li class=\"MsoNormal\"><span>With the TableAdapter selected, expand the InsertCommand and view the CommandText.<span>&nbsp; <\/span>Notice that it updates the single table you selected in the wizard.<span>&nbsp; <\/span><br \/><\/span><span>INSERT INTO [dbo].[Order Details] ([OrderID], [ProductID], [UnitPrice], [Quantity], [Discount]) VALUES (@OrderID, @ProductID, @UnitPrice, @Quantity, @Discount)<\/span><span>\n<\/p>\n<p><\/span><\/li>\n<li class=\"MsoNormal\"><span>On the TableAdapter, expand the SelectCommand and click the [&hellip;] on the CommandText to open the Query Builder.\n<\/p>\n<p><\/span><\/li>\n<li class=\"MsoNormal\"><span>Using the Query Builder select the top visual pane and using the context menu select Add Table&hellip;<span>&nbsp; <\/span>\n<\/p>\n<p><\/span><\/li>\n<li class=\"MsoNormal\"><span>Add the Products table and check the ProductName column\n<\/p>\n<p><\/span><\/li>\n<li class=\"MsoNormal\"><span>This now generates a joined query that will include the ProductName. <span>&nbsp;<\/span>This now gives you a SELECT statement like the following:<br \/><\/span><span>SELECT<span>&nbsp; <\/span>dbo.[Order Details].OrderID, dbo.[Order Details].ProductID, dbo.[Order Details].UnitPrice, dbo.[Order Details].Quantity, dbo.[Order Details].Discount, dbo.Products.ProductName<br \/>FROM<span>&nbsp;&nbsp;&nbsp; <\/span>dbo.[Order Details] INNER JOIN<br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>dbo.Products ON dbo.[Order Details].ProductID = dbo.Products.ProductID\n<\/p>\n<p><\/span><\/li>\n<li class=\"MsoNormal\"><span>Close the Query Builder by pressing [OK]\n<\/p>\n<p><\/span><\/li>\n<li class=\"MsoNormal\"><span>Although the DataTable doesn&rsquo;t resize (bug), you can use the context menu to select Autosize Selected Controls and you&rsquo;ll now see the ProductName column in the typed DataTable\n<\/p>\n<p><\/span><\/li>\n<li class=\"MsoNormal\"><span>With the TableAdapter selected, look at the Insert, Update and Delete commands. Notice how they still only operate on the base table (Order Details)\n<\/p>\n<p><\/span><\/li>\n<\/ol>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>This is the same model that works with the 2002 and 2003 DataAdapter wizards.<span>&nbsp; <\/span>While this only updates a single table it is possible to update multiple tables using the RowUpdated and RowUpating events which are exposed through the partial class.<span>&nbsp; <\/span>(But that&rsquo;s a topic for another entry&hellip;)<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><b><span>Supporting Joins directly in the wizard<\/p>\n<p><\/span><\/b><\/p>\n<p class=\"MsoNormal\"><span>We have discussed how we might support this in the initial pass through the wizard.<span>&nbsp; <\/span>As with all the great ideas we&rsquo;ve received from customers, we need to figure out when to stop so we can just ship already.<span>&nbsp; <\/span>However, should our team get ahead of schedule we are considering the following:<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>When we detect a joined query, we don&rsquo;t really know which table you would want to update.<span>&nbsp; <\/span>However, we do have enough info to ask the user which table they&rsquo;d like the Insert, Update and Delete statements created for.<span>&nbsp; <\/span>We would prompt the user with a dialog listing the tables within the query and once they choose one we would continue as we did above.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>No promises as we still have lots of bugs and we really want to make sure we finish up enabling scenarios that are otherwise unachievable.<span>&nbsp; <\/span>With the above work around, we don&rsquo;t see this as a ship killer, but it is one of my pet peeves and I hope we have time to fix it.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>Steve Lasker<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>Program Manager<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>Data Design-Time for Client apps.<\/span><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A statement we hear a lot is: &ldquo;The Typed DataSet is great, but the DataAdapters don&rsquo;t support joined queries.&nbsp; I need a relational model in the database, but I need my application model to flatten things out:&rdquo; &nbsp; Using the Data Component Wizard, which is essentially the 2003 DataAdapter Wizard, developers can create an updatable [&hellip;]<\/p>\n","protected":false},"author":260,"featured_media":8818,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[192,195],"tags":[],"class_list":["post-7413","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-featured","category-visual-basic"],"acf":[],"blog_post_summary":"<p>A statement we hear a lot is: &ldquo;The Typed DataSet is great, but the DataAdapters don&rsquo;t support joined queries.&nbsp; I need a relational model in the database, but I need my application model to flatten things out:&rdquo; &nbsp; Using the Data Component Wizard, which is essentially the 2003 DataAdapter Wizard, developers can create an updatable [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/7413","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/users\/260"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/comments?post=7413"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/7413\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media\/8818"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media?parent=7413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/categories?post=7413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/tags?post=7413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}