protected async override Task <Action <AsyncCodeActivityContext> > ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken) { var connString = ConnectionString.Get(context); SecureString connSecureString = null; var provName = ProviderName.Get(context); connSecureString = ConnectionSecureString.Get(context); DatabaseConnection existingConnection = null; existingConnection = ExistingDbConnection.Get(context); DatabaseConnection dbConnection = null; var continueOnError = ContinueOnError.Get(context); try { ConnectionHelper.ConnectionValidation(existingConnection, connSecureString, connString, provName); dbConnection = await Task.Run(() => existingConnection ?? new DatabaseConnection().Initialize(connString ?? new NetworkCredential("", connSecureString).Password, provName)); if (UseTransaction) { dbConnection.BeginTransaction(); } } catch (Exception e) { Trace.TraceError($"{e}"); HandleException(e, continueOnError); } return(asyncCodeActivityContext => { DatabaseConnection.Set(asyncCodeActivityContext, dbConnection); }); }
protected override void EndExecute(AsyncCodeActivityContext context, IAsyncResult result) { DatabaseConnection existingConnection = ExistingDbConnection.Get(context); try { Func <DataTable> action = (Func <DataTable>)context.UserState; DataTable dt = action.EndInvoke(result); if (dt == null) { return; } DataTable.Set(context, dt); } catch (Exception ex) { HandleException(ex, ContinueOnError.Get(context)); } finally { if (existingConnection == null) { DbConnection.Dispose(); } } }
protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state) { DataTable dataTable = null; string connString = null; string provName = null; string tableName = null; try { DbConnection = ExistingDbConnection.Get(context); connString = ConnectionString.Get(context); provName = ProviderName.Get(context); tableName = TableName.Get(context); dataTable = DataTable.Get(context); } catch (Exception ex) { HandleException(ex, ContinueOnError.Get(context)); } // create the action for doing the actual work Func <int> action = () => { DbConnection = DbConnection ?? new DatabaseConnection().Initialize(connString, provName); if (DbConnection == null) { return(0); } return(DbConnection.InsertDataTable(tableName, dataTable)); }; context.UserState = action; return(action.BeginInvoke(callback, state)); }
protected override void EndExecute(AsyncCodeActivityContext context, System.IAsyncResult result) { DatabaseConnection existingConnection = ExistingDbConnection.Get(context); try { Func <DBExecuteCommandResult> action = (Func <DBExecuteCommandResult>)context.UserState; DBExecuteCommandResult commandResult = action.EndInvoke(result); this.AffectedRecords.Set(context, commandResult.Result); foreach (var param in commandResult.ParametersBind) { var currentParam = Parameters[param.Key]; if (currentParam.Direction == ArgumentDirection.Out || currentParam.Direction == ArgumentDirection.InOut) { currentParam.Set(context, param.Value.Item1); } } } catch (Exception ex) { HandleException(ex, ContinueOnError.Get(context)); } finally { if (existingConnection == null) { DbConnection.Dispose(); } } }
protected async override Task <Action <AsyncCodeActivityContext> > ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken) { DataTable dataTable = null; string connString = null; SecureString connSecureString = null; string provName = null; string tableName = null; string[] columnNames = null; DatabaseConnection existingConnection = null; long affectedRecords = 0; IExecutorRuntime executorRuntime = null; var continueOnError = ContinueOnError.Get(context); try { existingConnection = DbConnection = ExistingDbConnection.Get(context); connString = ConnectionString.Get(context); provName = ProviderName.Get(context); tableName = TableName.Get(context); dataTable = DataTable.Get(context); columnNames = ColumnNames.Get(context); executorRuntime = context.GetExtension <IExecutorRuntime>(); connSecureString = ConnectionSecureString.Get(context); ConnectionHelper.ConnectionValidation(existingConnection, connSecureString, connString, provName); affectedRecords = await Task.Run(() => { DbConnection = DbConnection ?? new DatabaseConnection().Initialize(connString != null ? connString : new NetworkCredential("", connSecureString).Password, provName); if (DbConnection == null) { return(0); } if (executorRuntime != null && executorRuntime.HasFeature(ExecutorFeatureKeys.LogMessage)) { return(DbConnection.BulkUpdateDataTable(BulkUpdateFlag, tableName, dataTable, columnNames, executorRuntime)); } else { return(DbConnection.BulkUpdateDataTable(BulkUpdateFlag, tableName, dataTable, columnNames)); } }); } catch (Exception ex) { HandleException(ex, continueOnError); } finally { if (existingConnection == null) { DbConnection?.Dispose(); } } return(asyncCodeActivityContext => { AffectedRecords.Set(asyncCodeActivityContext, affectedRecords); }); }
protected override IAsyncResult BeginExecute(NativeActivityContext context, AsyncCallback callback, object state) { var connString = ConnectionString.Get(context); var provName = ProviderName.Get(context); var dbConnection = ExistingDbConnection.Get(context) ?? new DatabaseConnection().Initialize(connString, provName); ConnectionInitFunc = () => dbConnection; return(ConnectionInitFunc.BeginInvoke(callback, state)); }
protected override System.IAsyncResult BeginExecute(AsyncCodeActivityContext context, System.AsyncCallback callback, object state) { string connString = null; string provName = null; string sql = string.Empty; int commandTimeout = TimeoutMS.Get(context); if (commandTimeout < 0) { throw new ArgumentException(UiPath.Database.Activities.Properties.Resources.TimeoutMSException, "TimeoutMS"); } Dictionary <string, Tuple <object, ArgumentDirection> > parameters = null; try { sql = Sql.Get(context); DbConnection = ExistingDbConnection.Get(context); connString = ConnectionString.Get(context); provName = ProviderName.Get(context); if (Parameters != null) { parameters = new Dictionary <string, Tuple <object, ArgumentDirection> >(); foreach (var param in Parameters) { parameters.Add(param.Key, new Tuple <object, ArgumentDirection>(param.Value.Get(context), param.Value.Direction)); } } } catch (Exception ex) { HandleException(ex, ContinueOnError.Get(context)); } // create the action for doing the actual work Func <DBExecuteCommandResult> action = () => { DBExecuteCommandResult executeResult = new DBExecuteCommandResult(); if (DbConnection == null) { DbConnection = new DatabaseConnection().Initialize(connString, provName); } if (DbConnection == null) { return(executeResult); } executeResult = new DBExecuteCommandResult(DbConnection.Execute(sql, parameters, commandTimeout, CommandType), parameters); return(executeResult); }; context.UserState = action; return(action.BeginInvoke(callback, state)); }
protected async override Task <Action <AsyncCodeActivityContext> > ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken) { DataTable dataTable = null; string connString = null; SecureString connSecureString = null; string provName = null; string tableName = null; DatabaseConnection existingConnection = null; int affectedRecords = 0; var continueOnError = ContinueOnError.Get(context); try { existingConnection = DbConnection = ExistingDbConnection.Get(context); connString = ConnectionString.Get(context); provName = ProviderName.Get(context); tableName = TableName.Get(context); dataTable = DataTable.Get(context); connSecureString = ConnectionSecureString.Get(context); ConnectionHelper.ConnectionValidation(existingConnection, connSecureString, connString, provName); // create the action for doing the actual work affectedRecords = await Task.Run(() => { DbConnection = DbConnection ?? new DatabaseConnection().Initialize(connString != null ? connString : new NetworkCredential("", connSecureString).Password, provName); if (DbConnection == null) { return(0); } return(DbConnection.InsertDataTable(tableName, dataTable)); }); } catch (Exception ex) { HandleException(ex, continueOnError); } finally { if (existingConnection == null) { DbConnection?.Dispose(); } } return(asyncCodeActivityContext => { AffectedRecords.Set(asyncCodeActivityContext, affectedRecords); }); }
protected override void EndExecute(AsyncCodeActivityContext context, IAsyncResult result) { DatabaseConnection existingConnection = ExistingDbConnection.Get(context); try { Func <int> action = (Func <int>)context.UserState; int affectedRecords = action.EndInvoke(result); this.AffectedRecords.Set(context, affectedRecords); } catch (Exception ex) { HandleException(ex, ContinueOnError.Get(context)); } finally { if (existingConnection == null) { DbConnection.Dispose(); } } }
protected async override Task <Action <AsyncCodeActivityContext> > ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken) { string connString = null; SecureString connSecureString = null; string provName = null; string sql = string.Empty; int commandTimeout = TimeoutMS.Get(context); DatabaseConnection existingConnection = null; DBExecuteCommandResult affectedRecords = null; if (commandTimeout < 0) { throw new ArgumentException(Resources.TimeoutMSException, "TimeoutMS"); } Dictionary <string, Tuple <object, ArgumentDirection> > parameters = null; var continueOnError = ContinueOnError.Get(context); try { sql = Sql.Get(context); existingConnection = DbConnection = ExistingDbConnection.Get(context); connString = ConnectionString.Get(context); connSecureString = ConnectionSecureString.Get(context); provName = ProviderName.Get(context); if (Parameters != null) { parameters = new Dictionary <string, Tuple <object, ArgumentDirection> >(); foreach (var param in Parameters) { parameters.Add(param.Key, new Tuple <object, ArgumentDirection>(param.Value.Get(context), param.Value.Direction)); } } ConnectionHelper.ConnectionValidation(existingConnection, connSecureString, connString, provName); // create the action for doing the actual work affectedRecords = await Task.Run(() => { DBExecuteCommandResult executeResult = new DBExecuteCommandResult(); if (DbConnection == null) { DbConnection = new DatabaseConnection().Initialize(connString ?? new NetworkCredential("", connSecureString).Password, provName); } if (DbConnection == null) { return(executeResult); } executeResult = new DBExecuteCommandResult(DbConnection.Execute(sql, parameters, commandTimeout, CommandType), parameters); return(executeResult); }); } catch (Exception ex) { HandleException(ex, continueOnError); } finally { if (existingConnection == null) { DbConnection?.Dispose(); } } return(asyncCodeActivityContext => { AffectedRecords.Set(asyncCodeActivityContext, affectedRecords.Result); foreach (var param in affectedRecords.ParametersBind) { var currentParam = Parameters[param.Key]; if (currentParam.Direction == ArgumentDirection.Out || currentParam.Direction == ArgumentDirection.InOut) { currentParam.Set(asyncCodeActivityContext, param.Value.Item1); } } }); }