I've written a program in VB (although I'm equally comfortable with C#, so please answer in either language) that is used to insert a large amount of data into a table in my SQL server DB.
The way I've planned on doing it is:
- To get the table's Schema from the DB using the
SqlDataAdapter.FillSchema
method. - Add the many rows of data to my DataTable
- Use
SqlBulkCopy
(.WriteToServer(dtToCopy.CreateDataReader)
) to load in the data
I've done this many times before and it's proven very efficient. My problem is that the table I'm writing to has an auto-incrementing primary key column, RowNum
.
Now I know I could re-query my table after getting the schema and get the latest value of RowNum
and simply write in the values myself into the corresponding column in my DataTable, but my fear is that the table may change between the time of my query and the time of my write, in which case, I could have un-desirable results.
I've seen posts relating to MissingSchemaAction
> AddWithKey
, but I'm not sure exactly how I could implement it in my case?
Would I just remove the primary key and delete the RowNum
column from my DataTable?
And, if so, at what point do I set that property? It doesn't seem to be a property of the DataTable and I don't see where to add it in the SqlBulkCopy.
Thanks!!!