Tom Archer said:
I am a little confused about the "negative autonumber workaround". Why
would I need to do this if the SqlCommandBuilder is ignoring my values
anyways and SQL Server is automatically generating the correct value?
Because your DataTable may have a primary key on the autonumber column, or a
foreign-key relationship refrencing the autonumber column. If so the
DataSet will not let you insert a row with a null or duplicate value in the
field. You must have some value, but since you're disconnected, and the
database will eventually assign the value, you can't assign the final value.
Thus the temporary fake autonumbers.
Also when inspecting the DataTable (Northwind->Employees), the
EmployeeID column states that it's not AutoInc ?! Yet I can pass any
value in the DataRow item array (including leaving it blank) and SQL
Server will auto inc this column.
If you want to use the "negative autonumber workaround", you must set the
column to AutoIncrement yourself, either in the DataSet designer or in code.
If not, then you can just put any old value in the column.
Whatever value you stick in for EmployeeID, the SQLCommandBuilder still
recognizes that EmployeeID is an IDENTITY column and omits it from the
insert statement (check SQLCommandBuilder.InsertCommand.CommandText).
The values only need to be non-null and unique when using DataTable primary
keys or in a DataSet containing multiple related DataTables. For instance
if you added Orders to your DataSet along with Employees, and wanted to
insert a new Employee and enter some Orders for that employee, you would
need to create a temporary EmployeeID and use that in Orders.EmployeeID to
relate the rows. Then after flushing the Employees table back to the
database, you would need to retrieve the permanent EmployeeID and update the
Orders DataTable before flushing it back to the Database. Setting
Employees.EmployeeID to Autoincrement and cascading updates along the
relationship just help to automate this process.
David