|
| 1 | +--- |
| 2 | +title: "Deploy an SSIS project with .NET code (C#) | Microsoft Docs" |
| 3 | +ms.date: "09/25/2017" |
| 4 | +ms.topic: "article" |
| 5 | +ms.prod: "sql-server-2017" |
| 6 | +ms.technology: |
| 7 | + - "integration-services" |
| 8 | +author: "douglaslMS" |
| 9 | +ms.author: "douglasl" |
| 10 | +manager: "craigg" |
| 11 | +--- |
| 12 | +# Deploy an SSIS project with C# code in a .NET app |
| 13 | +This quick start tutorial demonstrates how to write C# code to connect to a database server and deploy an SSIS project. |
| 14 | + |
| 15 | +To create a C# app, you can use Visual Studio, Visual Studio Code, or another tool of your choice. |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +Before you start, make sure you have Visual Studio or Visual Studio Code installed. Download the free Community edition of Visual Studio, or the free Visual Studio Code, from [Visual Studio Downloads](https://www.visualstudio.com/downloads/). |
| 20 | + |
| 21 | +> [!NOTE] |
| 22 | +> An Azure SQL Database server listens on port 1433. If you're trying to connect to an Azure SQL Database server from within a corporate firewall, this port must be open in the corporate firewall for you to connect successfully. |
| 23 | +
|
| 24 | +## Get the connection info if deployed to SQL Database |
| 25 | + |
| 26 | +If your packages are deployed to an Azure SQL Database, get the connection information you need to connect to the SSIS Catalog database (SSISDB). You need the fully qualified server name and login information in the procedures that follow. |
| 27 | + |
| 28 | +1. Log in to the [Azure portal](https://portal.azure.com/). |
| 29 | +2. Select **SQL Databases** from the left-hand menu, and click the SSISDB database on the **SQL databases** page. |
| 30 | +3. On the **Overview** page for your database, review the fully qualified server name. To bring up the **Click to copy** option, hover over the server name. |
| 31 | +4. If you forget your Azure SQL Database server login information, navigate to the SQL Database server page to view the server admin name. You can reset the password if necessary. |
| 32 | +5. Click **Show database connection strings**. |
| 33 | +6. Review the complete **ADO.NET** connection string. The sample code uses a `SqlConnectionStringBuilder` to recreate this connection string with the individual parameter values that you provide. |
| 34 | + |
| 35 | +## Create a new Visual Studio project |
| 36 | + |
| 37 | +1. In Visual Studio, choose **File**, **New**, **Project**. |
| 38 | +2. In the **New Project** dialog, and expand **Visual C#**. |
| 39 | +3. Select **Console App** and enter *deploy_ssis_project* for the project name. |
| 40 | +4. Click **OK** to create and open the new project in Visual Studio. |
| 41 | + |
| 42 | +## Add references |
| 43 | +1. In Solution Explorer, right-click the **References** folder and select **Add Reference**. The **Reference Manager** dialog box opens. |
| 44 | +2. In the **Reference Manager** dialog box, expand **Assemblies** and select **Extensions**. |
| 45 | +3. Select the following two references to add: |
| 46 | + - Microsoft.SqlServer.Management.Sdk.Sfc |
| 47 | + - Microsoft.SqlServer.Smo |
| 48 | +4. Click the **Browse** button to add a reference to **Microsoft.SqlServer.Management.IntegrationServices**. (This assembly is installed only in the global assembly cache (GAC).) The **Select the files to reference** dialog box opens. |
| 49 | +5. In the **Select the files to reference** dialog box, navigate to the GAC folder that contains the assembly. Typically this folder is `C:\Windows\assembly\GAC_MSIL\Microsoft.SqlServer.Management.IntegrationServices\14.0.0.0__89845dcd8080cc91`. |
| 50 | +6. Select the assembly (that is, the .dll file) in the folder and click **Add**. |
| 51 | +7. Click **OK** to close the **Reference Manager** dialog box and add the three references. To make sure the references are there, check the **References** list in Solution Explorer. |
| 52 | + |
| 53 | +## Add the C# code |
| 54 | +1. Open **Program.cs**. |
| 55 | + |
| 56 | +2. Replace the contents of **Program.cs** with the following code. Add the appropriate values for your server, database, user, and password. |
| 57 | + |
| 58 | +> [!NOTE] |
| 59 | +> The following example uses Windows Authentication. To use SQL Server authentication, replace the `Integrated Security=SSPI;` argument with `User ID=<user name>;Password=<password>;`. |
| 60 | +
|
| 61 | +```csharp |
| 62 | +using Microsoft.SqlServer.Management.IntegrationServices; |
| 63 | +using System; |
| 64 | +using System.Data.SqlClient; |
| 65 | +using System.IO; |
| 66 | + |
| 67 | +namespace deploy_ssis_project |
| 68 | +{ |
| 69 | + class Program |
| 70 | + { |
| 71 | + static void Main(string[] args) |
| 72 | + { |
| 73 | + // Variables |
| 74 | + string targetServerName = "localhost"; |
| 75 | + string targetFolderName = "Project1Folder"; |
| 76 | + string projectName = "Integration Services Project1"; |
| 77 | + string projectFilePath = @"C:\Projects\Integration Services Project1\Integration Services Project1\bin\Development\Integration Services Project1.ispac"; |
| 78 | + |
| 79 | + // Create a connection to the server |
| 80 | + string sqlConnectionString = "Data Source=" + targetServerName + |
| 81 | + ";Initial Catalog=master;Integrated Security=SSPI;"; |
| 82 | + SqlConnection sqlConnection = new SqlConnection(sqlConnectionString); |
| 83 | + |
| 84 | + // Create the Integration Services object |
| 85 | + IntegrationServices integrationServices = new IntegrationServices(sqlConnection); |
| 86 | + |
| 87 | + // Get the Integration Services catalog |
| 88 | + Catalog catalog = integrationServices.Catalogs["SSISDB"]; |
| 89 | + |
| 90 | + // Create the target folder |
| 91 | + CatalogFolder folder = new CatalogFolder(catalog, |
| 92 | + targetFolderName, "Folder description"); |
| 93 | + folder.Create(); |
| 94 | + |
| 95 | + Console.WriteLine("Deploying " + projectName + " project."); |
| 96 | + |
| 97 | + byte[] projectFile = File.ReadAllBytes(projectFilePath); |
| 98 | + folder.DeployProject(projectName, projectFile); |
| 99 | + |
| 100 | + Console.WriteLine("Done."); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +## Run the code |
| 107 | + |
| 108 | +1. To run the application, press **F5**. |
| 109 | +2. In SSMS, verify that the project has been deployed. |
| 110 | + |
| 111 | +## Next steps |
| 112 | +- Consider other ways to deploy a package. |
| 113 | + - [Deploy an SSIS package with SSMS](./ssis-quickstart-deploy-ssms.md) |
| 114 | + - [Deploy an SSIS package with Transact-SQL (SSMS)](./ssis-quickstart-deploy-tsql-ssms.md) |
| 115 | + - [Deploy an SSIS package with Transact-SQL (VS Code)](ssis-quickstart-deploy-tsql-vscode.md) |
| 116 | + - [Deploy an SSIS package from the command prompt](./ssis-quickstart-deploy-cmdline.md) |
| 117 | + - [Deploy an SSIS package with PowerShell](ssis-quickstart-deploy-powershell.md) |
| 118 | +- Run a deployed package. To run a package, you can choose from several tools and languages. For more info, see the following articles: |
| 119 | + - [Run an SSIS package with SSMS](./ssis-quickstart-run-ssms.md) |
| 120 | + - [Run an SSIS package with Transact-SQL (SSMS)](./ssis-quickstart-run-tsql-ssms.md) |
| 121 | + - [Run an SSIS package with Transact-SQL (VS Code)](ssis-quickstart-run-tsql-vscode.md) |
| 122 | + - [Run an SSIS package from the command prompt](./ssis-quickstart-run-cmdline.md) |
| 123 | + - [Run an SSIS package with PowerShell](ssis-quickstart-run-powershell.md) |
| 124 | + - [Run an SSIS package with C#](./ssis-quickstart-run-dotnet.md) |
0 commit comments