Articles → .NET → Adding your own code snippet in visual studio 2008

Adding your own code snippet in visual studio 2008




Introduction







Creation of code snippet






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








<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>MyCode</Title>
		</Header>
	</CodeSnippet>
</CodeSnippets>






public DataSet GetResultSet(string query) {
	DataSet resultSet = new DataSet();
	using(OleDbConnection connection = new OleDbConnection("")) {
		connection.Open();
		using(OleDbDataAdapter adapter = new OleDbDataAdapter(query, connection)) {
			adapter.Fill(resultSet);
		}
	}
	return resultSet;
}




<Snippet>
    <Code Language="CSharp">
			<![CDATA[
				public DataSet GetResultSet(string query)
				{
					DataSet resultSet = new DataSet();
					using (OleDbConnection connection = new OleDbConnection(""))
					{
							connection.Open();
							using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, connection))
							{
									adapter.Fill(resultSet);
							}
					}
						return resultSet;
					}
			] ]>
		</Code>
</Snippet>






<CodeSnippets
	xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>MyCode</Title>
		</Header>
		<Snippet>
			<Code Language="CSharp">
				<![CDATA[
						public DataSet GetResultSet(string query)
						{
								DataSet resultSet = new DataSet();
								using (OleDbConnection connection = new OleDbConnection(""))
								{
										connection.Open();
										using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, connection))
										{
												adapter.Fill(resultSet);
										}
								}
								return resultSet;
						}
					] ]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>








Picture showing 'Code Snippets Manager..' option in tools menu

Click to Enlarge



Picture showing Code Snippets Manager pop up window

Click to Enlarge





Picture showing 'Snippet' folder in Code Snippets Manager pop up window

Click to Enlarge




Use code snippet in your project later




Picture showing how 'Snippet' folder is displayed when 'CTRL + K + X' is pressed

Click to Enlarge



Picture showing the 'MyCode' snippet

Click to Enlarge




Summary





Posted By  -  Karan Gupta
 
Posted On  -  Saturday, December 31, 2011

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250