Skip to content

Commit e5cef80

Browse files
Fix/commands null (#3310)
* Return exception when a batch has empty commands list. * Add tests. * Add error message to Resources. * Fixing error in Unit Test. Removing the 's'.
1 parent 14bee54 commit e5cef80

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBatch.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#if NET
66

7+
using System;
78
using System.Collections.Generic;
89
using System.Data;
910
using System.Data.Common;
@@ -223,6 +224,10 @@ private void SetupBatchCommandExecute()
223224
{
224225
throw ADP.ConnectionRequired(nameof(SetupBatchCommandExecute));
225226
}
227+
if (_commands is null)
228+
{
229+
throw ADP.InvalidOperation(StringsHelper.GetString(Strings.ADP_NoSqlBatchCommandList));
230+
}
226231
_batchCommand.Connection = Connection;
227232
_batchCommand.Transaction = Transaction;
228233
_batchCommand.SetBatchRPCMode(true, _commands.Count);

src/Microsoft.Data.SqlClient/src/Resources/Strings.Designer.cs

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Microsoft.Data.SqlClient/src/Resources/Strings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4740,4 +4740,7 @@
47404740
<data name="ADP_ColumnSchemaMissing1" xml:space="preserve">
47414741
<value>Missing the DataColumn '{0}' for the SourceColumn '{2}'.</value>
47424742
</data>
4743+
<data name="ADP_NoSqlBatchCommandList" xml:space="preserve">
4744+
<value>SqlBatchCommand list has not been initialized.</value>
4745+
</data>
47434746
</root>

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ public static void MissingCommandTextThrows()
2525
}
2626
}
2727

28+
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
29+
public static async Task MissingCommandsThrows()
30+
{
31+
using (var connection = new SqlConnection(DataTestUtility.TCPConnectionString))
32+
using (var batch = new SqlBatch { Connection = connection })
33+
{
34+
connection.Open();
35+
await Assert.ThrowsAsync<InvalidOperationException>(() => batch.ExecuteReaderAsync());
36+
}
37+
}
38+
2839
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
2940
public static void MissingConnectionThrows()
3041
{

0 commit comments

Comments
 (0)