<?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>dotnet thoughts &#187; SQL Server</title>
	<atom:link href="http://www.dotnetthoughts.net/tag/sql-server/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetthoughts.net</link>
	<description>a dotnet developer&#039;s technical blog</description>
	<lastBuildDate>Thu, 02 Feb 2012 03:18:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Introduction to Code First development with Entity Framework</title>
		<link>http://www.dotnetthoughts.net/2012/01/01/introduction-to-code-first-development-with-entity-framework/</link>
		<comments>http://www.dotnetthoughts.net/2012/01/01/introduction-to-code-first-development-with-entity-framework/#comments</comments>
		<pubDate>Sun, 01 Jan 2012 14:52:41 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 4.0]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[ASP.Net MVC]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code First]]></category>
		<category><![CDATA[Entity Framework Code First]]></category>
		<category><![CDATA[Entity Framewrok]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=2194</guid>
		<description><![CDATA[The Database First approach is interesting when the database already exists. You use Visual Studio and the Entity Framework Designer to generate the C# and VB.NET classes which reflect the existing database model. You may then change relations using the &#8230; <a href="http://www.dotnetthoughts.net/2012/01/01/introduction-to-code-first-development-with-entity-framework/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">The Database First approach is interesting when the database already exists. You use Visual Studio and the Entity Framework Designer to generate the C# and VB.NET classes which reflect the existing database model. You may then change relations using the Designer (or the XML mapping files) later to further optimize the model. The priority is the database &#8211; the code and the model are only secondary. When your priority is the code and you want to begin from scratch without any existing schema or XML mapping files using source code, then the approach is called CodeFirst. Code-First enables an easy development workflow. It enables you to:</p>
<ol>
<li>Develop without opening the designer or mapping in XML files.</li>
<li>Define model objects by simply writing POCO(Plain old CLR object) with no base classes required</li>
<li>Use a &#8220;convention over configuration&#8221; approach that enables database persistence without explicitly configuring anything</li>
</ol>
<p>You can download the latest version of code first from this url : <a href="http://www.microsoft.com/download/en/details.aspx?id=26825">ADO.NET Entity Framework 4.1 &#8211; Update 1</a>. </p>
<p>You can also install via NuGet.</p>
<blockquote><p>install-package entityframework</p></blockquote>
<p><strong>Quick CRUD example with EF Code first.</strong></p>
<p>First we will create the model classes.</p>
<pre class="brush: csharp; title: ; notranslate">
public class Task
{
    public int TaskId { get; set; }
    public string TaskDescription { get; set; }
    public DateTime TaskDate { get; set; }
    public short TaskPriority { get; set; }
    public bool TaskIsDone { get; set; }
}
</pre>
<p>Now we need to create a Context for this. Context is required to use the POCO classes for DataAccess. It can be implemented by deriving it from DbContext class. The DbContext class is available under System.Data.Entity namespace.</p>
<pre class="brush: csharp; title: ; notranslate">
public class TaskContext : DbContext
{
    public DbSet Tasks { get; set; }
}
</pre>
<p>We are done. <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Now we can use this class for communicating to Database.</p>
<p>Inserting records to Database.</p>
<pre class="brush: csharp; title: ; notranslate">
using (TaskContext taskContext = new TaskContext())
{
    taskContext.Tasks.Add(new Task()
    {
        TaskDate = DateTime.Today,
        TaskDescription = &quot;Create a blog post&quot;,
        TaskIsDone = false,
        TaskPriority = 1
    });
    if (taskContext.SaveChanges() != 0)
    {
        Console.WriteLine(&quot;Task added.&quot;);
    }
}
</pre>
<p>Updating records, updating the first task from Tasks, if the Task date is today.</p>
<pre class="brush: csharp; title: ; notranslate">
using (TaskContext taskContext = new TaskContext())
{
    var currentTask = taskContext.Tasks.Where
		(task =&gt; task.TaskDate == DateTime.Today).FirstOrDefault();
	currentTask.TaskIsDone = true;
    if (taskContext.SaveChanges() != 0)
    {
        Console.WriteLine(&quot;Task updated.&quot;);
    }
}
</pre>
<p>Removing records, removing first task from Tasks, if the Task is completed.</p>
<pre class="brush: csharp; title: ; notranslate">
using (TaskContext taskContext = new TaskContext())
{
    var completedTask = taskContext.Tasks.Where
        (task =&gt; task.TaskIsDone == true).FirstOrDefault();
    taskContext.Tasks.Attach(completedTask);
    taskContext.Tasks.Remove(completedTask);
    if (taskContext.SaveChanges() != 0)
    {
        Console.WriteLine(&quot;Task Deleted.&quot;);
    }
}
</pre>
<p>And retriving records, retriving all the tasks.</p>
<pre class="brush: csharp; title: ; notranslate">
using (TaskContext taskContext = new TaskContext())
{
	var tasks = taskContext.Tasks.Where
		(task =&gt; task.TaskDate == DateTime.Today);
	tasks.ToList().ForEach(task =&gt;
        Console.WriteLine(&quot;Task : {0} IsCompleted : {1}&quot;,
        task.TaskDescription, task.TaskIsDone));
}
</pre>
<p>It is very easy right? Another interesting question is where this data is getting stored? Because we didn&#8217;t specified any Database, or Connection string. By default the database will be created on local instance of SQLEXPRESS (localhost\SqlExpress). The database is named after the fully qualified name of your derived context, in our case that it will be &#8220;EFCodeFirst.TaskContext&#8221;. You can control this by providing connection string or by setting DefaultConnectionFactory.</p>
<p>You can provide connectionstring either in App.Config / Web.Config file or in code.</p>
<p>If you are providing the connection string in config, file the name of the connection string element should be same as Context class name. Like this</p>
<pre class="brush: xml; title: ; notranslate">
&lt;connectionStrings&gt;
  &lt;add name=&quot;TaskContext&quot;
  connectionString=&quot;Server=.\SqlExpress; Integrated Security=SSPI; Database=TaskDb;&quot;
  providerName=&quot;System.Data.SqlClient&quot;/&gt;
&lt;/connectionStrings&gt;
</pre>
<p>And if you want to use your own connection string name, you can do this using CreateConnection() method of the DefaultConnectionFactory class. You can also specify the connection string. You need to provide this information before creating the instance of task context.</p>
<pre class="brush: csharp; title: ; notranslate">
Database.DefaultConnectionFactory.CreateConnection(&quot;TaskDbConnection&quot;);
using (TaskContext taskContext = new TaskContext())
{
	//Code.
}
</pre>
<p>Entity Framework also supports various databases also, like SQL CE, MySql etc. This can be achive either using the provider name is the config settings or by using DefaultConnectionFactory propery of Database class. Following statement helps to use SqlCe database instead of Sql Server</p>
<pre class="brush: csharp; title: ; notranslate">
Database.DefaultConnectionFactory =
	new SqlCeConnectionFactory(&quot;System.Data.SqlServerCe.4.0&quot;);
using (TaskContext taskContext = new TaskContext())
{
	//Code.
}
</pre>
<p>Model validations also supported by entity framework. I may cover them in another post.</p>
<p>Happy 2012 <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="betterrelated"><p><strong>Related content:</strong></p>
<ol><li> <a href="http://www.dotnetthoughts.net/2009/10/07/how-to-store-and-retrieve-files-from-sql-server-database/" title="Permanent link to How to Store and Retrieve files from SQL Server Database">How to Store and Retrieve files from SQL Server Database</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2011/06/30/how-to-use-taskdialog-api-in-c/" title="Permanent link to How to use TaskDialog API in C#">How to use TaskDialog API in C#</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/10/22/implementing-custom-paging-in-datarepeater-using-c-and-sql-server/" title="Permanent link to Implementing Custom Paging in DataRepeater using C# and SQL Server">Implementing Custom Paging in DataRepeater using C# and SQL Server</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2011/02/22/quick-introduction-to-sqlite-with-c/" title="Permanent link to Quick introduction to SQLite with C#">Quick introduction to SQLite with C#</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2010/11/25/raven-db-introduction/" title="Permanent link to Raven DB &#8211; Introduction">Raven DB &#8211; Introduction</a>  </li>
</ol></div>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2012/01/01/introduction-to-code-first-development-with-entity-framework/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>SQL Server 2012 Release Candidate is now Available!</title>
		<link>http://www.dotnetthoughts.net/2011/11/22/sql-server-2012-release-candidate-is-now-available/</link>
		<comments>http://www.dotnetthoughts.net/2011/11/22/sql-server-2012-release-candidate-is-now-available/#comments</comments>
		<pubDate>Tue, 22 Nov 2011 15:43:33 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server 2012 RC]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=2090</guid>
		<description><![CDATA[SQL Server 2012 RC is the first release which is feature complete and represents an exciting milestone for customers and partners looking to experience the full value of SQL Server 2012. Today, you have the chance to download, preview and &#8230; <a href="http://www.dotnetthoughts.net/2011/11/22/sql-server-2012-release-candidate-is-now-available/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div id="attachment_2093" class="wp-caption alignleft" style="width: 191px"><a href="http://www.dotnetthoughts.net/2011/11/22/sql-server-2012-release-candidate-is-now-available/logo_sqlserver/" rel="attachment wp-att-2093"><img class="size-full wp-image-2093" title="SQL Server 2012" src="http://www.dotnetthoughts.net/wp-content/uploads/2011/11/logo_sqlserver.gif" alt="SQL Server 2012" width="181" height="48" /></a><p class="wp-caption-text">SQL Server 2012</p></div>
<p>SQL Server 2012 RC is the first release which is feature complete and represents an exciting milestone for customers and partners looking to experience the full value of SQL Server 2012. Today, you have the chance to download, preview and deploy the full marquee capabilities being delivered in SQL Server 2012. RC is a production quality release that includes access to upgrade and migration tools like Upgrade Advisor, Distributed Replay and SQL Server Migration Assistant (SSMA). Upgrade Advisor and Distributed Replay allow you to perform thorough analysis and testing of your current SQL Server applications before upgrading so you know what to expect. You can also use SSMA to automate migrate non-SQL Server databases to SQL Server 2012.</p>
<p>Download SQL Server 2012 RC today <a href="http://www.microsoft.com/download/en/details.aspx?id=28145">here</a>. Learn more about SQL Server 2012 at <a href="http://www.microsoft.com/sqlserver">www.microsoft.com/sqlserver</a></p>
<div class="betterrelated"><p><strong>Related content:</strong></p>
<ol><li> <a href="http://www.dotnetthoughts.net/2009/12/18/enumerating-instances-of-sql-server-using-c/" title="Permanent link to Enumerating Instances of SQL Server using C#">Enumerating Instances of SQL Server using C#</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2011/10/05/how-to-enable-remote-connections-to-sql-server-2008-using-command-line/" title="Permanent link to How to enable remote connections to SQL Server 2008 using command line">How to enable remote connections to SQL Server 2008 using command line</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2008/10/20/changing-authentication-mode-and-enabling-sa-using-osql/" title="Permanent link to Changing authentication mode and enabling sa using osql">Changing authentication mode and enabling sa using osql</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/07/06/filestream-in-sql-server-2008/" title="Permanent link to FILESTREAM in SQL Server 2008">FILESTREAM in SQL Server 2008</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/06/11/mini-teched-in-trivandrum/" title="Permanent link to Mini TechEd in Trivandrum">Mini TechEd in Trivandrum</a>  </li>
</ol></div>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2011/11/22/sql-server-2012-release-candidate-is-now-available/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to enable remote connections to SQL Server 2008 using command line</title>
		<link>http://www.dotnetthoughts.net/2011/10/05/how-to-enable-remote-connections-to-sql-server-2008-using-command-line/</link>
		<comments>http://www.dotnetthoughts.net/2011/10/05/how-to-enable-remote-connections-to-sql-server-2008-using-command-line/#comments</comments>
		<pubDate>Wed, 05 Oct 2011 12:38:55 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Commandline]]></category>
		<category><![CDATA[Remote Connections]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL2008]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=1838</guid>
		<description><![CDATA[Here is a small sql snippet, which will helps to enable remote connections using Command Line. It will enable remote connections to your SQL Server. Happy Coding No related content found.]]></description>
			<content:encoded><![CDATA[<p>Here is a small sql snippet, which will helps to enable remote connections using Command Line.</p>
<pre class="brush: sql; title: ; notranslate">
EXEC sys.sp_configure N'remote access', N'1'
GO
RECONFIGURE WITH OVERRIDE
GO
</pre>
<p>It will enable remote connections to your SQL Server.<br />
Happy Coding <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="betterrelated none"><p>No related content found.</p></div>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2011/10/05/how-to-enable-remote-connections-to-sql-server-2008-using-command-line/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to fix Error Code 29506, While installing SQL Management Studio Express on Windows 7 x64</title>
		<link>http://www.dotnetthoughts.net/2011/07/15/how-to-fix-error-code-29506-while-installing-sql-management-studio-express-on-windows-7-x64/</link>
		<comments>http://www.dotnetthoughts.net/2011/07/15/how-to-fix-error-code-29506-while-installing-sql-management-studio-express-on-windows-7-x64/#comments</comments>
		<pubDate>Fri, 15 Jul 2011 17:59:34 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[Error Code 29506]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=1754</guid>
		<description><![CDATA[Today I started installing SQL Server Management Studio express on my Windows 7 x64, and I started installing it, after few seconds, I got an error message like this This installer has encountered an unexpected error installing this package. This &#8230; <a href="http://www.dotnetthoughts.net/2011/07/15/how-to-fix-error-code-29506-while-installing-sql-management-studio-express-on-windows-7-x64/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Today I started installing SQL Server Management Studio express on my Windows 7 x64, and I started installing it, after few seconds, I got an error message like this</p>
<blockquote><p>This installer has encountered an unexpected error installing this package. This may indicate a problem with this package. The error code is 29506</p></blockquote>
<div id="attachment_1755" class="wp-caption aligncenter" style="width: 390px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2011/07/sqlerror.png"><img class="size-full wp-image-1755" title="Error Code 29506, While installing SQL Management Studio Express on Windows 7 x64" src="http://www.dotnetthoughts.net/wp-content/uploads/2011/07/sqlerror.png" alt="Error Code 29506, While installing SQL Management Studio Express on Windows 7 x64" width="380" height="188" /></a><p class="wp-caption-text">Error Code 29506, While installing SQL Management Studio Express on Windows 7 x64</p></div>
<p>My initial thought was may be it is because of some problem with my downloaded installer. I downloaded it again and still the problem exists. Then I tried it with <strong>commandline(cmd)</strong>, with <strong>Run As Administrator option</strong> and its worked <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="betterrelated"><p><strong>Related content:</strong></p>
<ol><li> <a href="http://www.dotnetthoughts.net/2011/09/29/csc-error-cs1548-cryptographic-failure-while-signing-assembly-xxx-dll-error-signing-assembly-access-is-denied/" title="Permanent link to CSC : error CS1548: Cryptographic failure while signing assembly xxx.dll Error signing assembly &#8212; Access is denied">CSC : error CS1548: Cryptographic failure while signing assembly xxx.dll Error signing assembly &#8212; Access is denied</a>  </li>
</ol></div>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2011/07/15/how-to-fix-error-code-29506-while-installing-sql-management-studio-express-on-windows-7-x64/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Implementing Paging in SQL Server 2005 Stored Procedures</title>
		<link>http://www.dotnetthoughts.net/2010/04/29/implementing-paging-in-sql-server-2005-stored-procedures/</link>
		<comments>http://www.dotnetthoughts.net/2010/04/29/implementing-paging-in-sql-server-2005-stored-procedures/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 15:21:24 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Paging]]></category>
		<category><![CDATA[SQL 2005]]></category>
		<category><![CDATA[Stored Procedure]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=870</guid>
		<description><![CDATA[Normally for implementing Paging in Grid View, we enable the Paging property in GridView and will write code in the PageIndex Changed event. But the problem with this approach is it will fetch all the data from Database for Paging, &#8230; <a href="http://www.dotnetthoughts.net/2010/04/29/implementing-paging-in-sql-server-2005-stored-procedures/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Normally for implementing Paging in Grid View, we enable the Paging property in GridView and will write code in the PageIndex Changed event. But the problem with this approach is it will fetch all the data from Database for Paging, will result wastage of network bandwidth and resources, also affect in the Page Performance. In SQL Server 2005 Microsoft introduced a new concepts called Row_Number() and Derived Table with the help these two we can move the Paging logic to Database instead of ASP.Net web forms. This concept also works with custom paging implementations for DataList or Repeater controls.</p>
<pre class="brush: sql; title: ; notranslate">
CREATE PROCEDURE usp_GetContacts
(@Page INT, @RecsPerPage INT)
AS
SELECT tblContacts.[Name], tblContacts.[IsParent] FROM
(SELECT ROW_NUMBER() OVER(ORDER BY [ID]) AS RowNumber,
[Name], [IsParent] FROM [TreeView].[dbo].[Contacts]) tblContacts
WHERE RowNumber &gt; @RecsPerPage*(@Page) AND RowNumber &lt;= @RecsPerPage*(@Page+1)
</pre>
<p>In this code we are creating a Column called &#8220;RowNumber&#8221;. And we are comparing the input parameters with the derived table tblContacts.</p>
<p>Thanks to Aneesh / Prasanth for their valueable suggestions and comments.</p>
<div class="betterrelated"><p><strong>Related content:</strong></p>
<ol><li> <a href="http://www.dotnetthoughts.net/2011/11/15/how-to-persist-checkbox-state-in-gridview-while-paging/" title="Permanent link to How to persist checkbox state in gridview while paging">How to persist checkbox state in gridview while paging</a>  </li>
</ol></div>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2010/04/29/implementing-paging-in-sql-server-2005-stored-procedures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

