SSIS Add Oracle connection manager with connection string - c#

In package MainPackage.dtsx I have 10 connected Connection managers in that package. 9 are "OLE DB\Oracle Provider for OLE DB" and 1 is "OLE DB\SQL Server Native Client 11.0". In project, I have one C# Script, that create package NewPackageThatNotWillSaved.dtsx with dataFlow that take rows from table A, add column with datetime, copy to table B and count copied rows. And execute package NewPackageThatNotWillSaved.dtsx. In order for the package to be executed, I add 2 connection managers to the package(Source and Destination are connection managers from package MainPackage.dtsx by Dts.Connections) by connection string.
Here the problem. Connection string from SQL Server work fine, but connection string from Oracle throw error DTS_E_CAN NOT ACQUIRE CONNECTION FROM CONNECTION MANAGER every time, when I srcDesignTimeComponent.AcquireConnections(null); (IDTSDesigntimeComponent100 srcDesignTimeComponent). I can`t use password for oracle connection string because 9 different oracle servers have 9 different passwords and password in connection sting sounds bad.
Some C# code that I use:
// Create package
Application app = new Application();
Package package = new Package();
// Get connection manager
ConnectionManager sourceConnection = null;
ConnectionManager destinationConnection = null;
foreach (ConnectionManager conn in Dts.Connections)
{
if (conn.Name.Equals(sourceConnectionName))
{
sourceConnection = conn;
}
if (conn.Name.Equals(destinationConnectionName))
{
destinationConnection = conn;
}
}
// Create dataFlow
Executable e = package.Executables.Add("STOCK:PipelineTask");
TaskHost thMainPipe = e as TaskHost;
MainPipe dataFlowTask = thMainPipe.InnerObject as MainPipe;
thMainPipe.Name = dataFlowName;
// Add connection manager for OLE DB Source
ConnectionManager sourceConn = package.Connections.Add("OLEDB");
sourceConn.Name = sourceConnection.Name + "_src";
sourceConn.ConnectionString = sourceConnection.ConnectionString;
// Add OLE DB Source
IDTSComponentMetaData100 source = dataFlowTask.ComponentMetaDataCollection.New();
source.ComponentClassID = "DTSAdapter.OleDbSource";
source.ValidateExternalMetadata = true;
IDTSDesigntimeComponent100 srcDesignTimeComponent = source.Instantiate();
srcDesignTimeComponent.ProvideComponentProperties();
// Set connetcion manager for OLE DB Source
source.RuntimeConnectionCollection[0].ConnectionManager = DtsConvert.GetExtendedInterface(sourceConn); ;
source.RuntimeConnectionCollection[0].ConnectionManagerID = sourceConn.ID;
// Setting propreties
srcDesignTimeComponent.SetComponentProperty("AccessMode", 2);
srcDesignTimeComponent.SetComponentProperty("SqlCommand", sqlQuery);
srcDesignTimeComponent.AcquireConnections(null); // Throw ERROR -1071611876
srcDesignTimeComponent.ReinitializeMetaData();
srcDesignTimeComponent.ReleaseConnections();
Edit 1: Maybe exist better way to AcquireConnections from exist connected connection manager from MainPackage.dtsx?

Related

Unable to open SQLiteConnection on the network directory in c#

Using C#, .Net framework 4.8, SQLite 1.0.115
I am using System.Data.SQLite.SQLiteConnection to open a sqllite database on the network drive. But it fails with "unable to open database file". While debugging it I found that it is always pointing to the local drive instead of the network.
example: I have provided "\\NW\test\DB\test.sqlite" as a data source in the connection string. But when I see an exception it says can't find the file in "C:\NW\test\DB\test.sqlite". My program runs on C: drive.
private SQLiteConnection _connection;
var builder = new SQLiteConnectionStringBuilder
{
DataSource = "\\\\NW\\test\\DB\\test.sqlite",
Version = 3,
ForeignKeys = true,
};
var connectionString = builder.ConnectionString;
if (_connection == null)
{
_connection = CreateConnection(connectionString );
_connection.Open();
}
return _connection.BeginTransaction(isolationLevel);
On connection open, I am getting this exception
SQLite error (14): os_win.c:47688: (3) winOpen(C:\NW\test\DB\test.sqlite) - The system cannot find the path specified.
Does anyone know why it is pointing to a local drive instead of the network?
By doing multiple trials and errors this change fixed the issue
private SQLiteConnection _connection;
var builder = new SQLiteConnectionStringBuilder
{
DataSource = #"\\\\NW\\test\\DB\\test.sqlite",
Version = 3,
ForeignKeys = true,
};
var connectionString = builder.ConnectionString;
if (_connection == null)
{
_connection = CreateConnection(connectionString );
_connection.Open();
}
return _connection.BeginTransaction(isolationLevel);
So, in the Datasource it needs "\\\\\\\\NW\\test\\DB\\test.sqlite" path. Not sure why so many back slashes but this fixed the problem.

How to restore backup form one linked server to another linked server using C# SMO

Want to restore a database backup from linked server A to a database located on linked-server B using C#. prefer to use SMO.
I can restore from my local backup to local machine.
{
conn = new ServerConnection
{
ConnectionString = #"Data Source =
(localdb)\MSSQLLocalDb;Initial Catalog=master;Integrated Security=true",
};
try
{
//Restore Full
srv = new Server(conn);
//lsrv = srv.LinkedServers[#"DEVSQL\ALPHA"]; need to figure out how to restore to linked server instead of local.
//srv.KillAllProcesses("G4TestNew");
var res = new Restore();
res.Database = "G4TestNew";
res.Action = RestoreActionType.Database;
filePath = #"\\CABCSERVER\Database\Temp\Full.bak";
res.Devices.AddDevice(filePath, DeviceType.File);
res.ReplaceDatabase = true;
res.NoRecovery = true;
var dataFile = new RelocateFile("G4Test", #"C:\TBD\G4Test.mdf");
var logFile = new RelocateFile("G4Test_log", #"C:\TBD\G4TestNew.ldf");
res.RelocateFiles.Add(dataFile);
res.RelocateFiles.Add(logFile);
res.SqlRestore(srv);
}
EDIT(Adding more detail):.
In this case the linked servers are accessed via 'sql server authentication' and application does not have access to the credential required to connect directly and can use 'Integrated Security' to connect to localdb only.
In SMO you would not connect to one server and then administer a linked server. Instead connect directly to the target server. eg:
ConnectionString = #"Data Source =
DEVSQL\ALPHA;Initial Catalog=master;Integrated Security=true",
srv = new Server(conn);

sqlite c#: database connection not valid

I have a sqlite database file in .s3db, It has all the tables and data already populated in it. I am trying to connect to to the database use sqliteConnection. But it does not seem to work..I have added the reference of sqlite.dll, does c# needs some other reference to make the connection? If I make a new sqlite db, it is made as xyz.sqlite, maybe it is not recognizing the database extension.
This is how I am making the connection:
// Creates a connection with our database file.
public void connectToDatabase()
{
//this.dbConnection = new SQLiteConnection(#"data source=Fut_Autobuyer_2012.s3db;version=3;");
string dbConnectionString = #"Data Source=Fut_Autobuyer_2012.s3db";
this.dbConnection = new SQLiteConnection(dbConnectionString);
}
This is what I get when the connection is made:
Database connection not valid for getting number of changes.
Database connection not valid for getting last insert rowid.
Database connection not valid for getting maximum memory used.
Database connection not valid for getting memory used.
It looks like you must open database connection:
using (var connection = SQLiteFactory.Instance.CreateConnection())
{
Debug.Assert(connection != null, "connection != null");
connection.ConnectionString = connectionString;
connection.Open();
try
{
using (var command = connection.CreateCommand())
{
// Execute connection
}
}
finally
{
connection.Close();
}
}

How to get SQL JobStep connection strings

After I get the server jobs and I got each job steps I want to get the connection string related to each step as you could find it while opening SQL management studio in jobs like this:
is there is a suitable way to get the connection strings for each package by C# code?
ServerConnection conn = new ServerConnection("localhost");
//new SqlConnection("data source=localhost;initial catalog=CPEInventory_20101122;integrated security=True;"));
Server server = new Server(conn);
JobCollection jobs = server.JobServer.Jobs;
var stepInformationsDetailsList = new List<StepInformationsDetails>();
foreach (Job job in jobs)
{
foreach (JobStep jobstep in job.JobSteps)
{
stepInformationsDetailsList.Add(new StepInformationsDetails() {
ServerName = job.Parent.MsxServerName,
ReportName = job.Name,
StepName = jobstep.Name,
Command = jobstep.Command,
Schedual = jobstep.DatabaseName,
StepID = jobstep.ID
});
}
}
dataGridView1.DataSource = stepInformationsDetailsList;
That data will all be in your Command variable. When you override/add anything on the job step tabs, the net result is that the command line passed to dtexec has those values. The UI is simply slicing arguments out of the command column in the msdb.dbo.sysjobsteps table to link them to various tabs.
On your localhost, this query ought to return back the full command line.
SELECT
JS.command
FROM
dbo.sysjobs AS SJ
INNER JOIN dbo.sysjobsteps AS JS
ON JS.job_id = SJ.job_id
WHERE
sj.name = 'Concessions made by Simba Auto-Report';
Since you are not overriding the value of the Connection Manager 'Simba Replica...', it is not going to show up in the output of the command. If it was, you'd have a string like /SQL "\"\MyFolder\MyPackage\"" /SERVER "\"localhost\sql2008r2\"" /CONNECTION SYSDB;"\"Data Source=SQLDEV01\INT;Initial Catalog=SYSDB;Provider=SQLNCLI10.1;Integrated Security=SSPI;Auto Translate=False;\"" /CHECKPOINTING OFF /REPORTING E The /CONNECTION section would correlate to your 'Simba Replica...' value.
I suspect, but do not know, that the UI is examining the SSIS package via the API Microsoft.SqlServer.Dts.Runtime and identifying all the Connection Manager, via Connections property to build out that dialog.

Copying databases between sql servers

I am developing a Web Application for copying data between SQL Servers. The tool will let you specify which server you are copying from/to and will then copy a specific database (which always has the same name) from the source server to the destination server.
What is the best method to do this? The data could be quite large, so speed needs to be taken into account also.
My attempt at this is to try to run an SSIS package that I created using SQL Server Management Studio. The package is stored locally.
The plan is to modify the Source and Destination connection strings and kick off the package.
This is my code for doing so:
public void DataTransfer(String sourceConnection, String destConnection, String pkgLocation)
{
Package pkg;
Application app;
DTSExecResult pkgResults;
try
{
app = new Application();
pkg = app.LoadPackage(pkgLocation, null);
foreach (ConnectionManager connectionManager in pkg.Connections)
{
SqlConnectionStringBuilder builder;
switch (connectionManager.Name)
{
case "SourceConnection":
builder = new SqlConnectionStringBuilder(sourceConnection);
builder.Remove("Initial Catalog");
builder.Add("Initial Catalog", "StagingArea");
var sourceCon = builder.ConnectionString + ";Provider=SQLNCLI;Auto Translate=false;";
//Added spaces to retain password!!!
sourceCon = sourceCon.Replace(";", "; ");
connectionManager.ConnectionString = sourceCon;
Debug.WriteLine(connectionManager.ConnectionString.ToString());
break;
case "DestinationConnection":
builder = new SqlConnectionStringBuilder(destConnection);
builder.Remove("Initial Catalog");
builder.Add("Initial Catalog", "StagingArea");
var destCon = builder.ConnectionString + ";Provider=SQLNCLI;Auto Translate=false;";
//Added spaces to retain password!!!
destCon = destCon.Replace(";", "; ");
connectionManager.ConnectionString = destCon;
Debug.WriteLine(connectionManager.ConnectionString.ToString());
break;
}
}
pkgResults = pkg.Execute();
}
catch (Exception e)
{
throw;
}
Debug.WriteLine(pkgResults.ToString());
}
When pkg is executed I get the following exceptions:
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.SqlServer.ManagedDTS.dll
A first chance exception of type 'Microsoft.SqlServer.Dts.Runtime.DtsComponentException' occurred in Microsoft.SqlServer.ManagedDTS.dll
I'm not really sure where to go from here, any ideas?
I would write an SSIS package to do the data copying / transformation and the website would simply configure the connection string and kick off the package.

Categories

Resources