VS 2008 / SQL 2008
I am importing .csv file to SQL Table.
I want to pass dynamically the Source File and Destination Connection string from C# Code.
For some reasons, this code is working well but package is not executing !!!! How should i pass connection string dynamically from C# code to SSIS Package !!
string strSourceConn = Server.MapPath(filePlacedOrder.Value);
string strDestConn = System.Configuration.ConfigurationManager.AppSettings["SDB"];
string pkgLocation = Server.MapPath("Package.dtsx");
Package pkg;
Microsoft.SqlServer.Dts.Runtime.Application app;
DTSExecResult pkgResults;
app = new Microsoft.SqlServer.Dts.Runtime.Application();
pkg = app.LoadPackage(pkgLocation, null);
pkg.Variables["sConn"].Value = strSourceConn;
pkg.Variables["dConn"].Value = strDestConn;
pkgResults = pkg.Execute();
The best way to dynamically change the connection string is to retrieve the desired connection from the package and then change its connection string. This is different from setting variables with the connection information. In this case you would want to use:
pkg.Connections["sConn"].ConnectionString = strSourceConn;
pkg.Connection["dConn"].ConnectionString = strDestConn;
Where sConn and dConn are the names of the connections in your package.
Related
I have a SSIS package with a bunch of variables, listed below:
I am trying to call the SSIS package from a C# Windows Form App using the code below:
// Create a connection to the server
string sqlConnectionString = "Data Source=BSQL_01;Initial Catalog=master;Integrated Security=SSPI;";
SqlConnection sqlConnection = new SqlConnection(sqlConnectionString);
// Create the Integration Services object
IntegrationServices integrationServices = new IntegrationServices(sqlConnection);
// Get the Integration Services catalog
Catalog catalog = integrationServices.Catalogs["SSISDB"];
// Get the folder
CatalogFolder folder = catalog.Folders["PORGPackages"];
// Get the project
ProjectInfo project = folder.Projects["PORGPackages"];
// Get the package
PackageInfo package = project.Packages["POHandler.dtsx"];
// Add project parameter
Collection<PackageInfo.ExecutionValueParameterSet> executionParameter = new Collection<PackageInfo.ExecutionValueParameterSet>();
executionParameter.Add(new PackageInfo.ExecutionValueParameterSet { ObjectType = 20, ParameterName = "SessionID", ParameterValue = "636943168325507712" });
// Run the package
long executionIdentifier = package.Execute(false, null, executionParameter);
ExecutionOperation executionOperation = integrationServices.Catalogs["SSISDB"].Executions[executionIdentifier];
while (!executionOperation.Completed) {
System.Threading.Thread.Sleep(5000);
executionOperation.Refresh();
MessageBox.Show("Running...");
}
if (executionOperation.Status == Operation.ServerOperationStatus.Success) {
Console.WriteLine("Success");
} else if (executionOperation.Status == Operation.ServerOperationStatus.Failed) {
Console.WriteLine("Failed");
} else {
Console.WriteLine("Something Went Really Wrong");
}
I am getting the following error:
The parameter 'SessionID' does not exist or you do not have sufficient
permissions.
Am I adding the parameter correctly? I don't know I can watch to see if it's being set, or if I have permission.
Am I adding the parameter correctly?
You have declared a variable called #SessionID not a parameter.
If you need to pass a variable value then you can refer to the following link:
How to pass variables to an SSIS package from a C# application
For more information about both objects (variables & parameters) you can refer to the following articles:
Getting Started with Parameters, Variables & Configurations in SSIS 2012
What's the difference between Variable and Parameters
SSIS Variables vs Parameters (SSIS Denali)
I have the following code that to execute the SSIS package from .Net Webapi, like this:
string targetServerName = "1.x.x.x";
string folderName = "MyFolder";
string projectName = "MyProject";
string packageName = "Package.dtsx";
// Create a connection to the server
string sqlConnectionString = "Data Source=" + targetServerName +
";Initial Catalog=master;User ID=sa; Password=abcd#1234;Persist Security Info=True;";
SqlConnection sqlConnection = new SqlConnection(sqlConnectionString);
// Create the Integration Services object
IntegrationServices integrationServices = new IntegrationServices(sqlConnection);
// Get the Integration Services catalog
Catalog catalog = integrationServices.Catalogs["SSISDB"];
// Get the folder
CatalogFolder folder = catalog.Folders[folderName];
// Get the project
ProjectInfo project = folder.Projects[projectName];
// Get the package
PackageInfo package = project.Packages[packageName];
// Run the package
package.Execute(false, null);
But when I run this code I get the following exception:
The operation cannot be started by an account that uses SQL Server Authentication. Start the operation with an account that uses Windows
Authentication.
What should I do so that I am able to execute the SSIS package from sql server authentication mode? Any help would be appreciated.
I am using Visual Studio 2017 and SQL Server 2017 and the last version of Crystal Reports. I then create a stored procedure in SQL Server and a new report in Visual Studio (C#) using the code to get the connection info from the app.config.
When I try to make the program work in PC client, I have the problem failed to open connection temp - the error is illustrated in the screenshot below.
This is the code for the connection info:
SaleReportCrystal aLL_PUSH_STUDENTS = new SaleReportCrystal();
System.Data.Common.DbConnectionStringBuilder builder = new System.Data.Common.DbConnectionStringBuilder();
builder.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString; ;
string server = builder["Data Source"] as string;
string database = builder["Initial Catalog"] as string;
string UserID = builder["User ID"] as string;
string password1 = builder["Password"] as string;
aLL_PUSH_STUDENTS.SetDatabaseLogon(UserID, password1, server, database);
ReportPrint studentInforamtionRep = new ReportPrint();
studentInforamtionRep.crystalReportViewer1.ReportSource = aLL_PUSH_STUDENTS;
studentInforamtionRep.ShowDialog();
Error:
That error code (17) means: "Server does not exist or access denied."
Hopefully, that should point you in the right direction.
I am trying to programmatically create a new database using SMO in C#. For this project, I do not want the .mdf/.ldf files placed in the default folder
"C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQL2008R2\MSSQL\DATA". I have not found anything on the web that tells how to modify the setting for the file location.
I get a failed operation exception when I run the following code:
Server srv = new Server(serverName.Text);
var db = new Database(srv, dbName.Text);
db.Create();
DataFile df = new DataFile(db.FileGroups["PRIMARY"],
dbName.Text, pathText.Text + dbName.Text + "_data.mdf");
df.Create();
LogFile lf = new LogFile(db, "Log01", pathText.Text + dbName.Text + "_log.ldf");
lf.Create();
The exception occurs at the df.Create(); line.
Any ideas?
I think this one answers the question for you.
Use SMO to Change SQL Server Database Default Locations
TTRider's Answer was:
You need to add information about Data and Log files explicitly:
TTRider's answer in the linked question points in the right direction, but is incomplete. Using some additional info from this post I was able to get it working (tested with SMO 2016 libraries).
private void CreateDatabase(string connectionString, string databaseName, string dataFilePath)
{
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
ServerConnection serverConnection = new ServerConnection(sqlConnection);
Server sqlServer = new Server(serverConnection);
Database smoDatabase = new Database(sqlServer, databaseName);
string dataFileName = string.Format("{0}_Data", databaseName);
string dataFileFullPath = Path.ChangeExtension(Path.Combine(dataFilePath, dataFileName), ".mdf");
string logFileName = string.Format("{0}_Log", databaseName);
string logFileFullPath = Path.ChangeExtension(Path.Combine(dataFilePath, logFileName), ".ldf");
FileGroup fileGroup = new FileGroup(smoDatabase, "PRIMARY");
smoDatabase.FileGroups.Add(fileGroup);
DataFile dataFile = new DataFile(fileGroup, dataFileName, dataFileFullPath);
dataFile.IsPrimaryFile = true;
fileGroup.Files.Add(dataFile);
LogFile logFile = new LogFile(smoDatabase, logFileName, logFileFullPath);
smoDatabase.LogFiles.Add(logFile);
smoDatabase.Create();
serverConnection.Disconnect();
}
}
Is there is any way by which we can rename a column in MS Access dynamically from C#?
I know its simple in SQL Server but which is the best approach to do the same in Access programmatically?
For an Access 2003 database file, you can use good old Jet DAO if your application is running as 32-bit:
// test data
string tableName = "Members";
string oldFieldName = "Photo";
string newFieldName = "Photograph";
// COM Reference required in C# project:
// Microsoft DAO 3.6 Object Library
//
var dbe = new DAO.DBEngine();
DAO.Database db = dbe.OpenDatabase(#"C:\Users\Public\mdbTest.mdb");
DAO.Field fld = db.TableDefs[tableName].Fields[oldFieldName];
fld.Name = newFieldName;
db.Close();
To operate on an .accdb file, or to perform the operation from a C# application that is running as 64-bit, the newer Access Database Engine (a.k.a "ACE") needs to be installed. Then the code would be:
// test data
string tableName = "Members";
string oldFieldName = "Photo";
string newFieldName = "Photograph";
// COM Reference required in C# project:
// Microsoft Office 14.0 Access Database Engine Object Library
//
var dbe = new Microsoft.Office.Interop.Access.Dao.DBEngine();
Microsoft.Office.Interop.Access.Dao.Database db = dbe.OpenDatabase(#"C:\Users\Public\accdbTest.accdb");
Microsoft.Office.Interop.Access.Dao.Field fld = db.TableDefs[tableName].Fields[oldFieldName];
fld.Name = newFieldName;
db.Close();