Skip to content

Instantly share code, notes, and snippets.

@follesoe
Created December 24, 2010 14:33
Show Gist options
  • Select an option

  • Save follesoe/754284 to your computer and use it in GitHub Desktop.

Select an option

Save follesoe/754284 to your computer and use it in GitHub Desktop.
Re-discovering code snippets
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Execute Sql Data Reader</Title>
<Author>Jonas Follesø</Author>
<Description>A standard way to execute a stored procedure returning a reader</Description>
<HelpUrl>http://jonas.follesoe.no</HelpUrl>
<Shortcut>reader</Shortcut>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>spname</ID>
<ToolTip>Name of stored procedure</ToolTip>
<Default>mySproc</Default>
</Literal>
<Literal>
<ID>connectionName</ID>
<ToolTip>Name of the SQL connection in the configuration file</ToolTip>
<Default>myConnection</Default>
</Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["$connectionName$"].ConnectionString))
{
SqlCommand command = new SqlCommand("$spname$", connection);
command.CommandType = CommandType.StoredProcedure;
using (command)
{
$end$
command.Connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
}
reader.Close();
}
command.Connection.Close();
}
}]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment