How to pass variables to an SSIS package from a C# application - c#

Basically i am trying to build an application that uses SSIS to run a series of sql stuff.
Here is my code thus far:
public JsonResult FireSSIS()
{
string x = string.Empty;
try
{
Application app = new Application();
Package package = null;
package = app.LoadPackage(#"C:\ft\Package.dtsx", null);
Microsoft.SqlServer.Dts.Runtime.DTSExecResult results = package.Execute();
if (results == Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure)
{
foreach (Microsoft.SqlServer.Dts.Runtime.DtsError local_DtsError in package.Errors)
{
x += string.Concat("Package Execution results: {0}", local_DtsError.Description.ToString());
}
}
}
catch (DtsException ex)
{
// Exception = ex.Message;
}
return Json(x, JsonRequestBehavior.AllowGet);
}
Does anybody know how to pass a variable to the package itself in this way?

You need to use the Package.Variables property.
Package package = null;
package = app.LoadPackage(#"C:\ft\Package.dtsx", null);
package.Variables["User::varParam"].Value = "param value";

Try that:
Microsoft.SqlServer.Dts.RunTime.Variables myVars = package.Variables;
myVars["MyVariable1"].Value = "value1";
myVars["MyVariable2"].Value = "value2";
Microsoft.SqlServer.Dts.Runtime.DTSExecResult results = package.Execute(null, myVars, null, null, null);

Related

The type initializer for 'Google.OrTools.ConstraintSolver.operations_research_constraint_solverPINVOKE' threw an exception

I have a google routing web service. it works in a local run project.
but when I upload it on the host I get this error message:
The type initializer for
'Google.OrTools.ConstraintSolver.operations_research_constraint_solverPINVOKE'
threw an exception.
notes
1-I use vs 2019
2- I set enable-32-bit-application to false in my Plesk host panel
3- I build my project in 64 bit
4- I installed google.ortool by the package manager download
my code is :
static List<int> PrintSolution(in RoutingModel routing, in RoutingIndexManager manager, in Assignment solution, ref bool HasErr, ref string ErrMsg)
{
List<int> orderIndex = new List<int>();
try
{
long routeDistance = 0;
var index = routing.Start(0);
while (routing.IsEnd(index) == false)
{
orderIndex.Add((int)index);
var previousIndex = index;
index = solution.Value(routing.NextVar(index));
routeDistance += routing.GetArcCostForVehicle(previousIndex, index, 0);
}
}
catch (Exception e)
{
HasErr = true;
ErrMsg = "GetIndexOrderLocations " + e.Message;
orderIndex = null;
}
return orderIndex;
}
public static List<int> GetIndexOrderLocations(/String[] args/long[,] DistanceMatrix, ref bool HasErr, ref string ErrMsg)
{
RoutingModel routing = null;
RoutingIndexManager manager = null;
Assignment solution = null;
List<int> Result = new List<int>();
try
{
// Instantiate the data problem.
DataModel data = new DataModel();
// Create Routing Index Manager
manager = new RoutingIndexManager(/ data./ DistanceMatrix.GetLength(0), data.VehicleNumber, data.Depot);
// Create Routing Model.
routing = new RoutingModel(manager);
int transitCallbackIndex = routing.RegisterTransitCallback((long fromIndex, long toIndex) => {
// Convert from routing variable Index to distance matrix NodeIndex.
var fromNode = manager.IndexToNode(fromIndex);
var toNode = manager.IndexToNode(toIndex);
return/* data.*/DistanceMatrix[fromNode, toNode];
});
// Define cost of each arc.
routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
// Setting first solution heuristic.
RoutingSearchParameters searchParameters =
operations_research_constraint_solver.DefaultRoutingSearchParameters();
searchParameters.FirstSolutionStrategy = FirstSolutionStrategy.Types.Value.PathCheapestArc;
// Solve the problem.
solution = routing.SolveWithParameters(searchParameters);
if (routing == null || manager == null || solution == null)
return null;
Result = PrintSolution(routing, manager, solution, ref HasErr, ref ErrMsg);
}
catch (Exception e)
{
HasErr = true;
ErrMsg = "GetIndexOrderLocations " + e.Message;
}
// Print solution on console.
return Result;
}
Why I get this error?

C# WUApiLib know if a windows update needs a restart

I use this code to get pending windows updates and also most of the informations of the update:
static List<PendingUpdate> GetPendingUpdates()
{
var updateSession = new UpdateSession();
var updateSearcher = updateSession.CreateUpdateSearcher();
updateSearcher.Online = false; //set to true if you want to search online
List<PendingUpdate> pendingUpdates = new List<PendingUpdate>();
try
{
var searchResult = updateSearcher.Search("IsInstalled=0 And IsHidden=0");
if (searchResult.Updates.Count > 0)
{
Console.WriteLine("There are updates available for installation");
foreach (IUpdate windowsUpdate in searchResult.Updates)
{
PendingUpdate update = new PendingUpdate();
update.Title = windowsUpdate.Title;
update.Description = windowsUpdate.Description;
update.Downloaded = windowsUpdate.IsDownloaded;
update.Urls = new List<string>();
foreach (string url in windowsUpdate.MoreInfoUrls)
{
update.Urls.Add(url);
}
foreach (dynamic category in windowsUpdate.Categories)
{
update.Categories += category.Name + ", ";
}
pendingUpdates.Add(update);
}
}
}
catch (Exception ex)
{
Console.WriteLine("ERROR");
throw ex;
}
return pendingUpdates;
}
I also use this code to get to know if the computer currently needs a restart to finish installed updates:
static bool needsRestart()
{
ISystemInformation systemInfo = new SystemInformation();
return systemInfo.RebootRequired;
}
Now my question is, is it possible to get to know if an pending update needs a computer restart to finish? In the first code I get a IUpdate object but I dont see informations about a needed restart after installing this update. I there a way to get this information?
For the asynchronous installation I use something like this:
rebootRequired = false;
UpdateSession updateSession = new UpdateSession();
updateSession.ClientApplicationID = SusClientID;
IUpdateInstaller updatesInstaller = updateSession.CreateUpdateInstaller();
IInstallationJob job = updatesInstaller.BeginInstall(InstallProgressCallback, installComplete, installState);
// here is your installer code and the checking if the installation is completed
IInstallationProgress jobProgress = job.GetProgress();
for (int updateindex = 0; updateindex < updatesInstaller.Updates.Count; updateindex++)
{
IUpdateInstallationResult updateInstallResult = jobProgress.GetUpdateResult(updateindex);
rebootRequired |= updateInstallResult.RebootRequired;
}
if(rebootRequired)
{
// any of the updates need a reboot
}

How to pass variables from c# to ssis package

I am trying to build an application with ASP.NET that execute SSIS Package,so I want to pass variables from the application to this Package.
I want to know if there is some configuration missed in SSIS.
var a = demande_provision_creance.Année;
var t = demande_provision_creance.Trimestre;
var e = demande_provision_creance.Etat;
var i = demande_provision_creance.ID;
db.SaveChanges();
try
{
String pkgLocation = #"C:\PROVISION_CREANCES.dtsx";
Application app = new Application();
Package ssisPackage = null;
ssisPackage = app.LoadPackage(pkgLocation, null);
Microsoft.SqlServer.Dts.Runtime.Variables vars = ssisPackage.Variables;
vars["User::Annee"].Value = a;
vars["User::trimestre"].Value = t;
vars["User::etat"].Value = e;
vars["User::id_demande"].Value = i;
Microsoft.SqlServer.Dts.Runtime.DTSExecResult results = ssisPackage.Execute(null, vars, null, null, null);
if (results == Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure)
{
foreach (Microsoft.SqlServer.Dts.Runtime.DtsError local_DtsError in ssisPackage.Errors)
{
x += string.Concat("Package Execution results: {0}", local_DtsError.Description.ToString());
}
}
}
catch (DtsException ex)
{
// Exception = ex.Message;
}
return Json(x, JsonRequestBehavior.AllowGet);
You are almost there, you don't need to provide the variables scope beside of the variables name:
vars["Annee"].Value = a;
vars["trimestre"].Value = t;
vars["etat"].Value = e;
vars["id_demande"].Value = i;
Providing the scope is used within a script component or script task, but not when working with packages programatically.
Working with Variables Programmatically

Parse WebCacheV01.dat in C#

I'm looking to parse the WebCacheV01.dat file using C# to find the last file location for upload in an Internet browser.
%LocalAppData%\Microsoft\Windows\WebCache\WebCacheV01.dat
I using the Managed Esent nuget package.
Esent.Isam
Esent.Interop
When I try and run the below code it fails at:
Api.JetGetDatabaseFileInfo(filePath, out pageSize, JET_DbInfo.PageSize);
Or if I use
Api.JetSetSystemParameter(instance, JET_SESID.Nil, JET_param.CircularLog, 1, null);
at
Api.JetAttachDatabase(sesid, filePath, AttachDatabaseGrbit.ReadOnly);
I get the following error:
An unhandled exception of type
'Microsoft.Isam.Esent.Interop.EsentFileAccessDeniedException' occurred
in Esent.Interop.dll
Additional information: Cannot access file, the file is locked or in use
string localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string filePathExtra = #"\Microsoft\Windows\WebCache\WebCacheV01.dat";
string filePath = string.Format("{0}{1}", localAppDataPath, filePathExtra);
JET_INSTANCE instance;
JET_SESID sesid;
JET_DBID dbid;
JET_TABLEID tableid;
String connect = "";
JET_SNP snp;
JET_SNT snt;
object data;
int numInstance = 0;
JET_INSTANCE_INFO [] instances;
int pageSize;
JET_COLUMNDEF columndef = new JET_COLUMNDEF();
JET_COLUMNID columnid;
Api.JetCreateInstance(out instance, "instance");
Api.JetGetDatabaseFileInfo(filePath, out pageSize, JET_DbInfo.PageSize);
Api.JetSetSystemParameter(JET_INSTANCE.Nil, JET_SESID.Nil, JET_param.DatabasePageSize, pageSize, null);
//Api.JetSetSystemParameter(instance, JET_SESID.Nil, JET_param.CircularLog, 1, null);
Api.JetInit(ref instance);
Api.JetBeginSession(instance, out sesid, null, null);
//Do stuff in db
Api.JetEndSession(sesid, EndSessionGrbit.None);
Api.JetTerm(instance);
Is it not possible to read this without making modifications?
Viewer
http://www.nirsoft.net/utils/ese_database_view.html
Python
https://jon.glass/attempts-to-parse-webcachev01-dat/
libesedb
impacket
Issue:
The file is probably in use.
Solution:
in order to free the locked file, please stop the Schedule Task -\Microsoft\Windows\Wininet\CacheTask.
The Code
public override IEnumerable<string> GetBrowsingHistoryUrls(FileInfo fileInfo)
{
var fileName = fileInfo.FullName;
var results = new List<string>();
try
{
int pageSize;
Api.JetGetDatabaseFileInfo(fileName, out pageSize, JET_DbInfo.PageSize);
SystemParameters.DatabasePageSize = pageSize;
using (var instance = new Instance("Browsing History"))
{
var param = new InstanceParameters(instance);
param.Recovery = false;
instance.Init();
using (var session = new Session(instance))
{
Api.JetAttachDatabase(session, fileName, AttachDatabaseGrbit.ReadOnly);
JET_DBID dbid;
Api.JetOpenDatabase(session, fileName, null, out dbid, OpenDatabaseGrbit.ReadOnly);
using (var tableContainers = new Table(session, dbid, "Containers", OpenTableGrbit.ReadOnly))
{
IDictionary<string, JET_COLUMNID> containerColumns = Api.GetColumnDictionary(session, tableContainers);
if (Api.TryMoveFirst(session, tableContainers))
{
do
{
var retrieveColumnAsInt32 = Api.RetrieveColumnAsInt32(session, tableContainers, columnIds["ContainerId"]);
if (retrieveColumnAsInt32 != null)
{
var containerId = (int)retrieveColumnAsInt32;
using (var table = new Table(session, dbid, "Container_" + containerId, OpenTableGrbit.ReadOnly))
{
var tableColumns = Api.GetColumnDictionary(session, table);
if (Api.TryMoveFirst(session, table))
{
do
{
var url = Api.RetrieveColumnAsString(
session,
table,
tableColumns["Url"],
Encoding.Unicode);
var downloadedFileName = Api.RetrieveColumnAsString(
session,
table,
columnIds2["Filename"]);
if(string.IsNullOrEmpty(downloadedFileName)) // check for download history only.
continue;
// Order by access Time to find the last uploaded file.
var accessedTime = Api.RetrieveColumnAsInt64(
session,
table,
columnIds2["AccessedTime"]);
var lastVisitTime = accessedTime.HasValue ? DateTime.FromFileTimeUtc(accessedTime.Value) : DateTime.MinValue;
results.Add(url);
}
while (Api.TryMoveNext(session, table.JetTableid));
}
}
}
} while (Api.TryMoveNext(session, tableContainers));
}
}
}
}
}
catch (Exception ex)
{
// log goes here....
}
return results;
}
Utils
Task Scheduler Wrapper
You can use Microsoft.Win32.TaskScheduler.TaskService Wrapper to stop it using c#, just add this Nuget package [nuget]:https://taskscheduler.codeplex.com/
Usage
public static FileInfo CopyLockedFileRtl(DirectoryInfo directory, FileInfo fileInfo, string remoteEndPoint)
{
FileInfo copiedFileInfo = null;
using (var ts = new TaskService(string.Format(#"\\{0}", remoteEndPoint)))
{
var task = ts.GetTask(#"\Microsoft\Windows\Wininet\CacheTask");
task.Stop();
task.Enabled = false;
var byteArray = FileHelper.ReadOnlyAllBytes(fileInfo);
var filePath = Path.Combine(directory.FullName, "unlockedfile.dat");
File.WriteAllBytes(filePath, byteArray);
copiedFileInfo = new FileInfo(filePath);
task.Enabled = true;
task.Run();
task.Dispose();
}
return copiedFileInfo;
}
I was not able to get Adam's answer to work. What worked for me was making a copy with AlphaVSS (a .NET class library that has a managed API for the Volume Shadow Copy Service). The file was in "Dirty Shutdown" state, so I additionally wrote this to handle the exception it threw when I opened it:
catch (EsentErrorException ex)
{ // Usually after the database is copied, it's in Dirty Shutdown state
// This can be verified by running "esentutl.exe /Mh WebCacheV01.dat"
logger.Info(ex.Message);
switch (ex.Error)
{
case JET_err.SecondaryIndexCorrupted:
logger.Info("Secondary Index Corrupted detected, exiting...");
Api.JetTerm2(instance, TermGrbit.Complete);
return false;
case JET_err.DatabaseDirtyShutdown:
logger.Info("Dirty shutdown detected, attempting to recover...");
try
{
Api.JetTerm2(instance, TermGrbit.Complete);
Process.Start("esentutl.exe", "/p /o " + newPath);
Thread.Sleep(5000);
Api.JetInit(ref instance);
Api.JetBeginSession(instance, out sessionId, null, null);
Api.JetAttachDatabase(sessionId, newPath, AttachDatabaseGrbit.None);
}
catch (Exception e2)
{
logger.Info("Could not recover database " + newPath + ", will try opening it one last time. If that doesn't work, try using other esentutl commands", e2);
}
break;
}
}
I'm thinking about using the 'Recent Items' folder as when you select a file to upload an entry is written here:
C:\Users\USER\AppData\Roaming\Microsoft\Windows\Recent
string recent = (Environment.GetFolderPath(Environment.SpecialFolder.Recent));

Setting Hyper-V snapshot's name programmatically

I'm creating an Hyper-V snapshot with a C# program:
private static bool Snapshot(string vmName, string snapshotName)
{
var result = false;
var scope = new ManagementScope(#"root\virtualization", null);
var virtualSystemService = Utility.GetServiceObject(scope, "Msvm_VirtualSystemManagementService");
var vm = Utility.GetTargetComputer(vmName, scope);
var inParams = virtualSystemService.GetMethodParameters("CreateVirtualSystemSnapshot");
inParams["SourceSystem"] = vm.Path.Path;
var outParams = virtualSystemService.InvokeMethod("CreateVirtualSystemSnapshot", inParams, null);
if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)
{
if (Utility.JobCompleted(outParams, scope))
{
Console.WriteLine("Snapshot was created successfully.");
result = true;
}
else
{
Console.WriteLine("Failed to create snapshot VM");
result = false;
}
}
else if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
{
Console.WriteLine("Snapshot was created successfully.");
result = true;
}
else
{
Console.WriteLine("Create virtual system snapshot failed with error {0}", outParams["ReturnValue"]);
result = false;
}
inParams.Dispose();
outParams.Dispose();
vm.Dispose();
virtualSystemService.Dispose();
return result;
}
(NOTE: This code was taken from MSDN)
Is there a way to set the snapshot name through this WMI call ? Otherwise, does anyone know a working solution to rename a snapshot through WMI call? I already found this thread, but it's some kind of ambiguous and it doesn't provide any solution ...
EDIT: The solution was to rename snapshot after having create it. Here's my function to rename the snapshot using Hans advice:
SOLUTION:
public static bool RenameSnapshot(string vmName, string snapshotName)
{
var result = false;
var scope = new ManagementScope(#"root\virtualization", null);
var vm = Utility.GetTargetComputer(vmName, scope);
// load snapshot
var objSnapshot = GetLastVirtualSystemSnapshot(vm);
// rename snapshot
objSnapshot["ElementName"] = snapshotName;
// save
var virtualSystemService = Utility.GetServiceObject(scope, "Msvm_VirtualSystemManagementService");
var inParams = virtualSystemService.GetMethodParameters("ModifyVirtualSystem");
inParams["ComputerSystem"] = vm.Path.Path;
inParams["SystemSettingData"] = objSnapshot.GetText(TextFormat.CimDtd20);
var outParams = virtualSystemService.InvokeMethod("ModifyVirtualSystem", inParams, null);
if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
{
result = true;
}
else
{
result = false;
}
inParams.Dispose();
outParams.Dispose();
vm.Dispose();
virtualSystemService.Dispose();
return result;
}
You have to use the ModifyVirtualSystem method of the Msvm_VirtualSystemManagementService class to rename a hyper-v snapshot. There is a MSDN example on how to rename a hyper-v virtual machine (You have to modify the code in order to rename a snapshot). Furthermore I've found this example on how to rename a hyper-v snapshot.
Hope, this helps.

Categories

Resources