Code Snippets in Visual Studio 2005
page 6 of 7
by Bilal Haidar
Feedback
Average Rating: 
Views (Total / Last 10 Days): 35101/ 41

How to Build a Custom Code Snippet

In this section, I will consider a step-by-step process to show you how to create a new code snippet and add it to the Visual Studio to be accessible any time you want to use it.

The code snippet I will create will be a method that fits in the Data Access Layer of any application. This method would simply return a SqlDataReader for clients living in a certain region.

The first step in developing a code snippet is to start by configuring the Header section of the new code snippet.

Create a new text file; rename the file to SqlDataReader.snippet. Then place the following text shown in Listing 2 in the snippet file.

<?xml version="1.0" encoding="utf-8" ?>

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

  <CodeSnippet Format="1.0.0">

    <Header>

      <Title>SqlDataReader</Title>

      <Shortcut>dr</Shortcut>

      <Description>Code snippet for a method to return a Sql data reader </Description>

      <Author>Bilal Haidar</Author>

      <SnippetTypes>

        <SnippetType>Expansion</SnippetType>

      </SnippetTypes>

    </Header>

The next step is to specify the replicable values inside that method. Let us state the replicable values I have come up with:

  • Method name: I will give the developer freedom to change the method name.
  • SqlConnection instance name: The developer can change the SqlConnection instance used.
  • ConnectionString Text: The developer can change the Connect string text.
  • SqlCommand instance name: The developer can change the SqlCommand instance used.
  • Query Text: The developer will change the SQL statement used to retrieve a Data Reader.
  • SqlDataReader instance name: The developer will change the SqlDataReader instance name used.

The above analysis resembles the following Declarations section which is part of the Snippet section. Each Literal defines the state of each replicable value in the emitted text by the code snippet. Listing 3 below shows the Declarations section.

    <Snippet>

      <Declarations>

        <Literal>

          <ID>MethodName</ID>

          <ToolTip>Method name</ToolTip>

          <Default>GetAllCustomers</Default>

        </Literal>

        <Literal>

          <ID>SqlConnectionInstance</ID>

          <ToolTip>SqlConnection Instance Name</ToolTip>

          <Default>conn</Default>

        </Literal>

        <Literal>

          <ID>ConnectionString</ID>

          <ToolTip>Connection String</ToolTip>

          <Default>ConnectionString</Default>

        </Literal>

        <Literal>

          <ID>SqlCommandInstance</ID>

          <ToolTip>SqlCommand Instance Name</ToolTip>

          <Default>cmd</Default>

        </Literal>

        <Literal>

          <ID>QueryText</ID>

          <ToolTip>The select statement to be executed </ToolTip>

          <Default>SELECT * FROM Customers</Default>

        </Literal>

        <Literal>

          <ID>SqlDataReaderInstance</ID>

          <ToolTip>SqlDataReader Instance Name</ToolTip>

          <Default>dr</Default>

        </Literal>

      </Declarations>

The Code section, which is a mandatory section in the Snippet section, is shown in Listing 4 below.

      <Code Language="csharp">

<![CDATA[

public static SqlDataReader $MethodName$()

{

  SqlConnection $SqlConnectionInstance$ = new SqlConnection("$ConnectionString$");

  SqlCommand $SqlCommandInstance$ = new SqlCommand("$QueryText$", $SqlConnectionInstance$);

  $SqlConnectionInstance$.Open();

  SqlDataReader $SqlDataReaderInstance$ = $SqlCommandInstance$.ExecuteReader(CommandBehavior.CloseConnection);

  return $SqlDataReaderInstance$;

}

$end$]]>

      </Code>

    </Snippet>

  </CodeSnippet>

</CodeSnippets>

The above is a simple method that instantiates a connection, command, and executes a data reader that is returned as the output of that method.

Now, copy the above sections of the code-snippet into the SqlDataReader.snippet file created in a previous step.

In Visual Studio 2005, go to the Code Snippet Manager and select Visual C# from the drop-down list of available languages. Click on Import, select the code snippet file created, and you will be faced by the following screen:

You can select the My Code Snippets checkbox or any other checkbox and click Finish.

If you add this code snippet to your page in the same way discussed earlier, you would see something similar to the following method in Listing 5 below:

public static SqlDataReader GetAllCustomers()

{

  SqlConnection conn = new SqlConnection("ConnectionString");

  SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", conn);

  conn.Open();

  SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

  return dr;

}

All the green highlighted values are the ones defined in the Declarations section in the code snippet file.

You can either change the default values or use your own.


View Entire Article

User Comments

Title: developer   
Name: sunil
Date: 2009-10-27 9:23:04 AM
Comment:
very nice exmpl
Title: developer   
Name: nanda
Date: 2007-08-06 2:00:31 AM
Comment:
nice article..
Title: Developer   
Name: Stefan Leoni
Date: 2006-12-13 7:18:51 AM
Comment:
concise. Excactly what i was looking for.

Thanks.
Title: Re:   
Name: Bilal Haidar
Date: 2006-01-20 3:38:15 PM
Comment:
You can now, discuss, comment, and ask me questions related to this article at this forum:

http://bhaidar.net/cs/forums/10/ShowForum.aspx

Regards
Title: Solution Developer   
Name: Mostafa
Date: 2006-01-07 3:05:53 AM
Comment:
Good Article.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 12:52:50 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search