|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Restoring DataColumn.AutoIncrement valueHi All,
I'm deriving the AutoIncrement, AutoIncrementSeed and AutoIncrementStep properties of DataColumn objects by inspecting the underlying properties of the source tables in Sql-Server. When I create and empty row of that table, ado.net generates a new identity value with the apropiate seed and step. But, if this new row is not finally saved on the database, I would like to restore the autoincrement value to its previous state. How achive this? Where are stored these generated vales? Thanks in advance, Mario Vazquez This is not much elegant, but it works:
public static void RestoreAutoIncrementValue(DataColumn autonumericColumn) { long step = autonumericColumn.AutoIncrementStep; DataTable table = autonumericColumn.Table; DataRow dtr; autonumericColumn.AutoIncrementStep = -(step); dtr = table.NewRow(); table.Rows.Add(dtr); table.Rows.Remove(dtr); autonumericColumn.AutoIncrementStep = step; } Show quote "Mario Vázquez" <algu***@microsoft.com> escribió en el mensaje news:eKSxiTXNHHA.1816@TK2MSFTNGP06.phx.gbl... > Hi All, > > I'm deriving the AutoIncrement, AutoIncrementSeed and AutoIncrementStep > properties of DataColumn objects by inspecting the underlying properties > of the source tables in Sql-Server. > When I create and empty row of that table, ado.net generates a new > identity value with the apropiate seed and step. > But, if this new row is not finally saved on the database, I would like to > restore the autoincrement value to its previous state. > How achive this? > Where are stored these generated vales? > > Thanks in advance, > Mario Vazquez > > I normally set my initial seed and step to -1, then if I roll back a row or
delete a new row the negative numbers do not confuse anyone (i.e. negative numbered rows are uncommitted new rows.) Once these rows are committed to the database, the identities are updated by the database. The reset autoincrement is only a viable solution when you are rolling off the last row in the list, and then when you are not asynchronous. Gaps shouldn't have an impact on client side data with new rows. Show quote "Mario Vázquez" <algu***@microsoft.com> wrote in message news:%23swNh5XNHHA.1280@TK2MSFTNGP04.phx.gbl... > This is not much elegant, but it works: > > public static void RestoreAutoIncrementValue(DataColumn autonumericColumn) > > { > > long step = autonumericColumn.AutoIncrementStep; > > DataTable table = autonumericColumn.Table; > > DataRow dtr; > > autonumericColumn.AutoIncrementStep = -(step); > > dtr = table.NewRow(); > > table.Rows.Add(dtr); > > table.Rows.Remove(dtr); > > autonumericColumn.AutoIncrementStep = step; > > } > > > "Mario Vázquez" <algu***@microsoft.com> escribió en el mensaje > news:eKSxiTXNHHA.1816@TK2MSFTNGP06.phx.gbl... >> Hi All, >> >> I'm deriving the AutoIncrement, AutoIncrementSeed and AutoIncrementStep >> properties of DataColumn objects by inspecting the underlying properties >> of the source tables in Sql-Server. >> When I create and empty row of that table, ado.net generates a new >> identity value with the apropiate seed and step. >> But, if this new row is not finally saved on the database, I would like >> to restore the autoincrement value to its previous state. >> How achive this? >> Where are stored these generated vales? >> >> Thanks in advance, >> Mario Vazquez >> >> > > |
|||||||||||||||||||||||