I’m trying to insert the contents of a DataTable verbatim into a SQL Server database using ADO.NET. (Note: I’m using SQL Server CE at the moment, but I’d like a solution that works in either regular or CE.)
Using the following code results in a command that does not include the Id column:
var builder = new SqlCeCommandBuilder();
builder.DataAdapter = adapter;
builder.SetAllValues = true;
var command = builder.GetInsertCommand(true);
This causes the Id to be auto-generated, which is not what I want. I need each Id to be copied exactly as it appears in the DataTable.
Is there any way to force SqlCeCommandBuilder to include the Id field in the insert command when using GetInsertCommand()? If not, is there another solution? I’m open to using ADO.NET or EntityFramework.
Note that the Id column is a primary key with AutoIncrement = true.
Edit
I came up with what I thought was a solution to this, but that ended up just creating another problem (see my answer below). So, I’m still looking for a good answer to this.
Edit 2
I just edited my answer. I think I found a workaround. Still clunky and database specific. Would prefer a proper ADO.NET or EntityFramework solution.
I spent a lot of time messing with
CommandBuilder, including manually inserting “Id” into the CommandText and adding an Id to the parameters, but I just couldn’t get it to work.So I decided to give up on that for the moment and just roll the insert queries myself. The code below seems to work for several example scenarios I threw at it, but I could be missing some conversions in
ItemToSqlFormattedValue().Comments/corrections much appreciated, and if you have a better solution, please post it as an answer.
Edit
I found a problem with this. Despite turning IDENTITY_INSERT back off, SQL Server CE can’t seem to figure out what the next ID is supposed to be, so it fails with the following error if you try to do an insert without specifying an Id value:
I guess SQL Server CE doesn’t just use
MAX(Id) + 1when figuring out what the next Id should be. I believe it tries to track it based on past inserts, but when inserts are performed with IDENTITY_INSERT turned on, this tracking doesn’t occur. That’s my hunch anyway.So, at the moment, this is really only a viable solution if the database is read-only or you always generate your own Id’s.
Edit 2
I think/hope this fixes it:
The key difference is that I’m now reseeding the Id column. That seemed to do the trick.