<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>LINQ To SQL</title>
	<atom:link href="http://www.linqtosql.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.linqtosql.net</link>
	<description>Blogging About LINQ</description>
	<lastBuildDate>Thu, 24 Sep 2009 17:37:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>XSD ( XSD tool / XSD.exe ) To generate .Net Class file</title>
		<link>http://www.linqtosql.net/xsd-xsd-tool-xsd-exe-to-generate-net-class-file/</link>
		<comments>http://www.linqtosql.net/xsd-xsd-tool-xsd-exe-to-generate-net-class-file/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 16:08:37 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[generate dot net class file]]></category>
		<category><![CDATA[xml schema definition]]></category>
		<category><![CDATA[xsd]]></category>
		<category><![CDATA[xsd.exe]]></category>

		<guid isPermaLink="false">http://www.linqtosql.net/?p=38</guid>
		<description><![CDATA[1) We can use XSD Tool ( Xml Schema Definition Tool ) or XSD.exe to generate class files (.cs or .vb ) as per your requirments from Typed Datasets. XSD.exe is the XML Schema Definition tool which generates XML schema or common language runtime classes from XDR, XML, and XSD files.

The file locates at C:\Program [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-size: 10pt; font-family: Verdana;">1) We can use XSD Tool ( Xml Schema Definition Tool ) or XSD.exe to generate class files (.cs or .vb ) as per your requirments from Typed Datasets. XSD.exe is the <strong>XML Schema Definition</strong> tool which generates XML schema or common language runtime classes from XDR, XML, and XSD files.<br />
</span></p>
<p><span style="font-size: 10pt; font-family: Verdana;">The file locates at <strong>C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\<span style="color: #ff0000;">XSD.exe</span><br />
</strong>OR<br />
C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin Depends up on your version of .Net.<br />
</span></p>
<p><span style="font-size: 10pt; font-family: Verdana;">If you are having Express edition of ( V 2.0) then it might possible that you don’t have .Net command prompt option available in Start-&gt;Programs , </span></p>
<p><span style="font-size: 10pt; font-family: Verdana;">then you have to go <strong>command prompt</strong> and goto location<br />
</span></p>
<p><span style="font-size: 10pt; font-family: Verdana;">“C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin”<br />
</span></p>
<p><span style="font-size: 10pt; font-family: Verdana;">and then type following with location of .xsd file on prompt<br />
</span></p>
<p><span style="font-size: 10pt; font-family: Verdana;">xsd.exe -c -l:c# C:\MyProjects\Employee\EmpTableDataSet.xsd</span></p>
<p>2 ) This tool can be also be used to <strong>generate xml schema file (.xsd) from an xml data file (.xml)</strong>. Suppose we have file Catelog.xml as shown ,then we can generate XSD files as below from Command prompt.<br />
C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin&gt;xsd.exe C:\CatelogOne.Xml</p>
<p>C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin&gt;xsd.exe C:\CatelogOne.Xml Microsoft (R) Xml Schemas/DataTypes support utility<br />
[Microsoft (R) .NET Framework, Version 2.0.50727.42]<br />
Copyright (C) Microsoft Corporation. All rights reserved.<br />
Writing file ‘C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\CatelogOne.xsd’.</p>
<p>3 )To <strong>generate schema from a compiled class library ( .dll)<br />
</strong>C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin&gt;xsd C:\BLLTable.dll<br />
on enter it will generate the schema of the various types present in the dll.<br />
( Consider Class library you created in project or any other )</p>
<p>C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin&gt;xsd C:\BLLTable.dll<br />
Microsoft (R) Xml Schemas/DataTypes support utility<br />
[Microsoft (R) .NET Framework, Version 2.0.50727.42]<br />
Copyright (C) Microsoft Corporation. All rights reserved.<br />
Writing file ‘C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\schema0.xsd’.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.linqtosql.net/xsd-xsd-tool-xsd-exe-to-generate-net-class-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Resyncing the LINQ to SQL class model</title>
		<link>http://www.linqtosql.net/resyncing-the-linq-to-sql-class-model/</link>
		<comments>http://www.linqtosql.net/resyncing-the-linq-to-sql-class-model/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 12:23:40 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.linqtosql.net/?p=35</guid>
		<description><![CDATA[Problem:

When you change the structure of your sql tables &#8212; say, change a type from a varchar to&#8230;  say an int &#8212; the linq to sql class model (eg. project.dbml) is not updated or sync&#8217;d.
Is there a way to sync the model with the db?
Solution:
1) One way is to delete the &#8216;table&#8217; in the class [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Problem:<br />
</strong></p>
<p>When you change the structure of your sql tables &#8212; say, change a type from a varchar to&#8230;  say an int &#8212; the <strong>linq to sql class model</strong> (eg. project.dbml) <strong>is not updated or sync&#8217;d</strong>.</p>
<p>Is there a way to sync the model with the db?</p>
<p><strong>Solution:</strong></p>
<p>1) One way is to delete the &#8216;table&#8217; in the class model, then drag-n-drop / re-add it back.</p>
<p align="left">2) Another work around:</p>
<p>1. Make the changes you want in the dbml file, and save it, if the designer did not regenerate the code:<br />
2. Rename the dbml file (for example Northwind.dbml to Northwind2.dbml)<br />
3. Make a copy of it (Drag it to the same folder)<br />
4. Now rename the New file to the original name.<br />
5. while renaming I found that the designer has regenerated the code file</p>
]]></content:encoded>
			<wfw:commentRss>http://www.linqtosql.net/resyncing-the-linq-to-sql-class-model/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Code Generation Tool (SqlMetal.exe)</title>
		<link>http://www.linqtosql.net/code-generation-tool-sqlmetal-exe/</link>
		<comments>http://www.linqtosql.net/code-generation-tool-sqlmetal-exe/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 11:58:17 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[build linq to sql classes]]></category>
		<category><![CDATA[SqlMetal]]></category>
		<category><![CDATA[SqlMetal batch file]]></category>
		<category><![CDATA[sqlmetal dbml]]></category>
		<category><![CDATA[sqlmetal switches]]></category>
		<category><![CDATA[SqlMetal.exe]]></category>

		<guid isPermaLink="false">http://www.linqtosql.net/?p=31</guid>
		<description><![CDATA[The SqlMetal command-line tool generates code and mapping for the LINQ to SQL component of the .NET Framework. By applying options that appear later in this topic, you can instruct SqlMetal to perform several different actions that include the following:
*  From a database, generate source code and mapping attributes or a mapping file.
*  [...]]]></description>
			<content:encoded><![CDATA[<p>The SqlMetal command-line tool <b>generates code and mapping for the LINQ to SQL component</b> of the .NET Framework. By applying options that appear later in this topic, you can instruct SqlMetal to perform several different actions that include the following:</p>
<p>*  From a database, generate source code and mapping attributes or a mapping file.<br />
*  From a database, generate an intermediate database markup language (.dbml) file for customization.<br />
*  From a .dbml file, generate code and mapping attributes or a mapping file.</p>
<p>The SQLMetal file is included in the Windows SDK that is installed with Visual Studio. By default, the file is located at drive:\Program Files\Microsoft SDKs\Windows\vn.nn\bin. If you do not install Visual Studio, you can also get the SQLMetal file by downloading the <a title="Windows SDK" href="http://msdn.microsoft.com/en-us/windows/bb980924.aspx" mce_href="http://msdn.microsoft.com/en-us/windows/bb980924.aspx" target="_blank">Windows SDK</a>.</p>
<p align="left"><span style="color: rgb(255, 0, 0);" mce_style="color: #ff0000;"><b>SQLMetal is an all or nothing process</b></span>.&nbsp; It includes everything within the database and there is no way to tailor the selection of tables/viewes required. If you do not want select all of the database tables, stored procs etc you will need to use the designer.</p>
<p>SQLMetal is fine for generating the model classes. I am not very fond of the LINQ designer, so typically, I will use the designer in the initial design phase, and later additions or alterations to the datamodel is then done by hand editing the dbml file.</p>
<p><span style="color: rgb(255, 0, 0);" mce_style="color: #ff0000;"><b>I use this simple command for building my LINQ-to-SQL classes, which I put in a batch file for maximum convenience:<br />
</b></span><br />
<span style="color: rgb(0, 0, 255);" mce_style="color: #0000ff;">sqlmetal /code:YourCodeFile.designer.cs /namespace:Desired.Namespace YourModel.dbml<br />
</span></p>
<p>The SQLMetal approach works fine for me, and is a reliable way of generating the code. Any handwritten additions to the generated classes, should of course be kept in partial class declarations in a separate code file.</p>
<p><b>Examples:<br />
</b></p>
<p>Generate a .dbml file that includes extracted SQL metadata:</p>
<p><span style="color: rgb(0, 0, 255);" mce_style="color: #0000ff;">sqlmetal /server:myserver /database:northwind /dbml:mymeta.dbml<br />
</span><br />
Generate a .dbml file that includes extracted SQL metadata from an .mdf file by using SQL Server Express:</p>
<p><span style="color: rgb(0, 0, 255);" mce_style="color: #0000ff;">sqlmetal /dbml:mymeta.dbml mydbfile.mdf<br />
</span><br />
Generate a .dbml file that includes extracted SQL metadata from SQL Server Express:</p>
<p><span style="color: rgb(0, 0, 255);" mce_style="color: #0000ff;">sqlmetal /server:.\sqlexpress /dbml:mymeta.dbml /database:northwind<br />
</span><br />
Generate source code from a .dbml metadata file:</p>
<p><span style="color: rgb(0, 0, 255);" mce_style="color: #0000ff;">sqlmetal /namespace:nwind /code:nwind.cs /language:csharp mymetal.dbml<br />
</span><br />
Generate source code from SQL metadata directly:</p>
<p><span style="color: rgb(0, 0, 255);" mce_style="color: #0000ff;">sqlmetal /server:myserver /database:northwind /namespace:nwind /code:nwind.cs /language:csharp</span></p>
<p><span style="color: rgb(0, 0, 0);" mce_style="color: #000000;">You can extract <b>only functions</b> or <b>only stored procedures</b> or <b>only views</b> using switches:</span></p>
<p><span style="color: rgb(0, 0, 255);" mce_style="color: #0000ff;">/views</span> Extracts database views.<br />
<span style="color: rgb(0, 0, 255);" mce_style="color: #0000ff;">/functions</span> Extracts database functions.<br />
<span style="color: rgb(0, 0, 255);" mce_style="color: #0000ff;">/sprocs</span> Extracts stored procedures.</p>
<p>You also have an option to output schema as .dbml file, source code or xml mapping file</p>
<p><span style="color: rgb(0, 0, 255);" mce_style="color: #0000ff;">/dbml[:file]</span> Sends output as <b>.dbml</b>. Cannot be used with /map option.<br />
<span style="color: rgb(0, 0, 255);" mce_style="color: #0000ff;">/code[:file]</span> Sends output as <b>source code</b>. Cannot be used with /dbml option.<br />
<span style="color: rgb(0, 0, 255);" mce_style="color: #0000ff;">/map[:file]</span> Generates an <b>XML mapping file</b> instead of attributes. Cannot be used with /dbml option.</p>
<p>Miscellaneous output options: </p>
<p><span mce_style="color: #0000ff;" style="color: rgb(0, 0, 255);">/language:&lt;language&gt;</span> Specifies source code language. Valid &lt;language&gt;: vb, csharp. Default value: Derived from extension on code file name.</p>
<p><span mce_style="color: #0000ff;" style="color: rgb(0, 0, 255);">/namespace:&lt;name&gt;</span> Specifies namespace of the generated code. Default value: no namespace.<br /><span mce_style="color: #0000ff;" style="color: rgb(0, 0, 255);"><br />/context:&lt;type&gt;</span> Specifies name of data context class. Default value: Derived from database name.</p>
<p><span mce_style="color: #0000ff;" style="color: rgb(0, 0, 255);">/entitybase:&lt;type&gt;</span> Specifies the base class of the entity classes in the generated code. Default value: Entities have no base class.</p>
<p><span mce_style="color: #0000ff;" style="color: rgb(0, 0, 255);">/pluralize</span> Automatically pluralizes or singularizes class and member names. This option is available only in the U.S. English version.<br /><span mce_style="color: #0000ff;" style="color: rgb(0, 0, 255);"><br />/serialization:&lt;option&gt;</span> Generates serializable classes. Valid &lt;option&gt;: None, Unidirectional. Default value: None.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.linqtosql.net/code-generation-tool-sqlmetal-exe/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to: Update Rows in the Database (LINQ to SQL)</title>
		<link>http://www.linqtosql.net/how-to-update-rows-in-the-database-linq-to-sql/</link>
		<comments>http://www.linqtosql.net/how-to-update-rows-in-the-database-linq-to-sql/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 10:05:47 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[linqtosql]]></category>
		<category><![CDATA[SubmitChages]]></category>
		<category><![CDATA[update linqtosql]]></category>
		<category><![CDATA[update row linq to sql]]></category>

		<guid isPermaLink="false">http://www.linqtosql.net/?p=29</guid>
		<description><![CDATA[To update a row in the database


Query the database for the row to be updated.
Make desired changes to member values in the resulting LINQ to SQL object.
Submit the changes to the database.



// Query the database for the row to be updated.
var query =
    from ord in db.Orders
    where ord.OrderID [...]]]></description>
			<content:encoded><![CDATA[<h3>To update a row in the database</h3>
<div>
<ol>
<li>Query the database for the row to be updated.</li>
<li>Make desired changes to member values in the resulting LINQ to SQL object.</li>
<li>Submit the changes to the database.</li>
</ol>
</div>
<div><span><span id="ctl00_MTCS_main_ctl22_ctl00_ctl01"></p>
<pre id="ctl00_MTCS_main_ctl22_ctl00_ctl01" style="white-space: pre-wrap;"><span style="color: green;">// Query the database for the row to be updated.</span>
var query =
    from ord <span style="color: blue;">in</span> db.Orders
    where ord.OrderID == 11000
    select ord;

<span style="color: green;">// Execute the query, and change the column values</span>
<span style="color: green;">// you want to change.</span>
<span style="color: blue;">foreach</span> (Order ord <span style="color: blue;">in</span> query)
{
    ord.ShipName = <span style="color: maroon;"><span style="color: maroon;">"Mariner"</span></span>;
    ord.ShipVia = 2;
    <span style="color: green;">// Insert any additional changes to column values.</span>
}

<span style="color: green;">// Submit the changes to the database.</span>
<span style="color: blue;">try</span>
{
    db.SubmitChanges();
}
<span style="color: blue;">catch</span> (Exception e)
{
    Console.WriteLine(e);
    <span style="color: green;">// Provide for exceptions.</span>
}</pre>
<p></span></span></div>
]]></content:encoded>
			<wfw:commentRss>http://www.linqtosql.net/how-to-update-rows-in-the-database-linq-to-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Inserting New Data</title>
		<link>http://www.linqtosql.net/inserting-new-data/</link>
		<comments>http://www.linqtosql.net/inserting-new-data/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 09:14:01 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[InsertOnSubmit]]></category>
		<category><![CDATA[linqtosql]]></category>
		<category><![CDATA[linqtosql InsertOnSubmit]]></category>

		<guid isPermaLink="false">http://www.linqtosql.net/?p=25</guid>
		<description><![CDATA[To insert a row into the database


Create a new object that includes the column data to be submitted.
Add the new object to the LINQ to SQL Table collection associated with the target table in the database.
Submit the change to the database.


What earlier was adding rows to tables, now is just adding new objects to context collections. [...]]]></description>
			<content:encoded><![CDATA[<h3>To insert a row into the database</h3>
<div>
<ol>
<li>Create a new object that includes the column data to be submitted.</li>
<li>Add the new object to the LINQ to SQL <strong>Table</strong> collection associated with the target table in the database.</li>
<li>Submit the change to the database.</li>
</ol>
</div>
<p>What earlier was adding rows to tables, now is just adding new objects to context collections. When you are ready to send the changes to the database, <strong>call <span style="color: #ff0000;">SubmitChanges()</span> method of the context</strong>. Before doing this, you <span style="color: #ff0000;">must first set all properties that <span style="text-decoration: underline;">do not support</span></span> <strong>null</strong> (<strong>Nothing</strong>) values. The SubmitChanges() method generates and executes commands that perform the equivalent INSERT, UPDATE, or DELETE statements against the data source.</p>
<p><span style="color: #0000ff;">CrmDemoDataContext context = new CrmDemoDataContext();</span></p>
<p><span style="color: #339966;">// Create a new category</span><br />
ProductCategory newCategory = new ProductCategory();<br />
newCategory.CategoryID = 1000;<br />
newCategory.CategoryName = &#8220;New category&#8221;;</p>
<p><span style="color: #339966;">// Create a new product<br />
</span>Product newProduct = new Product();<br />
newProduct.ProductID = 2000;<br />
newProduct.ProductName = &#8220;New product&#8221;;<br />
newProduct.Price = 20;</p>
<p><span style="color: #339966;">// Associate the new product with the new category<br />
</span>newProduct.ProductCategory = newCategory;<br />
context.Products.InsertOnSubmit(newProduct);</p>
<p><span style="color: #339966;">// Send the changes to the database.<br />
// Until you do it, the changes are cached on the client side.<br />
</span>context.SubmitChanges();</p>
<p><span style="color: #339966;">// Request the new product from the database<br />
</span>var query = from it in context.Products<br />
where it.ProductID == 2000<br />
select it;</p>
<p><span style="color: #339966;">// Since we query for a single object instead of a collection, we can use the method First()<br />
</span>Product product = query.First();<br />
Console.WriteLine(&#8221;{0} | {1} | {2}&#8221;,<br />
product.ProductCategory.CategoryName, product.ProductName, product.Price);<br />
Console.ReadLine();</p>
<p>The <strong>InsertOnSubmit()</strong> method is created for every collection in the context. This method stores in the database information about all linked objects. As shown in the example, it is only necessary to call InsertOnSubmit() once to submit both product and category objects.</p>
<p>Note that after you have added the new product and category by submitting the changes, you cannot execute this solution again as is. To execute the solution again, change the IDs of the objects to be added.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.linqtosql.net/inserting-new-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ColumnAttribute AutoSync</title>
		<link>http://www.linqtosql.net/columnattribute-autosync/</link>
		<comments>http://www.linqtosql.net/columnattribute-autosync/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 08:00:43 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AutoSync]]></category>
		<category><![CDATA[ColumnAttribute]]></category>
		<category><![CDATA[ColumnAttribute AutoSync]]></category>

		<guid isPermaLink="false">http://www.linqtosql.net/?p=22</guid>
		<description><![CDATA[LINQ to SQL: .NET Language-Integrated Query for Relational Data&#8221; says (last item in the &#8220;Column Attribute&#8221; section): &#8220;Specifies if the column is automatically synchronized from the value generated by the database on insert or update commands.
public enum AutoSync {
 Default = 0, // Automatically choose
Always = 1,
Never = 2,
OnInsert = 3,
OnUpdate = 4
}

]]></description>
			<content:encoded><![CDATA[<p>LINQ to SQL: .NET Language-Integrated Query for Relational Data&#8221; says (last item in the &#8220;Column Attribute&#8221; section): &#8220;Specifies if the column is automatically synchronized from the value generated by the database on insert or update commands.</p>
<p><span style="color: #808080; font-size: x-small;"><span style="color: #0000ff; font-size: x-small;">public</span><span style="font-size: x-small;"> </span><span style="color: #0000ff; font-size: x-small;">enum</span><span style="font-size: x-small;"> </span><span style="color: #2b91af; font-size: x-small;">AutoSync</span><span style="font-size: x-small;"> {</span></p>
<p><span style="font-size: x-small;"> Default = 0, </span><span style="color: #008000; font-size: x-small;">// Automatically choose</span></p>
<p><span style="font-size: x-small;">Always = 1,</p>
<p>Never = 2,</p>
<p>OnInsert = 3,</p>
<p>OnUpdate = 4</p>
<p>}</p>
<p></span></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.linqtosql.net/columnattribute-autosync/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using LINQ Data Layer</title>
		<link>http://www.linqtosql.net/using-linq-data-layer/</link>
		<comments>http://www.linqtosql.net/using-linq-data-layer/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 11:30:18 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[LINQ Data Layer]]></category>

		<guid isPermaLink="false">http://www.linqtosql.net/?p=17</guid>
		<description><![CDATA[Now that we have generated our data layer. We will work on ASP.NET web application where we will use our data layer. To keep things simple we will create a web forms to search for customers and display search results. We will also create a web form to insert new customers. Let’s start by creating [...]]]></description>
			<content:encoded><![CDATA[<p>Now that we have generated our data layer. We will work on ASP.NET web application where we will use our data layer. To keep things simple we will create a web forms to search for customers and display search results. We will also create a web form to insert new customers. Let’s start by creating our web form for customer search. For this we will use the Default.aspx page. We will place few controls on the web form. These controls will give us search parameters and a button which will do the search and display results when clicked. This is what the form will look like after placing our controls.</p>
<p><img class="alignnone size-full wp-image-18" title="image-thumb13-thumb" src="http://www.linqtosql.net/wp-content/uploads/2009/09/image-thumb13-thumb.png" alt="image-thumb13-thumb" width="396" height="160" /></p>
<p>We will also place a GridView control on our form to display search results. We will now put in some code in our button’s click event handler to do the search and display results in GridView. Make sure that we have a reference to Data Layer project, System.Data.Linq and appropriate using statement. Here is what our button click event handler will contain.</p>
<div style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new">
<pre style="MARGIN: 0px"><span style="COLOR: blue">protected</span> <span style="COLOR: blue">void</span> buttonSearch_Click(<span style="COLOR: blue">object</span> sender, <span style="COLOR: #2b91af">EventArgs</span> e)</pre>
<pre style="MARGIN: 0px">{</pre>
<pre style="MARGIN: 0px"><span style="COLOR: blue">  using</span> (<span style="COLOR: #2b91af">NorthwindDataContext</span> context = <span style="COLOR: blue">new</span> <span style="COLOR: #2b91af">NorthwindDataContext</span>())</pre>
<pre style="MARGIN: 0px">  {</pre>
<pre style="MARGIN: 0px"><span style="COLOR: blue">    var</span> customers =</pre>
<pre style="MARGIN: 0px"><span style="COLOR: blue">      from</span> c <span style="COLOR: blue">in</span> context.Customers</pre>
<pre style="MARGIN: 0px"><span style="COLOR: blue">      select</span> c;</pre>
<pre style="MARGIN: 0px">    gridViewCustomers.DataSource = customers;</pre>
<pre style="MARGIN: 0px">    gridViewCustomers.DataBind();</pre>
<pre style="MARGIN: 0px">  }</pre>
<pre style="MARGIN: 0px">}</pre>
</div>
<p>This code will query the customers table in northwind database and will return all customers. We will now modify it slightly to accept customer name and company name as parameters for our query. After modification our event handler looks like this.</p>
<div style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new">
<pre style="MARGIN: 0px"><span style="COLOR: blue">protected</span> <span style="COLOR: blue">void</span> buttonSearch_Click(<span style="COLOR: blue">object</span> sender, <span style="COLOR: #2b91af">EventArgs</span> e)</pre>
<pre style="MARGIN: 0px">{</pre>
<pre style="MARGIN: 0px"><span style="COLOR: blue">  using</span> (<span style="COLOR: #2b91af">NorthwindDataContext</span> context = <span style="COLOR: blue">new</span> <span style="COLOR: #2b91af">NorthwindDataContext</span>())</pre>
<pre style="MARGIN: 0px">  {</pre>
<pre style="MARGIN: 0px"><span style="COLOR: blue">    var</span> customers =</pre>
<pre style="MARGIN: 0px"><span style="COLOR: blue">      from</span> c <span style="COLOR: blue">in</span> context.Customers</pre>
<pre style="MARGIN: 0px"><span style="COLOR: blue">      where</span> (</pre>
<pre style="MARGIN: 0px">        c.ContactName.Contains(textBoxCustomerName.Text.Trim())</pre>
<pre style="MARGIN: 0px">        &amp;&amp;</pre>
<pre style="MARGIN: 0px">        c.CompanyName.Contains(textBoxCompanyName.Text.Trim()))</pre>
<pre style="MARGIN: 0px"><span style="COLOR: blue">      select</span> c;</pre>
<pre style="MARGIN: 0px">      gridViewCustomers.DataSource = customers;</pre>
<pre style="MARGIN: 0px">      gridViewCustomers.DataBind();</pre>
<pre style="MARGIN: 0px">  }</pre>
<pre style="MARGIN: 0px">}</pre>
</div>
<p>Our search results will now be filtered.</p>
<p>Let us now created a data entry form for customers.  We will insert a new web form in our ASP.NET project and call it CustomerEntry. To start with we will make sure that our form contains fields required to insert a customer. Our form after completion will look like this.</p>
<p><img class="alignnone size-full wp-image-19" title="image123" src="http://www.linqtosql.net/wp-content/uploads/2009/09/image123.png" alt="image123" width="357" height="549" /></p>
<p>We expect a new row to be inserted into customers table when Save Customer button is clicked. This code achieves data insertion into customers table for us.</p>
<div style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new">
<pre style="MARGIN: 0px"><span style="COLOR: blue">protected</span> <span style="COLOR: blue">void</span> buttonSave_Click(<span style="COLOR: blue">object</span> sender, <span style="COLOR: #2b91af">EventArgs</span> e)</pre>
<pre style="MARGIN: 0px">{</pre>
<pre style="MARGIN: 0px"><span style="COLOR: blue">  using</span> (<span style="COLOR: #2b91af">NorthwindDataContext</span> context = <span style="COLOR: blue">new</span> <span style="COLOR: #2b91af">NorthwindDataContext</span>())</pre>
<pre style="MARGIN: 0px">  {</pre>
<pre style="MARGIN: 0px"><span style="COLOR: #2b91af">    Customer</span> customer = <span style="COLOR: blue">new</span> <span style="COLOR: #2b91af">Customer</span></pre>
<pre style="MARGIN: 0px">    {</pre>
<pre style="MARGIN: 0px">      CustomerID = textBoxCustomerID.Text,</pre>
<pre style="MARGIN: 0px">      CompanyName = textBoxCompanyName.Text,</pre>
<pre style="MARGIN: 0px">      ContactName = textBoxCustomerName.Text,</pre>
<pre style="MARGIN: 0px">      ContactTitle = textBoxTitle.Text,</pre>
<pre style="MARGIN: 0px">      Address = textBoxAddress.Text,</pre>
<pre style="MARGIN: 0px">      City = textBoxCity.Text,</pre>
<pre style="MARGIN: 0px">      Region = textBoxRegion.Text,</pre>
<pre style="MARGIN: 0px">      PostalCode = textBoxPostalCode.Text,</pre>
<pre style="MARGIN: 0px">      Country = textBoxCountry.Text,</pre>
<pre style="MARGIN: 0px">      Phone = textBoxPhone.Text,</pre>
<pre style="MARGIN: 0px">      Fax = textBoxFax.Text</pre>
<pre style="MARGIN: 0px">    };</pre>
<pre style="MARGIN: 0px">    context.Customers.InsertOnSubmit(customer);</pre>
<pre style="MARGIN: 0px">    context.SubmitChanges();</pre>
<pre style="MARGIN: 0px">  }</pre>
<pre style="MARGIN: 0px">}</pre>
</div>
<p>Similarly an existing row in database can be updated by first retrieving the data and then submitting it via DataContext.</p>
<h4>Conclusion</h4>
<p>In this tutorial we have not written a single SQL statement to retrieve or insert data into a database. This is the beauty of LINQ To SQL. Further our retrieval code while in C# looks a lot like a query. We can already appreciate the benefits of such a streamlined and unified approach in dealing with data.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.linqtosql.net/using-linq-data-layer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating Data Layer</title>
		<link>http://www.linqtosql.net/creating-data-layer/</link>
		<comments>http://www.linqtosql.net/creating-data-layer/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 10:47:23 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.linqtosql.net/?p=8</guid>
		<description><![CDATA[1) Before we generate our data layer we must create a new connection in Server Explorer which points to Northwind database.

2) We will now generate our data layer using LINQ To SQL. To do this you need to add a new item to the data layer project of type LINQ to SQL Classes. We will [...]]]></description>
			<content:encoded><![CDATA[<p>1) Before we generate our data layer we must create a new connection in Server Explorer which points to Northwind database.</p>
<p><img class="alignnone size-full wp-image-9" title="image-thumb3" src="http://www.linqtosql.net/wp-content/uploads/2009/09/image-thumb3.png" alt="image-thumb3" width="297" height="280" /></p>
<p>2) We will now generate our data layer using LINQ To SQL. To do this you need to add a new item to the data layer project of type LINQ to SQL Classes. We will name it Northwind as shown below.</p>
<p><img class="alignnone size-full wp-image-10" title="image41" src="http://www.linqtosql.net/wp-content/uploads/2009/09/image41.png" alt="image41" width="570" height="402" /></p>
<p>3) After adding a LINQ to SQL Class we are presented with a designer surface. Here we can simply drag the tables which will become part of our data layer. For this article we will drag all tables on the designer by selecting them all in one go. Our designer should look like this after dragging all tables on it.</p>
<p><img class="alignnone size-full wp-image-12" title="image30-thumb" src="http://www.linqtosql.net/wp-content/uploads/2009/09/image30-thumb.png" alt="image30-thumb" width="451" height="638" /></p>
<p>4) We should now build our solution to make sure everything is okay. And that’s it. We have successfully generated our data layer. In Solution Explorer we can see that we have two new files namely <strong>Northwind.dbml.layout</strong> and <strong>Northwind.designer.cs</strong>. We can also see that references required to compile and run our code have been added by Visual Studio.</p>
<p><img class="alignnone size-full wp-image-14" title="image36" src="http://www.linqtosql.net/wp-content/uploads/2009/09/image36.png" alt="image36" width="282" height="509" /></p>
<p>The <span style="color: #ff0000;"><strong>.cs</strong> </span>file contains the <span style="color: #ff0000;">code for our data layer</span>. Let’s examine the code that has been generated for us. We will look at the Region class.</p>
<div style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new">
<pre style="MARGIN: 0px">[<span style="COLOR: #2b91af">Table</span>(Name=<span style="COLOR: #a31515">"dbo.Region"</span>)]</pre>
<pre style="MARGIN: 0px"><span style="COLOR: blue">public</span> <span style="COLOR: blue">partial</span> <span style="COLOR: blue">class</span> <span style="COLOR: #2b91af">Region</span> : <span style="COLOR: #2b91af">INotifyPropertyChanging</span>, <span style="COLOR: #2b91af">INotifyPropertyChanged</span></pre>
</div>
<p>The class itself is decorated with <span style="color: #ff0000;">Table attribute</span> and the <span style="color: #ff0000;">Name property</span> has been assigned the actual table name we have in our database. Region class also implements <span style="color: #ff0000;">INotifyPropertyChanging and INotifyPropertyChanged interfaces</span>. These interfaces are used <strong><span style="color: #ff0000;">for databinding</span></strong>. Region class also contains <strong><span style="color: #0000ff;">one property per column</span></strong>. Let’s look at the RegionDescription property.</p>
<div style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new">
<pre style="MARGIN: 0px">[<span style="COLOR: #2b91af">Column</span>(Storage=<span style="COLOR: #a31515">"_RegionDescription"</span>, DbType=<span style="COLOR: #a31515">"NChar(50) NOT NULL"</span>,</pre>
<pre style="MARGIN: 0px">CanBeNull=<span style="COLOR: blue">false</span>)]</pre>
<pre style="MARGIN: 0px"><span style="COLOR: blue">public</span> <span style="COLOR: blue">string</span> RegionDescription</pre>
<pre style="MARGIN: 0px">{</pre>
<pre style="MARGIN: 0px"><span style="COLOR: blue">  get</span></pre>
<pre style="MARGIN: 0px">  {</pre>
<pre style="MARGIN: 0px"><span style="COLOR: blue">    return</span> <span style="COLOR: blue">this</span>._RegionDescription;</pre>
<pre style="MARGIN: 0px">  }</pre>
<pre style="MARGIN: 0px"><span style="COLOR: blue">  set</span></pre>
<pre style="MARGIN: 0px">  {</pre>
<pre style="MARGIN: 0px"><span style="COLOR: blue">    if</span> ((<span style="COLOR: blue">this</span>._RegionDescription != <span style="COLOR: blue">value</span>))</pre>
<pre style="MARGIN: 0px">    {</pre>
<pre style="MARGIN: 0px"><span style="COLOR: blue">      this</span>.OnRegionDescriptionChanging(<span style="COLOR: blue">value</span>);</pre>
<pre style="MARGIN: 0px"><span style="COLOR: blue">      this</span>.SendPropertyChanging();</pre>
<pre style="MARGIN: 0px"><span style="COLOR: blue">      this</span>._RegionDescription = <span style="COLOR: blue">value</span>;</pre>
<pre style="MARGIN: 0px"><span style="COLOR: blue">      this</span>.SendPropertyChanged(<span style="COLOR: #a31515">"RegionDescription"</span>);</pre>
<pre style="MARGIN: 0px"><span style="COLOR: blue">      this</span>.OnRegionDescriptionChanged();</pre>
<pre style="MARGIN: 0px">    }</pre>
<pre style="MARGIN: 0px">  }</pre>
<pre style="MARGIN: 0px">}</pre>
</div>
<p>Columns are decorated with <span style="color: #0000ff;">Column attribute</span> and values are passed in for <span style="color: #0000ff;">Storage, DbType and CanBeNull</span> which indicates if the column can be null or not.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.linqtosql.net/creating-data-layer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LINQ Frees You From Writing Looping Code</title>
		<link>http://www.linqtosql.net/linq-frees-you-from-writing-looping-code/</link>
		<comments>http://www.linqtosql.net/linq-frees-you-from-writing-looping-code/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 10:27:48 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[IEnumerable]]></category>
		<category><![CDATA[IQueryable]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://linqtosql.net/?p=6</guid>
		<description><![CDATA[Traditionally, working with collections of objects meant writing a lot of looping code using for loops or foreach loops to iterate through a list carrying out filtering using if statements, and some action like keeping a running sum of a total property. LINQ frees you from having to write looping code; it allows you to [...]]]></description>
			<content:encoded><![CDATA[<p>Traditionally, working with collections of objects meant writing a lot of looping code using for loops or foreach loops to iterate through a list carrying out filtering using if statements, and some action like keeping a running sum of a total property. LINQ frees you from having to write looping code; it allows you to <strong>write queries that filter a list or calculate aggregate functions on elements in a collection as a set</strong>.</p>
<p>We can write queries against <strong><span style="color: #ff0000;">any collection type</span></strong> that implements an interface called <strong><span style="color: #ff0000;">IEnumerable</span></strong> (and also a new interface called <strong><span style="color: #ff0000;">IQueryable</span></strong>, but more on that later). This is <strong><span style="color: #ff0000;">almost any collection type built into the .NET</span></strong> class libraries including simple arrays like string[], or int[], and any List&lt;T&gt; collection we define. Let us look at a few of the simplest examples to understand the basic syntax.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.linqtosql.net/linq-frees-you-from-writing-looping-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is LINQ</title>
		<link>http://www.linqtosql.net/what-is-linq/</link>
		<comments>http://www.linqtosql.net/what-is-linq/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 10:19:10 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://linqtosql.net/?p=3</guid>
		<description><![CDATA[Language Integrated Query (or LINQ, pronounced “link” for short), is a set of Microsoft® .NET technologies that provide built-in language querying functionality similar to SQL, not only for database access, but for accessing data from any source.
There is no one definition for LINQ, but it aims to solve the problem where we currently use different [...]]]></description>
			<content:encoded><![CDATA[<p>Language Integrated Query (or LINQ, pronounced “link” for short), is a set of Microsoft® .NET technologies that provide built-in language querying functionality similar to SQL, not only for database access, but for accessing data from any source.</p>
<p>There is no one definition for LINQ, but it aims to solve the problem where we currently use different techniques for manipulating and selecting data from databases versus XML versus object collections; LINQ aims to make our life easier by giving us a coordinated, consistent and efficient syntax from our development environment and by using your one chosen programming language, rather than switching between programming languages.</p>
<p>The current LINQ family of technologies and concepts allows an extensible set of operators that work over objects, <strong>SQL data and XML data sources</strong>. The generalized architecture of the technology also allows the LINQ concepts to be expanded to almost any data domain or technology, so what LINQ is today will expand in the future.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.linqtosql.net/what-is-linq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
