Tested with .Net 2.0.50215, Visual Studio 2005 Beta 2 8.50215.44
Code snippets are XML files that contain snippets of code you can insert into the VS.NET Code Editor workspace.
Code snippets are especially useful in situations where you frequently implement the same code blocks. You can save
a potentially great amount of time by using code snippets that contain frequently used code.
Code snippets are accessible by a variety of methods available in the VS.NET IDE (Integrated Devlopment
Environment). These are shown below.
Ctrl-K-X: Inserts the Code Snippet at the Current Line in the Code Editor
Ctrl-K-S: Surrounds the Current Code with the Code Snippet at the Current Location in the Code Editor
Shortcut: If the code snippet file contains a Shortcut element, you can access the snippet via the shortcut:
type the name of the Shortcut in the VS.NET code editor, or select it from the IntelliSense menu, at the point in the
code where you want the code snippet to appear; then press the [Tab] key.
Right-click: Right-click at the point in the code where you want to add the code snippet and then choose
either Insert Snippet... or Surround with....
The code snippet XML file follows the structure shown below:
The Beginning of the XML Code Snippet File:The Header:Select All Records from SQL Server DatabaseselallsqlTest Code snippet.C# .Net Fun - http://www.dotnetfun.com - Michael G.The Beginning of the Snippet:The Beginning of the Declarations:Literal Declarations:strConnSystem.StringYour SQL connection string."Data Source=HOSTNAME; User ID=userid; _
Password=password; Initial Catalog=Database;"strSQLSystem.StringYour SQL command string."SELECT * FROM MyTable;"TableNameSystem.StringThe name of the DataSet/database table."MyTable"Object Declarations:The End of the Declarations:The Code of the Snippet:The End of the Snippet:The End of the XML Code Snippet File:The IntelliSense Results (Ctrl-K-X):
try
{
DataSet ds = new DataSet();
using (SqlConnection sqlConn = new SqlConnection("Data Source=HOSTNAME; _
User ID=userid; Password=password; Initial Catalog=Database;"))
{
using (SqlDataAdapter sqlAdp = _
new SqlDataAdapter("SELECT * FROM MyTable;", sqlConn))
{
sqlAdp.fill(ds, "MyTable");
}
}
}
catch (SqlException e)
{
throw e;
}
catch (Exception e)
{
throw e;
}
Note: an underscore (_) character in this example signifies the code continues to the following line.
File naming convention: all code snippet files must be XML-compliant and should have the extension
.snippet.
Header: this area contains information about your snippet.
Title: contains the name of the snippet. The Title shows up in the list of snippets to chose from
when you issue a code snippet command in the VS.NET IDE.
Shortcut: contains a keyword that you can use to insert the code snippet by name and by pressing the
[Tab] key, rather than by selecting it from a menu (which is explained above).
Description: contains a ToolTip type description that appears when the input focus is placed on a
code snippet appearing in an IntelliSense menu.
Author: contains information about the author of the code snippet.
Literal Declaration: contains information about the literal variables used in the code snippet.
ID: The ID of a literal maps to the equivalent variable somewhere in the code of the code snippet.
The name of the ID is like the name of a variable. The value of the ID is in the Default element of the
Literal element (ID = Default). To use the Default value of the variable, you need to surround the name of the
ID with dollar signs. For example, if the ID is strSQL, you would use $strSQL$ in the code portion of the code
snippet. When the code snippet gets added to your code in the code editor, instead of the $strSQL$ appearing,
the Literal Default value for strSQL appears. If the Default element contained the literal
"SELECT * from MyTable" then whereever the $strSQL$ appears is where you would see this literal. So, think of
ID the name of a variable you can use in your code which holds the literl value set in the Default element.
Type: the fully qualified type name of the ID variable. For example, System.Data.SqlClient.ConnectionString.
ToolTip: contains a ToolTip type description that appears when the input focus is placed on a
code snippet appearing in an IntelliSense menu.
Default: contains the value of the ID variable. This is the value that actually shows up in the code of
the code snippet.
Object Declaration: contains information about the object variables used in the code snippet.
The properties of an Object declaration are the same as those for Literal declarations, which the exception of
course being that an Object declaration is for objects.
Code: contains the actual code of the code snippet.
Make sure that you surround your code with the CDATA element: <![CDATA[ ...code... ]]>.
Code Snippets Manager: Use this tool to add code snippets to folders or to add code snippet folders the
Snippet Inserter looks in when searching for code snippets to insert. You would use the Import... button
to add a code snippet to a selected folder. You would use the Add... button to add a folder that contains
code snippets to search.