﻿<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Call the ExecuteReader Method of a SqlCommand</Title>
      <Author>Microsoft Corporation</Author>
      <Description>Fetches data from a database using the SqlCommand.ExecuteReader method of an existing data command.</Description>
      <Shortcut>adoExecReader</Shortcut>
    </Header>
    <Snippet>
      <References>
        <Reference>
          <Assembly>System.Data.dll</Assembly>
        </Reference>
        <Reference>
          <Assembly>System.Xml.dll</Assembly>
        </Reference>
      </References>
      <Imports>
        <Import>
          <Namespace>System.Data</Namespace>
        </Import>
        <Import>
          <Namespace>System.Data.SqlClient</Namespace>
        </Import>
      </Imports>
      <Declarations>
        <Literal>
          <ID>ProcessSprocResult</ID>
          <Type>String</Type>
          <ToolTip>Replace with your code to process the result of the DataReader.</ToolTip>
          <Default>Console.WriteLine(reader.GetValue(0))</Default>
        </Literal>
        <Object>
          <ID>conn</ID>
          <Type>SqlConnection</Type>
          <ToolTip>Replace with an existing connection object.</ToolTip>
          <Default>conn</Default>
        </Object>
        <Object>
          <ID>cmd</ID>
          <Type>SqlCommand</Type>
          <ToolTip>Replace with a SqlCommand to call ExecuteReader on.</ToolTip>
          <Default>cmd</Default>
        </Object>
      </Declarations>
      <Code Language="VB" Kind="method body"><![CDATA[Dim reader As SqlDataReader
Dim previousConnectionState As ConnectionState = $conn$.State
Try
If $conn$.State = ConnectionState.Closed Then
	$conn$.Open()
End If
reader = $cmd$.ExecuteReader()
Using reader
	While reader.Read
		' Process SprocResults datareader here.
		$ProcessSprocResult$
	End While
End Using
Finally
    If previousConnectionState = ConnectionState.Closed Then
        $conn$.Close()
    End If
End Try
]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>