/// <summary> /// Executes the script. /// </summary> /// <remarks> /// Use script.Execute(transaction) to do the work. We will also pull the /// status of our script exection from here. /// </remarks> /// <param name="scriptName">Name of the script.</param> /// <param name="transaction">The current transaction.</param> /// <param name="dbUserName">Name of the DB owner.</param> public static void ExecuteScript(string scriptName, SqlTransaction transaction, string dbUserName) { SqlScriptRunner scriptRunner = new SqlScriptRunner(UnpackEmbeddedScript(scriptName), Encoding.UTF8); if(!string.IsNullOrEmpty(dbUserName)) scriptRunner.TemplateParameters.SetValue("dbUser", dbUserName); scriptRunner.Execute(transaction); }
/// <summary> /// Executes create / alter scripts against the database /// </summary> /// <param name="SqlScript"></param> public void RunScript(string SqlScript) { // gets the server connection string connectionString = _database.Parent.ConnectionContext.ConnectionString; SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString); // specify the database builder.InitialCatalog = _database.Name; // remove comments Regex re = new Regex(@"\/\*.+\*\/", RegexOptions.Multiline); SqlScript = re.Replace(SqlScript, ""); // create a script runner (SubText) object SqlScriptRunner runner = new SqlScriptRunner(SqlScript); // connect to the database and run the scripts using (SqlConnection cn = new SqlConnection(builder.ConnectionString)) { cn.Open(); using (SqlTransaction transaction = cn.BeginTransaction()) { runner.Execute(transaction); transaction.Commit(); } } runner = null; }
public void RunTearDownFile() { SqlScriptRunner runner = new SqlScriptRunner(GetCommandText(TearDownFile)); using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); using (SqlTransaction trans = conn.BeginTransaction()) { runner.Execute(trans); trans.Commit(); } } }
/// <summary> /// Executes the script. /// </summary> /// <remarks> /// Use script.Execute(transaction) to do the work. We will also pull the /// status of our script exection from here. /// </remarks> /// <param name="scripts">The collection of scripts to execute.</param> /// <param name="transaction">The current transaction.</param> /// <param name="dbUserName">Name of the DB owner.</param> public static void ExecuteScript(ScriptCollection scripts, SqlTransaction transaction, string dbUserName) { SqlScriptRunner scriptRunner = new SqlScriptRunner(scripts); if (!string.IsNullOrEmpty(dbUserName)) scriptRunner.TemplateParameters.SetValue("dbUser", dbUserName); scriptRunner.Execute(transaction); }
/// <summary> /// Provides the import information as provided by the user back /// into the import provider. /// The control passed in should be the same as that provided in /// <see cref="GatherImportInformation"/>, but with user values /// supplied within it. /// </summary> /// <param name="populatedControl">Populated control.</param> public override void Import(Control populatedControl) { string dotTextConnectionString; GetConnectionStringsFromControl(populatedControl, out dotTextConnectionString); using(SqlConnection connection = new SqlConnection(Config.ConnectionString.ToString())) { connection.Open(); using(SqlTransaction transaction = connection.BeginTransaction()) { try { //Set up script parameters... ConnectionString subtextConnection = Config.ConnectionString; ConnectionString dotTextConnection = ConnectionString.Parse(dotTextConnectionString); Stream stream = ScriptHelper.UnpackEmbeddedScript("ImportDotText095.sql"); SqlScriptRunner runner = new SqlScriptRunner(stream, Encoding.UTF8); runner.TemplateParameters["subtext_db_name"].Value = subtextConnection.Database; runner.TemplateParameters["dottext_db_name"].Value = dotTextConnection.Database; if(!dotTextConnection.TrustedConnection) { // We need to determine if we should be using the username we got from // the dotTextConnection, or use the default template value (dbo). if (DoesTableExist("blog_Config", dotTextConnection.UserId, dotTextConnection)) { // we have the correct user for the path to the dotText tables runner.TemplateParameters["dotTextDbUser"].Value = dotTextConnection.UserId; } } runner.Execute(transaction); transaction.Commit(); } catch(Exception) { transaction.Rollback(); throw; } } } }