Skip to content

SqlBatchCommand.CommandBehavior is unused #4083

Closed

Description

@campersau

Describe the bug

The SqlBatchCommand.CommandBehavior is completly unused which took me several hours to realize.
Here is an example, both tests should pass, only the first one with SqlCommand does.

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static void SqlCommandGetColumnSchemaKeyInfo()
{
    using (var connection = new SqlConnection(DataTestUtility.TCPConnectionString))
    {
        connection.Open();

        using (var cmd = connection.CreateCommand())
        {
            cmd.CommandText = "SELECT * FROM Categories";

            using var reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo);

            Assert.False(reader.Read());

            var schema = reader.GetColumnSchema();

            Assert.Equal(4, schema.Count);
            Assert.True(schema[0].IsKey);
        }
    }
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static void SqlBatchCommandGetColumnSchemaKeyInfo()
{
    using (var connection = new SqlConnection(DataTestUtility.TCPConnectionString))
    {
        connection.Open();

        using (var batch = connection.CreateBatch())
        {
            var cmd = new SqlBatchCommand();
            cmd.CommandText = "SELECT * FROM Categories";
            cmd.CommandBehavior = CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo;

            batch.BatchCommands.Add(cmd);

            using var reader = batch.ExecuteReader();

            Assert.False(reader.Read());

            var schema = reader.GetColumnSchema();

            Assert.Equal(4, schema.Count);
            Assert.True(schema[0].IsKey);
        }
    }
}

Expected behavior

Both tests should pass.

Further technical details

Microsoft.Data.SqlClient version: 7.0.0
.NET target: .NET 10
SQL Server version: SQL Server 2022
Operating system: Windows 11

Additional context

I have attemted a fix here: https://github.com/dotnet/SqlClient/compare/main...campersau:GetColumnSchemaKeyInfo?expand=1
Please let me know if I can submit it as a PR.

Activity

  1. priyankatiwari08 commented on Mar 31, 2026

    @priyankatiwari08
    Contributor

    Thanks @campersau for the detailed report and for already putting together a fix!

    Could you clarify which released version of Microsoft.Data.SqlClient (NuGet package) you observed this against? The version field in the issue template is left as placeholder text.

    Additionally, please go ahead and submit your fix as a PR so we can review it.

  2. moved this from To triage to Waiting for customer in SqlClient Boardon Mar 31, 2026
  3. campersau commented on Mar 31, 2026

    @campersau
    ContributorAuthor

    Version 7.0.0 but it was unused since its beginning in 5.1.0 d30d9d1

  4. moved this from Waiting for customer to In progress in SqlClient Boardon Apr 1, 2026
  5. added this to the 7.1.0-preview1 milestone on Apr 1, 2026
  6. moved this from In progress to Done in SqlClient Boardon Jun 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions