Search This Blog

Friday, January 3, 2014

Create a snippet in visual studio




To create snippet  add a xml file into project and the add the following code.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <Title>Insert</Title>
    <Author>Bhagwati</Author>
    <Shortcut>insert</Shortcut>
    <Description>description</Description>
    <SnippetTypes>
      <SnippetType>SurroundsWith</SnippetType>
      <SnippetType>Expansion</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Declarations>
      <Literal>
        <ID>conStraing</ID>
        <Default>YouConnectionString</Default>
        <ToolTip>Connection String</ToolTip>
      </Literal>
      <Literal>
        <ID>sqlCommand</ID>
        <Default>Query</Default>
        <ToolTip>YourQuery</ToolTip>
      </Literal>
    </Declarations>
    <Code Language="CSharp">
      <![CDATA[SqlConnection con=null;
      SqlCommand cmd=null;
      try
      {
        con=new SqlConnection($conStraing$);
        con.Open();
        cmd=new SqlCommand($sqlCommand$,con);
        cmd.ExecuteNonQuery();
      }
      catch(Exception ex)
      {
     
      }
      finally
      {
        if(con.State!=ConnectionState.Close)
        {
          con.close();
        }
      }
      ]]>
    </Code>
  </Snippet>
</CodeSnippet>

Change the file extension .xml to .snippet.
Then we need to register this snippet file into snippet manager, for that open snippet manager in tool menu, then click on import select your file that’s done.

No comments:

Post a Comment