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.