Tuesday, October 9, 2007

C# Coding standards guideline

Scott Guthrie published another post in the link listing series. One of my favorites, is the link to the C# coding standards document, which was written by Juval Lowry from IDesign.

As Scott wrote:

Extremely useful if you are looking for a concise checklist of good suggestions to keep your codebase clean.

Technorati Tags: , ,

Monday, October 8, 2007

Reset autonumber (IDENTITY) column in SQL Server

Sometimes we need to reset the autonumber column in a table.

Using the DELETE statement is not enough. The statement only delete the data but not reset the identity column.

So how can we do it?

   1:  DELETE FROM tblName 
   2:   
   3:  DBCC CHECKIDENT (tblName,RESEED, 0)
 
 

 

Technorati Tags:

 



Sunday, October 7, 2007

I Search with Google’s experimental

Google recently added "Experiemental Search" to their list of Google labs options. Yesterday I started to search with it, and I found it quite interesting.

There are currently four different experimental search options. You just have to go to Google Experimental and select one of the four features that are currently tested.

  1. New ways to view search results: using specialized snippets, on a timeline or on a map. You can also access this feature without joining the experiment, using the view operator: just add view:info, view:timeline or view:map to your query.
  2. keyboard shortcuts: select a search result or move to the next result without using your mouse. You'll like it if you use shortcuts in Gmail or Google Reader.
  3. Two similar experiments: put the search navigation at the left/right of the page.

 

Timeline and maps view
Timeline view is a great way to sort results, especially for research queries. The results are grouped by years (or decades) and ordered from oldest down to the most recent. At the top of the screen is a linkable timeline that allows you to filter the results by selected time periods.

For example, I would like to know Microsoft's history.

I query: microsoft view:timeline and I got:

01

Keyboard shortcuts
The keyboard shortcut interface is just as it sounds, it provides a quick and easy way to navigate the search results using only our keyboard.

Key Action
J Selects the next result.
K Selects the previous result.
O Opens the selected result.
<Enter> Opens the selected result.
/ Puts the cursor in the search box.
<Esc> Removes the cursor from the search box.

02

left/Right hand search navigation

03

 

You can only choose one experiment at a time, it's easy to switch between them or deactivate them and there's an option to send your feedback.

Enjoy!!

Microsoft Israel link to my post

I am really excited. I notified that the opening page of Microsoft Israel mention my post about query XML using XLinq.

ms

It's great!

Query XML using XLINQ

LINQ to XML is a built-in LINQ data provider that is implemented within the System.Xml.Linq namespace in .NET 3.5.
It enables us do the following to XML data:

  • Read.
  • Construct.
  • Write.

We can perform LINQ queries over XML from the file-system, from a remote HTTP URL or web-service, or from any in-memory XML content.
LINQ to XML provides much richer (and easier) querying and data shaping support than the low-level XmlReader/XmlWriter API in .NET 2 and also much more efficient with usage of much less memory than the DOM API that XmlDocument provides. That's because it does not require you to always have a document object to be able to work with XML. Therefore, you can work directly with nodes and modify them as content of the document without having to start from a root XmlDocument object. This is a very powerful and flexible feature that you can use to compose larger trees and XML documents from tree fragments. Now that you have an overview of the XLinq's capabilities, the next few sections will examine the reading and writing features of XLinq before discussing the query capabilities.

01 (Figure and classes explanations are taken from XLINQ overview.doc)

Of the classes shown in this figure, the XNode and XContainer classes are abstract. The XNode class is the base for element nodes, and provides a Parent method and methods such as AddBeforeThis, AddAfterThis, and Remove for updates in the imperative style. For IO, it provides methods for reading (ReadFrom) and writing (WriteTo).
Although the XElement class is bottom-most in the class hierarchy, it is the fundamental class. As the name suggests, it represents an XML element and allows you to perform the following operations:

  • Create elements with a specified element name
  • Change the element's contents
  • Add, change, or delete child elements
  • Add attributes to the element
  • Save the element as an XML fragment
  • Extract the contents in text form

This post will introduce hoe to query xml and it is the first of series of posts regards to XLINQ.

Lets get a sense of how LINQ to XML works!

Query xml from URL

   1:  public static void GetRssFeedFromURL()
   2:  {
   3:      string url = "http://feeds.feedburner.com/MaorDavid?format=xml";
   4:   
   5:      // load the rss feeds into the XElement
   6:      XElement feeds = XElement.Load(url);
   7:   
   8:   
   9:      if (feeds.Element("channel") != null)
  10:      {
  11:          var query = from f in feeds.Element("channel").Elements("item").Take(10)
  12:                      select new { Title = f.Element("title").Value, Link = f.Element("link").Value };
  13:   
  14:          foreach (var feed in query)
  15:          {
  16:              Console.WriteLine(String.Format("Feed title: {0}",feed.Title));
  17:              Console.WriteLine(String.Format("Link: {0}",feed.Link));
  18:          }
  19:      }
  20:  }

XLinq is an XML query language that inherits from the LINQ query foundation. You can use it to query XLinq objects such as XElement, XDocument, etc using LINQ query facilities.

We start by loading the XML into memory using the Load() method of the XElement class. (Line 6).


After loading the XML , the next step is to retrieve all items (Line 11) and now you can query and iterate the results as described in my previous posts.(Var keyword, Getting started with Linq).

Very simple! This example load an XML from URL. What if you want to query XML from the file system? Nothing changed beside the uri parameter to load into the XElement. (Line 6)


   1:  public static void GetRssFeedFromFile()
   2:  {
   3:      string path = @"C:\Work\Projects\Samples\VS2008Samples\LinqToXML\Maor Davids Blog.xml";
   4:   
   5:      // load the rss feeds into the XElement
   6:      XElement feeds = XElement.Load(path);
   7:   
   8:   
   9:      if (feeds.Element("channel") != null)
  10:      {
  11:          var query = from f in feeds.Element("channel").Elements("item").Take(10)
  12:                      select new { Title = f.Element("title").Value, Link = f.Element("link").Value };
  13:   
  14:          foreach (var feed in query)
  15:          {
  16:              Console.WriteLine(String.Format("Feed title: {0}", feed.Title));
  17:              Console.WriteLine(String.Format("Link: {0}", feed.Link));
  18:          }
  19:      }
  20:  }

 

As you can see, all the code snippets presented above are fairly simple. Once the XML loaded into the LINQ to XML API, you can write queries over that tree. The query syntax is easier than XPath or XQuery.

Enjoy!!

Friday, October 5, 2007

Operations Guidance for Team Foundation Server

Do you want more guidance on daily operations in Team System?

Microsoft just released the Team Foundation Operations Guidance to address the issue of planning the installation and operation of a Team Foundation.  It also covers monitoring, topology, operations plans, users and groups, where the configuration information is, and backup/restore planning.

Check it out here:  http://msdn2.microsoft.com/en-us/library/bb663036(VS.80).aspx

Thursday, October 4, 2007

Getting started with Astoria

Few weeks ago, the CTP refresh for VS 2008 Beta 2 of Astoria was released. Astoria is a really cool project based on .NET 3.0 (WFC), Entity Framework to provide pure HTTP access to SQL Server data.In order to play with it, first of all you need to install the following:

After installing all we need, we can create out first project - this post will describe the steps you need in order to do that.

Create new Web Application Project:

1

After I did it, I need to add new ADO.NET Entity model. On the Project menu, click 'Add new item' and select the ADO.NET Entity Data Model.

2

Now, the wizard will start. Select: Generate model from database.

3

Next we'll save the connection info

4

I checked the option to add connection string to web.config and that's what I got there:

<add name="NorthwindEntities" connectionString="metadata=~/bin/NorthWind.csdl|~/bin/NorthWind.ssdl|~/bin/NorthWind.msl;
provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\sqlexpress;Initial Catalog=Northwind;
Integrated Security=True&quot;"
providerName="System.Data.EntityClient" />

The next step we select the objects we want. I choose Tables only.


5


What we got so far? .edmx file was added to our project, which is the schema.


6


Next step we should add and setup Astoria service. We'll do it from the Project menu --> Add new item.


7


New class was generated, it's not full cause we should select the type of the service.


8


I added the NorthwindModel.NorthwindEntities type. (NorthwindModel is the model I generated)


9


Now everything ready to compile...


After compilation, I can browse to the service I created. Look what I got:


10


The service describes the schema with the object I selected (Tables only).


How can I query the service? For example, if I want all the categories, I'll change in the address in my browser to: http://localhost:58889/NorthwindWebDataService.svc/Categories and I got all the categories:


(localhost:58889 is on my computer off course, change your host:port to match yours)


11


Amazing....


What about query it from asp.net app? There is a class that comes with the Astoria samples that provides a class and series of methods to create a more encapsulated approach to calling an Astoria web data service. You can find it at the installation folder.


What if I want to query the service to get the customers order by contact name:


http://localhost:58889/NorthwindWebDataService.svc/Customers?$orderby=ContactName


and what if I want the top 10 records:


http://localhost:58889/NorthwindWebDataService.svc/Customers?$orderby=ContactName&$top=10


Conclusion


Astoria is set to provide the developers a richer way of interacting with data on the server. The syntax is easy and there is a more structure API on the way for ASP.NET AJAX.


 


Technorati Tags: ,