public ExecuteNonQuery ( string sql, int timeout = 30, object>.IDictionary |
||
sql | string | |
timeout | int | |
parameters | object>.IDictionary | |
return | void |
public void ExecuteNonQuery(string sql, int timeout = 30, IDictionary<string, object> parameters = null)
{
parameters = parameters ?? new Dictionary<string, object>();
var regex = new Regex("^GO", RegexOptions.IgnoreCase | RegexOptions.Multiline);
var allScripts = regex.Split(sql);
var statements = allScripts.Where(x => !string.IsNullOrWhiteSpace(x));
HandleWithRetry(sql, s =>
{
using (var connection = _openConnection())
{
using (var transaction = connection.BeginTransaction())
{
using (var command = connection.CreateCommand())
{
command.Transaction = transaction;
command.CommandTimeout = timeout;
foreach (var statement in statements)
{
command.Parameters.Clear();
foreach (var parameter in parameters)
{
if (statement.Contains(parameter.Key))
{
command.Parameters.AddWithValue(parameter.Key, parameter.Value);
}
}
command.CommandText = statement;
command.CommandType = CommandType.Text;
command.ExecuteNonQuery();
}
transaction.Commit();
}
}
}
}, 0);
}
public void ApplyReleases(string releasesPath, string databaseName) { if (!Directory.Exists(releasesPath)) { return; } var scriptsDirectoryInfo = new DirectoryInfo(releasesPath); var scriptFiles = scriptsDirectoryInfo.GetFiles("*.sql", SearchOption.TopDirectoryOnly); var applicableScriptFiles = from f in scriptFiles let number = _schemaVersionInformation.ReleaseNumberOfFile(f) orderby number select new { file = f, number }; foreach (var scriptFile in applicableScriptFiles) { // What to catch and throw here?! var sql = File.ReadAllText(scriptFile.file.FullName); sql = ReplaceScriptTags(sql, databaseName); _executeSql.ExecuteNonQuery(sql, 10800); //try // { // } // catch (SqlException exception) // { //throw new NotExecutableScriptException(scriptFile.file.FullName, "scriptFile", exception); // } } }