Data Adapter Batch using Insert command fails on Core but passes on Net 4.6.2
See original GitHub issueDescribe the bug
A clear and concise description of what the bug is. Data Adapter Update batch operation is throwing this exception below on Core 2.1 using the 4.7 Preview 6 package of System.Data.SqlClient.
System.Data.SqlClient.SqlException: Implicit conversion from data type nvarchar to varbinary is not allowed. Use the CONVERT function to run this query.
However the same batch adapter operation passes on Net 4.6.2 framework If you are seeing an exception, include the full exceptions details (message and stack trace).
Exception message:
Stack trace:
at System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
at LandisGyr.Data.DBTools.FlushUsingAdapters(String databaseKey, DataTable tbl, DbConnection con, DbCommand cmd, DbTransaction tran, Int32 commandTimeOut, Int32 adapterBatchSize) in D:\Git\NuGet_LGData\LandisGyr.Data\Foundation\DBTools.cs:line 1691
at DAL_Generator_Test.Insnetworkdatabatch2DataSet.Flush(DbConnection con, DbTransaction tran, Int32 commandTimeOut) in D:\Git\NuGet_LGDataSqlServer\Tests\LG.Data.SqlServer.Tests\GeneratedDAL\Insnetworkdatabatch2.generated.cs:line 450
at LandisGyr.Data.BaseDataBatch.FlushStoreNewTran(IDbDataStore store, DbConnection connection, Int32 timeout, Int32 originalTimeOut) in D:\Git\NuGet_LGData\LandisGyr.Data\Foundation\BaseDataBatch.cs:line 370
at LandisGyr.Data.BaseDataBatch.FlushDataSeperateTrans() in D:\Git\NuGet_LGData\LandisGyr.Data\Foundation\BaseDataBatch.cs:line 328
at LandisGyr.Data.BaseDataBatch.FlushData() in D:\Git\NuGet_LGData\LandisGyr.Data\Foundation\BaseDataBatch.cs:line 445
at DAL_Generator_Test.SqlServerTests.TestBasicConnectivity.TestReturnTypedDataTable() in D:\Git\NuGet_LGDataSqlServer\Tests\LG.Data.SqlServer.Tests\TestBasicConnectivity.cs:line 566
### To reproduce
1) Create table using script below
CREATE TABLE [dbo].[StgNetworkStats2](
[insertedDate] [datetime] NOT NULL,
[endPointId] [int] NOT NULL,
[recordTypeID] [int] NOT NULL,
[recordValue] [bigint] NOT NULL,
[packetSize] [int] NOT NULL,
[userComments] [varchar](255) NULL,
[packetContents] [varbinary](255) NULL,
CONSTRAINT [PKC_StgNetworkStats2_DtSnRt] PRIMARY KEY CLUSTERED
(
[insertedDate] ASC,
[endPointId] ASC,
[recordTypeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
2) Write Stored Procedure
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'Insnetworkdatabatch2')
BEGIN
PRINT 'Dropping Procedure Insnetworkdatabatch2'
DROP Procedure Insnetworkdatabatch2
END
GO
PRINT 'Creating Procedure Insnetworkdatabatch2'
GO
CREATE PROCEDURE [dbo].[Insnetworkdatabatch2]
@endPointId INT,
@insertedDateTime DATETIME,
@recordTypeID INT,
@recordValue BIGINT,
@packetSize INT,
@userComments varchar(255) null,
@packetContents varbinary(255) null
AS
SET nocount ON
SET xact_abort ON
BEGIN try
INSERT stgnetworkstats2
(inserteddate,
endpointid,
recordtypeid,
recordvalue,
packetsize,
userComments,
packetContents)
VALUES ( @insertedDateTime,
@endPointID,
@recordTypeID,
@recordValue,
@packetSize,
@userComments,
@packetContents )
END try
BEGIN catch
IF Error_number() = 2627 -- Violates Primary Key
RETURN 0
END catch
GO
3) Call the stored procedure in a batch using data adapters
and verify there is no exception in the insert and the records are inserted.
Here is our app code
DBTools.ExecuteSql(key, "TRUNCATE TABLE StgNetworkStats2");
Insnetworkdatabatch2DataBatchFactory factory = new Insnetworkdatabatch2DataBatchFactory();
var batch = factory.GetBatch(key, 100);
for (int i = 0; i < BULK_OP_RECORDS_SIZE; i++)
{
var record = batch.NewInsnetworkdatabatch2Record();
record.EndPointId = i;
record.InsertedDateTime = DateTime.UtcNow;
record.RecordValue = i;
record.RecordTypeID = 1;
record.PacketSize = 100;
record.UserComments = i % 2 == 0 ? $"comment {i}" : null;
record.PacketContents = i % 2 == 0 ? Encoding.ASCII.GetBytes("comment {i}") : null;
batch.AddInsnetworkdatabatch2Record(record);
}
batch.FlushData();
Here is our underlying data access code.. sample
public static void FlushUsingAdapters(string databaseKey, DataTable tbl, DbConnection con, DbCommand cmd, DbTransaction tran, int commandTimeOut, int adapterBatchSize)
{
using (DbDataAdapter adapter = DatabaseHelper.GetDbDataAdapter(databaseKey))
{
adapter.UpdateBatchSize = adapterBatchSize;
adapter.InsertCommand = cmd;
adapter.InsertCommand.Connection = con;
adapter.InsertCommand.CommandTimeout = commandTimeOut;
adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None;
adapter.InsertCommand.Transaction = tran;
adapter.Update(tbl);
}
}
```c#
Console.WriteLine("Hello World!");
Expected behavior
A clear and concise description of what you expected to happen.
Further technical details
Microsoft.Data.SqlClient version: (found on the nuget or Microsoft.Data.SqlClient.dll) .NET target: (e.g. Framework 4.7.1, Core 2.2.2) SQL Server version: (e.g. SQL Server 2017) Operating system: (e.g. Windows 2019, Ubuntu 18.04, macOS 10.13, Docker container)
Additional context Add any other context about the problem here.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (2 by maintainers)
@cheenamalhotra i got the latest preview published 3 days ago microsoft.data.sqlclient.1.0.19189.1-preview.nupkg and now my tests pass.
Thanks for confirming! We’ll close the issue now.