using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Text; namespace Concurrency { public class TemporaryConstraint : IDisposable { private readonly SqlCommand _command; private readonly string _constraintName; public TemporaryConstraint(SqlConnection connection, string schemaName, string tableName, string constraintBody) { _command = connection.CreateCommand(); _constraintName = string.Format("[{0}_{1}_TemporaryConstraint]", schemaName, tableName); try { _command.CommandText = string.Format( "ALTER TABLE {0}.{1} DROP CONSTRAINT {2}", schemaName, tableName, _constraintName); _command.ExecuteNonQuery(); } catch(Exception e) { //Console.WriteLine(e); } _command.CommandText = string.Format("ALTER TABLE {0}.{1} WITH NOCHECK ADD CONSTRAINT {2} CHECK({3})", schemaName, tableName, _constraintName, constraintBody); _command.ExecuteNonQuery(); _command.CommandText = string.Format("ALTER TABLE {0}.{1} DROP CONSTRAINT {2}", schemaName, tableName, _constraintName); } public void Dispose() { _command.ExecuteNonQuery(); } } }