diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBatch.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBatch.cs index 86d3e37094..7171bdbfe3 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBatch.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBatch.cs @@ -4,6 +4,7 @@ #if NET +using System; using System.Collections.Generic; using System.Data; using System.Data.Common; @@ -223,6 +224,10 @@ private void SetupBatchCommandExecute() { throw ADP.ConnectionRequired(nameof(SetupBatchCommandExecute)); } + if (_commands is null) + { + throw ADP.InvalidOperation(StringsHelper.GetString(Strings.ADP_NoSqlBatchCommandList)); + } _batchCommand.Connection = Connection; _batchCommand.Transaction = Transaction; _batchCommand.SetBatchRPCMode(true, _commands.Count); diff --git a/src/Microsoft.Data.SqlClient/src/Resources/Strings.Designer.cs b/src/Microsoft.Data.SqlClient/src/Resources/Strings.Designer.cs index 48cbfee2e6..7337cf5f42 100644 --- a/src/Microsoft.Data.SqlClient/src/Resources/Strings.Designer.cs +++ b/src/Microsoft.Data.SqlClient/src/Resources/Strings.Designer.cs @@ -1301,7 +1301,18 @@ internal static string ADP_NoQuoteChange { return ResourceManager.GetString("ADP_NoQuoteChange", resourceCulture); } } - + + /// + /// Looks up a localized string similar to SqlBatchCommand list has not been initialized.. + /// + internal static string ADP_NoSqlBatchCommandList + { + get + { + return ResourceManager.GetString("ADP_NoSqlBatchCommandList", resourceCulture); + } + } + /// /// Looks up a localized string similar to The stored procedure '{0}' doesn't exist.. /// diff --git a/src/Microsoft.Data.SqlClient/src/Resources/Strings.resx b/src/Microsoft.Data.SqlClient/src/Resources/Strings.resx index 675e2e55fb..1caf481f7c 100644 --- a/src/Microsoft.Data.SqlClient/src/Resources/Strings.resx +++ b/src/Microsoft.Data.SqlClient/src/Resources/Strings.resx @@ -4740,4 +4740,7 @@ Missing the DataColumn '{0}' for the SourceColumn '{2}'. + + SqlBatchCommand list has not been initialized. + \ No newline at end of file diff --git a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/Batch/BatchTests.cs b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/Batch/BatchTests.cs index b257b28c42..a883eb9b88 100644 --- a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/Batch/BatchTests.cs +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/Batch/BatchTests.cs @@ -25,6 +25,17 @@ public static void MissingCommandTextThrows() } } + [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))] + public static async Task MissingCommandsThrows() + { + using (var connection = new SqlConnection(DataTestUtility.TCPConnectionString)) + using (var batch = new SqlBatch { Connection = connection }) + { + connection.Open(); + await Assert.ThrowsAsync(() => batch.ExecuteReaderAsync()); + } + } + [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))] public static void MissingConnectionThrows() {