How to send SSIS failure notification when executing from SQL Agent Job - c#

I have a SQL Agent Job with steps that resemble the following:
Exec SSIS package A
Exec SSIS package B
Exec SSIS package C
Exec stored proc
I have it set up so that if any of the SSIS packages fail, the job moves on to next step. The package failure notification comes from SSIS. I have 2 script tasks. One in event handler to capture/append error codes/messages to a variable:
The second script task is in control flow and grabs the appended error message variables and other email forming variables and sends them to an outlook email account:
This works when running the package with my credentials + just the individual package. When I try to run the package through the SQL Agent Job with default credentials (or even my own) I get the following errors:
The desired result is SQL Agent Job runs completely regardless of any of steps 1-3 failing. When any step 1-3 fail, within SSIS the failure email message is sent.
EDIT: SQL Agent Job
We are using the legacy Package Deployment Model. The Package lives on the file system.

Related

Failed to execute SSIS package in SQL Job

I have a simple SSIS package that only has 2 steps. Step 1 is a script task that will return the creation date of the last created file in the specific folder. And the second step, it will save the step 1 data into the database. Here is the complete code for step 1 in C#.
string vDir = Dts.Variables["UnitDirectory"].Value.ToString();
string vDBName = Dts.Variables["DatabaseName"].Value.ToString();
var directory = new DirectoryInfo(vDir);
var myFile = directory.GetFiles(vDBName + "*").OrderByDescending(f => f.LastWriteTime).First();
DateTime creation = File.GetCreationTime(vDir + myFile);
Dts.Variables["CreatedDate"].Value = creation;
Dts.TaskResult = (int)ScriptResults.Success;
In this case, i pass UnitDirectory variable with network sharing directory.
\\192.168.0.2\Database Backup\
The problem is, I can execute this package both from the Integration Service Catalog in SQL Server Management Studio and from Visual Studio. But, when I executed it as SQL Job, it just errors in step 1 and giving an unclear message.
Script Task:Error: Exception has been thrown by the target of an invocation.
I run SQL Job with my domain administrator credentials which have login access into network sharing directory. And this problem starts when I run in new installed SQL Server 2014. Previously this package has run normally in my old SQL Server.
Can you give me solution for this? I need your solution based on your experience. Thanks in advance.

Application Insights Database Dependency Command Invalid

I am using Application Insights on a web application and it is logging SQL dependencies, but the command listed is not a valid SQL command (AFAICT).
The WebApp is targeting .net 4.6.1 and I'm using Application Insights for Web version 2.1.0. It is collecting data points no problem, but the database dependencies are crazy.
The Command is shown as 'Server | Database'. Here is a copy paste of the Dependency Properties screen.
Dependency Properties
Event time 7/26/2016, 3:08:05 PM
Dependency type SQL
Successful call true
Result code <empty>
Duration 1.66 ms
Command
my-db-servername | my-db-name
Related Items
Request in which this dependency call was made 1
Failed or slow calls to remote dependencies 1
All available telemetry for this operation 2
All available telemetry 5 minutes before and after this event 507
When I try to apply this 'command' using SSMS > new query, it says it is invalid.
We are using Sitefinity for part of the application and it is making 100s to 1000s of calls to the database, each lasting < 1 ms (~500us) and listing the above command as the database command.
At this point I'm not sure whether it is an app insights thing, a sitefinity thing, or something else all together.
However, I'm really interested in why that command is listed as such.
Application Insights command is not a command text. It is concatenated from multiple parameters like this:
If you run stored procedures (4.6+ or (4.0+ StatusMonitor)): server name , pipe, database name, pipe, SPROC name.
If you use StatusMonitor and you run command text: server name , pipe, database name, pipe, command text
If you do not use StatusMontior but have 4.6+ and you run command text: server name , pipe, database name.
I assume your case is 3.

Programmatically create SQL Agent Job running SSIS package in C#

I have a SSIS package that is being used to simply transfer data from one database to another on the same server on a SQL Server Agent job. I want to be able to create multiple jobs for different databases at any time so I am trying to create a C# program to create the jobs.
Looking at the Job Step class I cannot find a way to set the type for the step like in the UI:
I've tried setting the command to the same form that an already existing job has for calling a SSIS package in the SSIS catalog:
JobStep jobStep = new JobStep(job, "Transfer");
jobStep.Command = #"/ISSERVER ""\SSISDB\ABC\ABC\Transfer.dtsx"" /SERVER FRISCHE /Par ""DestinationConnectionOLEDB_InitialCatalog"";""Dest_Test"" /Par ""SourceConnectionOLEDB_InitialCatalog"";""Src_Test"" /Par ""$ServerOption::LOGGING_LEVEL(Int16)"";1 /Par ""$ServerOption::SYNCHRONIZED(Boolean)"";True /CALLERINFO SQLAGENT /REPORTING E";
But the step that this creates always defaults to the Transact-SQL script option. Is there some way I can tell the JobStep object that I want a type of an SSIS package?
You're looking for the SubSytem. The Enum values are listed here
Ssis - Specifies the SQL Server Integration Services subsystem.
Specifically, you want Ssis. Code should be approximately
jobStep.SubSystem = AgentSubSystem.Ssis;

How to handle OLEDB Destination Error if the table is not exist the database SSIS

Help in ssis package i have an oledb destination this oledb destination you wiill set a connection to an existing table. The problem is I have a sql task that will check if the table is not exists in the database the task will create a table and will continue to the data flow task.
This is the problem I cannot run the package because if the table is not exist in the database the package will return an error.
Is there anyway that I can run the package and set an oledb destination without the table in the database?
Your package is failing because the first thing it does when running is validate everything. However if you set the delayed validation property on your Data Flow task to true, that task will only be checked just before it runs (when the table exists) not on package start up.

Synchronising databases SQL Server via C#

How do I synchronise a database on one server to the other? I need to do it via C#, not scripts. I was planning to use ADO.NET to retrieve the schema and dataset from one database, but how do I sync it in code?
Thanks
There are various options available to you:
SSIS to Export/Import data between System1 & System2
Mirroring to copy data between System1 & System2
Replication to keep System2 in sync with System1
Scripts for Database Backup/Restore between servers using C# to be the IO glue that scripts the backup on System1, copies the file to System2 and calls the restore script on System2
The easiest option to implement is #4 specially if changes occur to System1 that need to be replicated to System2 (tables, indexes, views, procedures, functions, triggers..etc..etc...)
See Database Mirroring in SQL Server 2005 # SQL Server Performance or Database Mirroring in SQL Server 2005 # Microsoft for mirroring information.
If you need some more enlightment on #4 just reply. Oh, it would help to specify what version of SQL server you are using. This information assumes >=2005 (Yukon, Katmai and possibly Killimanjaro)
Update: I would stay clear of trying to implement your own runtime on this as there are so many variations that just copying between 2 servers requires the ability to do diffs against the objects. Even using the SMO .NET objects this would be an ardous task that would require a lengthy development schedule.
Update 1: The poster is interested in the SSIS version so we will use those instructions.
Start SQL Server Management Studio
Navigate to the chosen database
Right click and Tasks->Export Data
Click Next
Select required source
Click Next
Select destination settings
Click Next
Select either use either tables or Write a query (we will assume use tables)
Click Next
Select the required tables
Click Edit Mappings
Ensure that enable identity insert is selected if required
Ensure Delete/Appends rows is selected as required
Ensure Drop and re-create destination table selected as required
Click Ok
Click Next
Now save this to an SSIS package either into SQL Server or on the filesystem
You can now use DTSExec to execute this package via a scheduler or use the .NET wrapper and call from your own C# runtime
C# Code example from Microsoft Website
using System;
using Microsoft.SqlServer.Dts.Runtime;
namespace RunFromClientAppCS
{
class Program
{
static void Main(string[] args)
{
string pkgLocation;
Package pkg;
Application app;
DTSExecResult pkgResults;
pkgLocation =
#"C:\Program Files\Microsoft SQL Server\100\Samples\Integration Services" +
#"\Package Samples\CalculatedColumns Sample\CalculatedColumns\CalculatedColumns.dtsx";
app = new Application();
pkg = app.LoadPackage(pkgLocation, null);
pkgResults = pkg.Execute();
Console.WriteLine(pkgResults.ToString());
Console.ReadKey();
}
}
}
Resources
dtexec Books Online (BOL)
SQL Server Import Export Wizard BOL
Visual Walkthru # http://www.accelebrate.com/sql_training/ssis_2008_tutorial.htm
Youtube Videos
Loading & Running SSIS Packages Programmatically
Why would you build this when there are db tools that do it for you?
Look into SSIS, or as it was previously known, DTS.

Categories