U
UncleJoe
Hi all. I am trying to resolve this seemingly very simple and not to
mention well documented problem but just couldn't seem to get it to
work. I have a datatable that uses an identity column as the primary
key. I need to get the value of the identity column back after
performing an insert to the SQL Server 2005 (beta) database. From all
the information that I have gathered, the following code should work:
1 SqlConnection conn = new SqlConnection(connStr);
2 conn.Open();
3 SqlCommand cmd = new SqlCommand(string.Format("SELECT * FROM {0}",
dt.TableName), conn);
4 SqlDataAdapter da = new SqlDataAdapter(cmd);
5 SqlCommandBuilder CB = new SqlCommandBuilder(da);
6 da.InsertCommand = CB.GetInsertCommand();
7 da.InsertCommand.CommandText += string.Format("; SELECT * FROM {0}
WHERE {1} = @@IDENTITY", dt.TableName, "PKeyID");
8 da.InsertCommand.UpdatedRowSource = UpdateRowSource.Both;
9 da.Update(dt);
10 dt.AcceptChanges();
* dt is the database containing the rows that need to be inserted.
* I had the FillSchema operated on dt so the identity column's
AutoIncrement property is set to true.
* I set the AutoIncrementSeed and AutoIncrementStep properties of the
identity column set to -1.
* I have already tried setting the UpdatedRowSource property to "Both"
and "FirstReturnedRecord".
But after running the above code, the value of the identity column
remains -1. What am I missing???
In addition, if I need to wrap the update within a transaction, should
I invoke the AcceptChanges call inside or outside the transaction? Will
the row state reverse along with a rollback call?
Any help will be greatly appreciated.
mention well documented problem but just couldn't seem to get it to
work. I have a datatable that uses an identity column as the primary
key. I need to get the value of the identity column back after
performing an insert to the SQL Server 2005 (beta) database. From all
the information that I have gathered, the following code should work:
1 SqlConnection conn = new SqlConnection(connStr);
2 conn.Open();
3 SqlCommand cmd = new SqlCommand(string.Format("SELECT * FROM {0}",
dt.TableName), conn);
4 SqlDataAdapter da = new SqlDataAdapter(cmd);
5 SqlCommandBuilder CB = new SqlCommandBuilder(da);
6 da.InsertCommand = CB.GetInsertCommand();
7 da.InsertCommand.CommandText += string.Format("; SELECT * FROM {0}
WHERE {1} = @@IDENTITY", dt.TableName, "PKeyID");
8 da.InsertCommand.UpdatedRowSource = UpdateRowSource.Both;
9 da.Update(dt);
10 dt.AcceptChanges();
* dt is the database containing the rows that need to be inserted.
* I had the FillSchema operated on dt so the identity column's
AutoIncrement property is set to true.
* I set the AutoIncrementSeed and AutoIncrementStep properties of the
identity column set to -1.
* I have already tried setting the UpdatedRowSource property to "Both"
and "FirstReturnedRecord".
But after running the above code, the value of the identity column
remains -1. What am I missing???
In addition, if I need to wrap the update within a transaction, should
I invoke the AcceptChanges call inside or outside the transaction? Will
the row state reverse along with a rollback call?
Any help will be greatly appreciated.