Repository navigation
Milestone
Description
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.
Metadata
Metadata
Assignees
Labels
No labels
Type
Projects
- StatusShow more project fieldsDone
Activity
priyankatiwari08 commentedon Mar 31, 2026
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.
campersau commentedon Mar 31, 2026
Version 7.0.0 but it was unused since its beginning in 5.1.0 d30d9d1