Taal

SqlBatch.ExecuteReader Method

Definition

Overloads

Name Description
ExecuteReader()

Sends the Commands to the Connection and builds a SqlDataReader.

ExecuteReader(CommandBehavior)

Sends the Commands to the Connection and builds a SqlDataReader.

ExecuteReader()

Source:
SqlBatch.cs

Sends the Commands to the Connection and builds a SqlDataReader.

public:
 Microsoft::Data::SqlClient::SqlDataReader ^ ExecuteReader();
public Microsoft.Data.SqlClient.SqlDataReader ExecuteReader();
member this.ExecuteReader : unit -> Microsoft.Data.SqlClient.SqlDataReader
override this.ExecuteReader : unit -> Microsoft.Data.SqlClient.SqlDataReader
Public Function ExecuteReader () As SqlDataReader

Returns

Examples

The following example creates a SqlConnection and a SqlBatch, then adds multiple SqlBatchCommand objects to the batch. It then executes the batch, creating a SqlDataReader. The example reads through the results of the batch commands, writing them to the console. Finally, the example closes the SqlDataReader and then the SqlConnection as the using blocks fall out of scope.

using Microsoft.Data.SqlClient;

class Program
{
    static void Main()
    {
        string str = "Data Source=(local);Initial Catalog=Northwind;"
        + "Integrated Security=SSPI;Encrypt=False";
        RunBatch(str);
    }

    static void RunBatch(string connString)
    {
        using var connection = new SqlConnection(connString);
        connection.Open();

        using var batch = new SqlBatch(connection);

        const int count = 10;
        const string parameterName = "parameter";
        for (int i = 0; i < count; i++)
        {
            var batchCommand = new SqlBatchCommand($"SELECT @{parameterName} as value");
            batchCommand.Parameters.Add(new SqlParameter(parameterName, i));
            batch.BatchCommands.Add(batchCommand);
        }

        var results = new List<int>(count);
        using (SqlDataReader reader = batch.ExecuteReader())
        {
            do
            {
                while (reader.Read())
                {
                    results.Add(reader.GetFieldValue<int>(0));
                }
            } while (reader.NextResult());
        }
        Console.WriteLine(string.Join(", ", results));
    }
}

The following example creates a SqlConnection and a SqlBatch, then adds multiple SqlBatchCommand objects to the batch. It then executes the batch, creating a SqlDataReader. The example reads through the results of the batch commands, writing them to the console. Finally, the example closes the SqlDataReader and then the SqlConnection as the using blocks fall out of scope.

using Microsoft.Data.SqlClient;

class Program
{
    static void Main()
    {
        string str = "Data Source=(local);Initial Catalog=Northwind;"
                   + "Integrated Security=SSPI;Encrypt=False";
        RunBatch(str);
    }

    static void RunBatch(string connString)
    {
        using var connection = new SqlConnection(connString);
        connection.Open();

        using var batch = new SqlBatch(connection);

        const int count = 10;
        const string parameterName = "parameter";
        for (int i = 0; i < count; i++)
        {
            var batchCommand = new SqlBatchCommand($"SELECT @{parameterName} as value");
            batchCommand.Parameters.Add(new SqlParameter(parameterName, i));
            batch.BatchCommands.Add(batchCommand);
        }

        var results = new List<int>(count);
        using (SqlDataReader reader = batch.ExecuteReader())
        {
            do
            {
                while (reader.Read())
                {
                    results.Add(reader.GetFieldValue<int>(0));
                }
            } while (reader.NextResult());
        }
        Console.WriteLine(string.Join(", ", results));
    }
}

Applies to

ExecuteReader(CommandBehavior)

Sends the Commands to the Connection and builds a SqlDataReader.

public:
 Microsoft::Data::SqlClient::SqlDataReader ^ ExecuteReader(System::Data::CommandBehavior behavior);
public Microsoft.Data.SqlClient.SqlDataReader ExecuteReader(System.Data.CommandBehavior behavior);
member this.ExecuteReader : System.Data.CommandBehavior -> Microsoft.Data.SqlClient.SqlDataReader
override this.ExecuteReader : System.Data.CommandBehavior -> Microsoft.Data.SqlClient.SqlDataReader
Public Function ExecuteReader (behavior As CommandBehavior) As SqlDataReader

Parameters

behavior
CommandBehavior

An instance of CommandBehavior, specifying options for batch execution and data retrieval. Only SequentialAccess and CloseConnection are supported at the batch level.

Returns

Examples

The following example creates a SqlConnection and a SqlBatch, then adds multiple SqlBatchCommand objects to the batch. It then executes the batch, creating a SqlDataReader. The example reads through the results of the batch commands, writing them to the console. Finally, the example closes the SqlDataReader and then the SqlConnection as the using blocks fall out of scope.

using Microsoft.Data.SqlClient;

class Program
{
    static void Main()
    {
        string str = "Data Source=(local);Initial Catalog=Northwind;"
                   + "Integrated Security=SSPI;Encrypt=False";
        RunBatch(str);
    }

    static void RunBatch(string connString)
    {
        using var connection = new SqlConnection(connString);
        connection.Open();

        using var batch = new SqlBatch(connection);

        const int count = 10;
        const string parameterName = "parameter";
        for (int i = 0; i < count; i++)
        {
            var batchCommand = new SqlBatchCommand($"SELECT @{parameterName} as value");
            batchCommand.Parameters.Add(new SqlParameter(parameterName, i));
            batch.BatchCommands.Add(batchCommand);
        }

        var results = new List<int>(count);
        using (SqlDataReader reader = batch.ExecuteReader(CommandBehavior.CloseConnection))
        {
            do
            {
                while (reader.Read())
                {
                    results.Add(reader.GetFieldValue<int>(0));
                }
            } while (reader.NextResult());
        }
        Console.WriteLine(string.Join(", ", results));
    }
}

Applies to