question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Data Adapter Batch using Insert command fails on Core but passes on Net 4.6.2

See original GitHub issue

Describe 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:closed
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
suthramscommented, Jul 11, 2019

@cheenamalhotra i got the latest preview published 3 days ago microsoft.data.sqlclient.1.0.19189.1-preview.nupkg and now my tests pass.

0reactions
cheenamalhotracommented, Jul 11, 2019

Thanks for confirming! We’ll close the issue now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SqlDataAdapter Class (System.Data.SqlClient)
Represents a set of data commands and a database connection that are used to fill the DataSet and update a SQL Server database....
Read more >
MySQL Connector/NET Release Notes
Data can be passed in and out of a MySQL stored procedure through the ... When Connector/NET inserted multiple rows using a single...
Read more >
Spring Batch - Reference Documentation
The application contains all batch jobs and custom code written by developers using Spring Batch. The Batch Core contains the core runtime classes...
Read more >
ADO.NET Batch Insert with over 2000 parameters
Batch inserts for every 100 records. Do each insert as it's own command with a transaction wrapped around all inserts. Use SQL Bulk...
Read more >
JDBC Developer's Guide and Reference
These calls communicate with the database using Oracle Net Services. The JDBC OCI driver uses the OCI libraries, C-entry points, Oracle Net, core...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found