private void IdInsertOff(SqlCeTransaction localTrans, int idOrdinal) { if (_keepIdentity && idOrdinal >= 0) { using (var idCmd = AdoNetUtils.CreateCommand(_conn, localTrans, string.Format(CultureInfo.InvariantCulture, "SET IDENTITY_INSERT [{0}] OFF", DestinationTableName))) { idCmd.ExecuteNonQuery(); } } }
internal static long GetAutoIncNext(SqlCeConnection conn, string destinationTableName) { using (var ainCmd = AdoNetUtils.CreateCommand(conn, null, string.Format(CultureInfo.InvariantCulture, "SELECT AUTOINC_NEXT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{0}' AND AUTOINC_INCREMENT IS NOT NULL", destinationTableName))) { object res = ainCmd.ExecuteScalar(); if (res != null) { return((long)res); } return(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); } } }
internal static int IdentityOrdinal(SqlCeConnection conn, SqlCeTransaction transaction, string tableName) { int ordinal = -1; using (var ordCmd = AdoNetUtils.CreateCommand(conn, transaction, string.Format(CultureInfo.InvariantCulture, "SELECT ORDINAL_POSITION FROM information_schema.columns WHERE TABLE_NAME = N'{0}' AND AUTOINC_SEED IS NOT NULL", tableName))) { object val = ordCmd.ExecuteScalar(); if (val != null) { ordinal = (int)val - 1; } } return(ordinal); }
public static List <DestinationTableDefaultMetadata> GetDataForTable(SqlCeConnection conn, SqlCeTransaction transaction, string tableName) { var retVal = new List <DestinationTableDefaultMetadata>(); using (var ordCmd = AdoNetUtils.CreateCommand(conn, transaction, string.Format(CultureInfo.InvariantCulture, "SELECT Column_Name, Is_Nullable, Column_HasDefault FROM information_schema.columns WHERE TABLE_NAME = N'{0}' ORDER BY Ordinal_Position;", tableName))) { using (var val = ordCmd.ExecuteReader(CommandBehavior.SingleResult | CommandBehavior.SequentialAccess)) { while (val.Read()) { retVal.Add(new DestinationTableDefaultMetadata(val)); } } } return(retVal); }
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(); } }