{"id":5763,"date":"2007-07-11T22:48:00","date_gmt":"2007-07-11T22:48:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vbteam\/2007\/07\/11\/linq-cookbook-recipe-6-your-first-linq-application-using-northwind-kit-george\/"},"modified":"2024-07-05T14:43:27","modified_gmt":"2024-07-05T21:43:27","slug":"linq-cookbook-recipe-6-your-first-linq-application-using-northwind-kit-george","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/vbteam\/linq-cookbook-recipe-6-your-first-linq-application-using-northwind-kit-george\/","title":{"rendered":"Linq Cookbook, Recipe 6: Your first Linq Application using Northwind (Kit George)"},"content":{"rendered":"<p class=\"MsoNormal\"><span>This example is intended as a &lsquo;primer&rsquo; for following recipes (so we don&rsquo;t repeat the same instructions multiple times). This recipe takes you through everything you need, and assumes only that you&#8217;ve installed&nbsp;Visual Studio (and in so doing, you&#8217;ve installed SQLExpress). Subsequent Northwind based Linq-To-Sql recipes will point to this entry.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><b><span lang=\"EN\">Ingredients:<\/p>\n<p><\/span><\/b><\/p>\n<p class=\"MsoNormal\"><span lang=\"EN\">&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span lang=\"EN\">Visual Studio 2008 (Beta2 or Higher)<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span lang=\"EN\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>We assume that SQL Express (or better)&nbsp;has been installed<\/span><\/p>\n<p class=\"MsoNormal\"><span lang=\"EN\"><\/span><span lang=\"EN\">&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span lang=\"EN\">Categories: LINQ-To-SQL, LINQ and WinForms<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span lang=\"EN\">&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><b><span lang=\"EN\">Instructions:<\/span><\/b><\/p>\n<ul>\n<li>\n<div class=\"MsoNormal\"><span lang=\"EN\">We need the northwind database, so Download the <a href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?FamilyId=06616212-0356-46A0-8DA2-EEBC53A68034&amp;displaylang=en\"><font color=\"#800080\">northwind database here<\/font><\/a>. Run the file from the download button on the page<\/span><\/div>\n<\/li>\n<li>\n<div class=\"MsoNormal\"><span lang=\"EN\"><\/span><span lang=\"EN\">Once downloaded, you&rsquo;ll find the new samples in &ldquo;C:SQL Server 2000 Sample Databases&rdquo;. We&rsquo;ll be using the northwind.mdf file<\/span><\/div>\n<\/li>\n<li>\n<div class=\"MsoNormal\"><span lang=\"EN\"><\/span><span lang=\"EN\">Open Visual Studio, and create a new Windows Forms Application<\/span><\/div>\n<\/li>\n<li>\n<div class=\"MsoNormal\"><span lang=\"EN\"><\/span><span lang=\"EN\">Go to the View menu and click the Server Explorer menu item<\/span><\/div>\n<\/li>\n<li>\n<div class=\"MsoNormal\"><span lang=\"EN\"><\/span><span lang=\"EN\">On the treeview on the left, right-click the Data Connections and select &lsquo;Add Connection&rsquo;<\/span><\/div>\n<\/li>\n<li>\n<div class=\"MsoNormal\"><span lang=\"EN\"><\/span><span lang=\"EN\">In the next dialog select &lsquo;Microsoft SQL Server Database File&rsquo; and click Continue<\/span><\/div>\n<\/li>\n<li>\n<div class=\"MsoNormal\"><span lang=\"EN\"><\/span><span lang=\"EN\">In the next dialog, click the Browse button, and navigate to &ldquo;C:SQL Server 2000 Sample Databases&rdquo;. Select the &lsquo;NORTHWND.MDF&rsquo; file, and click Open<\/span><\/div>\n<\/li>\n<li>\n<div class=\"MsoNormal\"><span lang=\"EN\"><\/span><span lang=\"EN\">Click OK to finish Adding the Connection<\/span><\/div>\n<\/li>\n<li>\n<div class=\"MsoNormal\"><span lang=\"EN\"><\/span><span lang=\"EN\">In your Treeview node, under &lsquo;Data Connections&rsquo; NORTHWIND.MDF will be available. Expand this node (click on the plus beside it) and expand the Tables node<\/span><\/div>\n<\/li>\n<ul>\n<li>\n<div class=\"MsoNormal\"><span lang=\"EN\"><\/span><b><span lang=\"EN\">Note<\/span><\/b><span lang=\"EN\">: The steps up to this point need only be completed once. If you create a new project, then the database will still be available through server explorer. You may need to reopoen the Server Explorer window from session to session (see below)<\/span><\/div>\n<\/li>\n<\/ul>\n<li>\n<div class=\"MsoNormal\"><span lang=\"EN\"><\/span><span lang=\"EN\">Now add a Linq To Sql file to your application. Go to the Project menu, and select &lsquo;Add New Item&rsquo;<\/span><\/div>\n<\/li>\n<li>\n<div class=\"MsoNormal\"><span lang=\"EN\"><\/span><span lang=\"EN\">In the Add New Item dialog, select the &lsquo;LINQ To SQL Classes&rsquo; item<\/span><\/div>\n<\/li>\n<li>\n<div class=\"MsoNormal\"><span lang=\"EN\"><\/span><span lang=\"EN\">Change the name to &lsquo;Northwind.dbml&rsquo; and click Add<\/span><\/div>\n<\/li>\n<li>\n<div class=\"MsoNormal\"><span lang=\"EN\"><\/span><span lang=\"EN\">From the Server Explorer window (which will still be open) select all of the Tables from the node we expanded (DataConnectionsNORTHWND.MDFTables) and drag them to the O\/R designer window to the right (this window will have opened after we added the Northwind.dbml file)<\/span><\/div>\n<\/li>\n<li>\n<div class=\"MsoNormal\"><span lang=\"EN\"><\/span><span lang=\"EN\">You will get a dialog asking if you wish to copy the database locally. Click Yes<\/span><\/div>\n<\/li>\n<li>\n<div class=\"MsoNormal\"><span lang=\"EN\"><\/span><span lang=\"EN\">Close the designer file, and the Server Explorer window. You&rsquo;re ready to start writing your LinQ To Sql code!<\/span><\/div>\n<\/li>\n<li>\n<div class=\"MsoNormal\"><span lang=\"EN\"><\/span><span lang=\"EN\">To reopen the Server Explorer window, go to the View menu, and select Server Explorer<\/span><\/div>\n<\/li>\n<li>\n<div class=\"MsoNormal\"><span lang=\"EN\"><\/span><span lang=\"EN\">To reopen the O\/R designer, go to the Solution Explorer window, and double-click Northwind.dbml<\/span><\/div>\n<\/li>\n<\/ul>\n<p class=\"MsoNormal\"><span lang=\"EN\">&nbsp;<\/span><b><span lang=\"EN\">Test our project<\/span><\/b><\/p>\n<ul>\n<li>\n<div class=\"MsoNormal\"><b><span lang=\"EN\"><\/span><\/b><span lang=\"EN\">To ensure our project is ready to add linq statements to, let&rsquo;s try it out!<\/span><\/div>\n<\/li>\n<li>\n<div class=\"MsoNormal\"><span lang=\"EN\"><\/span><span lang=\"EN\">Add a button to form1<\/span><\/div>\n<\/li>\n<li>\n<div class=\"MsoNormal\"><span lang=\"EN\"><\/span><span lang=\"EN\">Add a DataGridView to form1<\/span><\/div>\n<\/li>\n<li>\n<div class=\"MsoNormal\"><span lang=\"EN\"><\/span><span lang=\"EN\">Double-Click the button to open its click event<\/span><\/div>\n<\/li>\n<li>\n<div class=\"MsoNormal\"><span lang=\"EN\"><\/span><span lang=\"EN\">Add this code inside the click event:<\/span><\/div>\n<\/li>\n<\/ul>\n<p class=\"MsoNormal\"><span>Dim<\/span><span> db <span>As<\/span> <span>New<\/span> NorthwindDataContext<\/span><\/p>\n<p class=\"MsoNormal\"><span><\/span><span>DataGridView1.DataSource = _<\/span><\/p>\n<p class=\"MsoNormal\"><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span><span>From<\/span> cust <span>In<\/span> db.Customers _<\/span><\/p>\n<p class=\"MsoNormal\"><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span><span>Where<\/span> cust.City = <span>&#8220;London&#8221;<\/span> _<\/span><\/p>\n<p class=\"MsoNormal\"><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span><span>Order<\/span> <span>By<\/span> cust.CompanyName _<\/span><\/p>\n<p class=\"MsoNormal\"><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Select<\/span><span> cust.CompanyName, _<\/span><\/p>\n<p class=\"MsoNormal\"><span>&nbsp;&nbsp;&nbsp;&nbsp; cu<\/span><span>st.ContactName, cust.Fax<\/span><\/p>\n<ul>\n<li class=\"MsoNormal\"><span><\/span><span><span lang=\"EN\"><span lang=\"EN\">Run the App (F5) and click the button to test it\n<\/p>\n<p><\/span><\/span><\/span><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This example is intended as a &lsquo;primer&rsquo; for following recipes (so we don&rsquo;t repeat the same instructions multiple times). This recipe takes you through everything you need, and assumes only that you&#8217;ve installed&nbsp;Visual Studio (and in so doing, you&#8217;ve installed SQLExpress). Subsequent Northwind based Linq-To-Sql recipes will point to this entry. Ingredients: &#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Visual Studio [&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":[90,93,94,166],"class_list":["post-5763","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-featured","category-visual-basic","tag-kit-george","tag-linq-cookbook","tag-linqvb9","tag-vb2008"],"acf":[],"blog_post_summary":"<p>This example is intended as a &lsquo;primer&rsquo; for following recipes (so we don&rsquo;t repeat the same instructions multiple times). This recipe takes you through everything you need, and assumes only that you&#8217;ve installed&nbsp;Visual Studio (and in so doing, you&#8217;ve installed SQLExpress). Subsequent Northwind based Linq-To-Sql recipes will point to this entry. Ingredients: &#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Visual Studio [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/5763","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=5763"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/5763\/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=5763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/categories?post=5763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/tags?post=5763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}