Exemplo n.º 1
0
 private void IdInsertOn(SqlCeTransaction localTrans, int idOrdinal)
 {
     if (_keepIdentity && idOrdinal >= 0)
     {
         using (var idCmd = AdoNetUtils.CreateCommand(_conn, localTrans, string.Format(CultureInfo.InvariantCulture, "SET IDENTITY_INSERT [{0}] ON", DestinationTableName)))
         {
             idCmd.ExecuteNonQuery();
         }
         if (_trans == null)
         {
             _autoIncNext = SqlCeBulkCopyTableHelpers.GetAutoIncNext(_conn, DestinationTableName);
         }
     }
 }
Exemplo n.º 2
0
        private void ResetSeed(int totalRows)
        {
            if (totalRows == 0)
            {
                return;
            }

            if (!_keepIdentity)
            {
                return;
            }

            //Cannot run re-seed when using user supplied transaction, so fail silently
            if (_keepIdentity && _trans != null)
            {
                return;
            }

            var newAutoIncNext = SqlCeBulkCopyTableHelpers.GetAutoIncNext(_conn, DestinationTableName);

            if (_autoIncNext != newAutoIncNext)
            {
                return;
            }

            using (var transact = _conn.BeginTransaction())
            {
                // Get Identity column
                string idCol = null;
                using (var ainCmd = AdoNetUtils.CreateCommand(_conn, transact, string.Format(CultureInfo.InvariantCulture,
                                                                                             "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{0}' AND AUTOINC_INCREMENT IS NOT NULL", DestinationTableName)))
                {
                    object res = ainCmd.ExecuteScalar();
                    if (res != null)
                    {
                        idCol = (string)res;
                    }
                }
                if (string.IsNullOrEmpty(idCol))
                {
                    return;
                }

                // Get Max value if the column
                long?maxVal = null;
                using (var ainCmd = AdoNetUtils.CreateCommand(_conn, transact, string.Format(CultureInfo.InvariantCulture,
                                                                                             "SELECT CAST(MAX([{0}]) AS bigint) FROM [{1}]", idCol, DestinationTableName)))
                {
                    object res = ainCmd.ExecuteScalar();
                    if (res != null)
                    {
                        maxVal = (long)res;
                    }
                }
                if (!maxVal.HasValue)
                {
                    return;
                }

                //Reseed
                using (var ainCmd = AdoNetUtils.CreateCommand(_conn, transact, string.Format(CultureInfo.InvariantCulture,
                                                                                             "ALTER TABLE [{0}] ALTER COLUMN [{1}] IDENTITY ({2},1);", DestinationTableName, idCol, maxVal + 1)))
                {
                    ainCmd.ExecuteNonQuery();
                }
                transact.Commit();
            }
        }