How to edit SSIS packages programatically in VB? - vb.net

We have created a workflow process as an SSIS package and would like to find a way of gaining access to this code so that through vb.net we can dynamically access and run that code. For example, we would like to change the data sources being used, or change the column mappings of existing packages and then execute them from a vb.net application. Please advise the best way to do this.

You will find some of your tasks easy, others not so much.
Generally speaking, you'll be interested in reading the Developers Guide to Integration Services. Of particular interest will be Building Packages Programmatically and Running and Managing Packages Programmatically.
For example, to run a package from VB.NET
Imports Microsoft.SqlServer.Dts.Runtime
Module Module1
Sub Main()
Dim pkgLocation As String
Dim pkg As New Package
Dim app As New Application
Dim pkgResults As DTSExecResult
pkgLocation = _
"C:\Program Files\Microsoft SQL Server\100\Samples\Integration Services" & _
"\Package Samples\CalculatedColumns Sample\CalculatedColumns\CalculatedColumns.dtsx"
pkg = app.LoadPackage(pkgLocation, Nothing)
pkgResults = pkg.Execute()
Console.WriteLine(pkgResults.ToString())
Console.ReadKey()
End Sub
End Module
To change the connection manager programmatically, it'd be the VB.NET equivalent of
ConnectionManager item = ep.Connections["MyConnectionManagerName"]
item.ConnectionString = #"Provider=SQLNCLI10.1;Data Source=Server1;Initial Catalog=ABC;Integrated Security=SSPI;";
Changing column mappings, that's where it's going to get interesting, for all definitions of the word interesting. I'm have a distilled example but it takes some work and you'll want to really understand the whole object model (and I hope you like COM). EzAPI might be of some assistance in that area.

Related

Clear cells in multiple sheets from SSIS by VBA

How I can use below VBA coding in SSIS vba. I want to clear cells(data) from multiple sheets from SSIS by VBA coding
sub cod()
ThisWorkbook.Worksheets("Case management details").Range("A2:K10000").clear
ThisWorkbook.Worksheets("interface Timeliness").Range("A2:G20000").clear
ThisWorkbook.Worksheets("Life Events").Range("A2:N10000").clear
End sub
I had the same problem here. If you decided to solve this using SSIS component try with Script Task.
After you create excel steps will be:
Add Script Task on control flow.
Double click on component.
Press 'Edit Script...' button.
Right-click on the project in new visual studio windows (should be something like 'ST_' and some guid) and click on Manage NuGet Packages...
From 'Browse' tab install:
Microsoft.Office.Interop.Excel
Microsoft.CSharp
Into namespaces region add these 2 lines:
using Microsoft.Office.Interop.Excel;
using Microsoft.CSharp.RuntimeBinder;
Copy below code into ScriptMain class:
var appExcel = new Excel.Application();
string filename = #"D:\PathToExcel\ExcelFile.xlsx";
var newBook = appExcel.Workbooks.Open(filename);
var oSheet1 = newBook.Worksheets["Case management details"];
oSheet1.Range("A2", "K10000").Delete();
newBook.Save();
appExcel.Workbooks.Close();
appExcel.Quit();
Close without saving the second visual studio window (where you write script code).
Run the package
Note:
Instead of hardcoded file location, you can replace it with a package variable. In this case 2nd line will be:
string filename = Dts.Variables["User::ExcelFilePath"].Value.ToString();
I tested this package before write answer and everything work perfect!
With this logic, create multiple sheet instance and delete what you want.
If something isn't clear or non-logical write me in a comment and I will make correction.

Access to path is denied when trying to import from the client's desktop with SSIS

I'm creating a html page that will import an excel file in to a tracking system. On a button click event excel file is located / ssis package is fired / data imported then closed out. Thats the idea work flow. Problem is the excel file access is being denied before the package even executes
Here is the exact error :
I've tried :
excel file properties have been shared to everyone
identity impersonate set to true
hard coding the path
here is the VB code
Protected Sub bntExecute_Click(sender As Object, e As EventArgs) Handles btnExecute.Click
Dim app As Application = New Application()
Dim package As Package = Nothing
'Dim fileName As String = "C:\Users\Desktop\T. Bryant III\PTSID_Update_Template"'
Try
Dim fileName As String = Server.MapPath(System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName.ToString()))
FileUpload1.PostedFile.SaveAs(fileName)
package = app.LoadPackage("#C:\Users\Desktop\T.Bryant III\KitImport", Nothing)
'excel connection from package'
package.Connections("SourceConnectionExcel").ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0data source =" + fileName + "Extended Properties = Excel 8.0"
'Execute the pakage'
Dim results As Microsoft.SqlServer.Dts.Runtime.DTSExecResult = package.Execute()
Catch ex As Exception
Throw ex
Finally
package.Dispose()
package = Nothing
End Try
End Sub
Thanks in advance or if there is an easier way to do this please let me know. The package when executing it in ssis works fine with its own connection manager etc.
A few things to try. If they don't work for you as permanent solutions, they should at least confirm that your code is working and you are dealing with a persmissions issue (which appears to be the case).
Move your file to the public folder (C:\Users\Public).
Run your application (or web browser) as an administrator (if applicable to your version of Windows).
If you are using a web browser, try using a different one.
If nothing else works, try pasting your code into a Windows Form Application.
If you still get the same error after trying all of this, it's time to take another look at your code. Remove the Try/Catch block to determine precisely which line is throwing the error. If you've tried hard coding, I'm guessing it's the SaveAs method. I'm not sure what class FileUpload1 is, but some SaveAs methods won't overwrite existing files unless you explicitly tell them to. Check the appropriate documentation and see if you don't need to pass a True value somewhere along with filename.
Update us with the results. At the very least, this should narrow down your problem and allow for a better diagnosis of it.

Importing B.exe into A.exe an then run B.exe from A.exe

I am using Visual Basic 2008 and I have a question about it?
I have a.exe and b.exe ( the a.exe is an vbApp, and b.exe is an executable file ). Is it possible to include the b.exe into a.exe and then running it from a.exe? By, for example, importing the b.exe into vbProject and then running it without extracting it.
The question is a bit vague, but you can definitely do something along those lines, in several different ways.
First, you can certainly compile a separate EXE (I'll call it EXEA) into a VB Project (Call it EXEB). When user runs EXEB, it extracts the resource containing EXEA, saves it as a file (likely to the temp folder or someplace with WRITE rights) and then shells to EXEA.
Another possibility would be to compile external functionality into a DLL, call it DLLA, then compile that dll into a VB project (call it EXEB).
When user runs EXEB, it extracts the resource containing DLLA, storing it as a memory stream, then uses ASSEMBLY.LOAD to load the DLL from the memory stream (instead of from a file), and at that point can create objects from that dll, and use it as normal.
In both these cases though, it's probably better to simply compile the second EXE or DLL and include both in an MSI installation project.
More details in the question might help narrow down other possible solutions as well.
Try this :
1) Open project of B.exe.
2) Create a new module in project B, add "Sub Main" , then write this
Sub Main(Byval args() As String)
Dim X As New Form1 'replace Form1" with your real startup form
X.ShowDialog
End Sub
3) Open properties of that project B, and uncheck checkbox with name "Enable application framework". And change "Startup Object" to "Sub Main"
4) Compile and close project B.
5) Now open project of A.exe, and use following code to run B.exe in memory:
Dim tmpAssembly As System.Reflection.Assembly = System.Reflection.Assembly.Load(System.IO.File.ReadAllBytes("C:\b.exe"))'Replace "C:\b.exe" real b.exe path
Dim TmpMethod As MethodInfo = tmpAssembly.EntryPoint
If TmpMethod IsNot Nothing Then
Dim TmpObject As Object = tmpAssembly.CreateInstance(TmpMethod.Name)
Dim args() As String = Nothing
TmpMethod.Invoke(TmpObject , args)
End If
Me.Close()
CAUTION! You can run applications on memory only if you have done steps 1 and 2 with those applications. Otherwise you will get errors , which are not solved yet...

Visual Basic 2008 File Search

I need a file search for my Visual Basic 2008 program. Do you guys know a way to start off? I've heard that you need admin priveledge for some file searches, but I don't want any of those kind of things since it would be a bug to the user.
The System.IO.Directory Class should help you here.
Also you may want to build a catalog system or find a system that supports an API. This way you dont have to walk a directory structure every time the user performs a query.
Basically you can do (pseudo code, adjust to fit):
Dim fn as File
For Each fn in System.IO.Directory.GetFiles(SomePath,"*.*",System.IO.SearchOption.AllDirectories)
' Do something with the fn
End For
Now there is no way around the NTFS ACL's and you would need to handle this as appropriate.
Have a look at this post to hook into Windows Desktop Search from C#.
Microsoft Windows Search 3.x SDK
Brief Description
Version 1.0
Download the Windows Search Software
Development Kit to explore sample
applications and develop using Visual
Studio, C#, and .NET technology.
Update 1
Function that does things with files in directories (pseudo code, adjust to fit):
Public Function MyDirectoryFunction(ByVal dir as String, ByVal fileMatch as String, ByVal beRecursive as Boolean)
Dim fn as File
Dim searchOpt as System.IO.SearchOption
If beRecursive = True Then
searchOpt = System.IO.SearchOption.AllDirectories
Else
searchOpt = System.IO.SearchOption.TopDirectoryOnly
End If
If String.IsNullOrEmpty(fileMatch) Then
fileMatch = "*.*"
End If
For Each fn in System.IO.Directory.GetFiles(dir, fileMatch, searchOpt)
' Do something with the fn
End For
End Function

Storing Connection Strings in Registry?

We decided to use the registry for handling our deployment with connection strings in our VB.net application.
The requirements are:
If the program cannot connect to the server, first check the registry for a connection string. IF not, create the folder and fill in the name, type, and data.
Make sure its encrypted.
I have never edited or created anything in the registry. Where do I start? If anybody has any code samples or links to articles I would really appreciate it.
It looks like this tutorial would be a good source for the problem. I would strongly recommend against storing the connection string in the registry. It adds more work and more dependencies on the current operating environment. Additionally, configuration files are more portable and are better suited for storing property related information. If you use a settings file the supporting admins and your support people will thank you. [Compared to placing the information in the registry.
Totally agree with Steven here, but if you have to do it...here is some info From MSDN (link to all you need to know at the bottom). The following example reads, increments, and then writes a DWORD value to HKCU:
Imports Microsoft.Win32
Dim regVersion As RegistryKey
regVersion =
Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\TestApp\\1.0", True)
If regVersion Is Nothing Then
' Key doesn't exist; create it.
regVersion =
Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\TestApp\\1.0")
End If
Dim intVersion As Integer = 0
If (Not regVersion Is Nothing) Then
intVersion = regVersion.GetValue("Version", 0)
intVersion = intVersion + 1
regVersion.SetValue("Version", intVersion)
regVersion.Close()
End If
http://msdn.microsoft.com/en-us/library/aa289494%28VS.71%29.aspx