Skip to content

Commit 63c4a0e

Browse files
committed
Support SqlBatchCommand.CommandBehavior
1 parent a11341f commit 63c4a0e

3 files changed

Lines changed: 59 additions & 10 deletions

File tree

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Batch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ internal void AddBatchCommand(SqlBatchCommand batchCommand)
3737
{
3838
// All batch sql statements must be executed inside sp_executesql, including those
3939
// without parameters
40-
BuildExecuteSql(CommandBehavior.Default, commandText, batchCommand.Parameters, ref rpc);
40+
BuildExecuteSql(batchCommand.CommandBehavior, commandText, batchCommand.Parameters, ref rpc);
4141
}
4242

4343
_RPCList.Add(rpc);

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Reader.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -463,13 +463,13 @@ private void BuildExecuteSql(
463463
SqlParameter sqlParam;
464464

465465
// @batch_text
466-
commandText ??= GetCommandText(behavior);
466+
string text = GetCommandText(behavior) + GetOptionsResetString(behavior);
467467
sqlParam = rpc.systemParams[0];
468-
sqlParam.SqlDbType = (commandText.Length << 1) <= TdsEnums.TYPE_SIZE_LIMIT
468+
sqlParam.SqlDbType = (text.Length << 1) <= TdsEnums.TYPE_SIZE_LIMIT
469469
? SqlDbType.NVarChar
470470
: SqlDbType.NText;
471-
sqlParam.Size = commandText.Length;
472-
sqlParam.Value = commandText;
471+
sqlParam.Size = text.Length;
472+
sqlParam.Value = text;
473473
sqlParam.Direction = ParameterDirection.Input;
474474

475475
// @batch_params

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/Batch/BatchTests.netcore.cs

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

@@ -15,6 +15,55 @@ namespace Microsoft.Data.SqlClient.ManualTesting.Tests
1515
{
1616
public static class BatchTests
1717
{
18+
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
19+
public static void SqlCommandGetColumnSchemaKeyInfo()
20+
{
21+
using (var connection = new SqlConnection(DataTestUtility.TCPConnectionString))
22+
{
23+
connection.Open();
24+
25+
using (var cmd = connection.CreateCommand())
26+
{
27+
cmd.CommandText = "SELECT * FROM Categories";
28+
29+
using var reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo);
30+
31+
Assert.False(reader.Read());
32+
33+
var schema = reader.GetColumnSchema();
34+
35+
Assert.Equal(4, schema.Count);
36+
Assert.True(schema[0].IsKey);
37+
}
38+
}
39+
}
40+
41+
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
42+
public static void SqlBatchCommandGetColumnSchemaKeyInfo()
43+
{
44+
using (var connection = new SqlConnection(DataTestUtility.TCPConnectionString))
45+
{
46+
connection.Open();
47+
48+
using (var batch = connection.CreateBatch())
49+
{
50+
var cmd = new SqlBatchCommand();
51+
cmd.CommandText = "SELECT * FROM Categories";
52+
cmd.CommandBehavior = CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo;
53+
54+
batch.BatchCommands.Add(cmd);
55+
56+
using var reader = batch.ExecuteReader();
57+
58+
Assert.False(reader.Read());
59+
60+
var schema = reader.GetColumnSchema();
61+
62+
Assert.Equal(4, schema.Count);
63+
Assert.True(schema[0].IsKey);
64+
}
65+
}
66+
}
1867

1968
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
2069
public static void MissingCommandTextThrows()
@@ -123,14 +172,14 @@ public static void MixedBatchSupported()
123172

124173
using (var connection = new SqlConnection(DataTestUtility.TCPConnectionString))
125174
using (var batch = new SqlBatch
126-
{
127-
Connection = connection,
128-
BatchCommands =
175+
{
176+
Connection = connection,
177+
BatchCommands =
129178
{
130179
new SqlBatchCommand("select @@SPID", CommandType.Text),
131180
new SqlBatchCommand("sp_help", CommandType.StoredProcedure, new List<SqlParameter> { new("@objname", "sys.indexes") })
132181
}
133-
})
182+
})
134183
{
135184
connection.RetryLogicProvider = prov;
136185
connection.Open();

0 commit comments

Comments
 (0)