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" ?>
<Description>Code snippet for a method to return a Sql data reader </Description>
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.
<ToolTip>SqlConnection Instance Name</ToolTip>
<ToolTip>SqlCommand Instance Name</ToolTip>
<ToolTip>The select statement to be executed </ToolTip>
<Default>SELECT * FROM Customers</Default>
<ToolTip>SqlDataReader Instance Name</ToolTip>
The Code section, which is a mandatory section in the Snippet section, is shown in Listing 4 below.
public static SqlDataReader $MethodName$()
SqlConnection $SqlConnectionInstance$ = new SqlConnection("$ConnectionString$");
SqlCommand $SqlCommandInstance$ = new SqlCommand("$QueryText$", $SqlConnectionInstance$);
SqlDataReader $SqlDataReaderInstance$ = $SqlCommandInstance$.ExecuteReader(CommandBehavior.CloseConnection);
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);
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
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.