If the requirement is to run dynamic query on each row, you might get inside Script comoponent. Here is how you can grab SQL connection and run your queries;
Your connection is defined here on script component;
Edit Script…
public class ScriptMain : UserComponent
{
IDTSConnectionManager100 connMgr;
SqlConnection sqlConn;
SqlDataReader sqlReader;
public override void AcquireConnections(object Transaction)
{
connMgr = this.Connections.MyConn;
sqlConn = (SqlConnection)connMgr.AcquireConnection(null);
}
public override void ReleaseConnections()
{
//base.ReleaseConnections();
connMgr.ReleaseConnection(sqlConn);
}
public override void PreExecute()
{
base.PreExecute();
string KeyValue = Variables.vFileOutputString;
String vSqlStatement = "SELECT SqlStatement FROM [DataHub].[vw_LastStepForRglData] WHERE ADNumber = '" + KeyValue + "' ";
SqlCommand cmd = new SqlCommand(vSqlStatement, sqlConn);
sqlReader = cmd.ExecuteReader();
}
public override void PostExecute()
{
base.PostExecute();
sqlReader.Close();
}
public override void CreateNewOutputRows()
{
while (sqlReader.Read())
{
{
DynamicSqlBuffer.AddRow();
DynamicSqlBuffer.SqlStatement = sqlReader.GetString(0);
}
}
}
}
I ma using Dapper and SqlClient libraries ASP.NET Core and Console application to access data from Database.
SSIS application;
Here is how this will work in SQL Server Integration services;
ADO.NET Source
SELECT DocumentId, [FileName], [ContentType], [FileStreamCol]
FROM [dbo].[Document]
WHERE 1=1
SC-Transform data
This is a SSIS Transformation script component. This prepares the data that will be used later on in Export Column task;
Here is the code block;
private byte[] fileContent = null;
private string fileExtension = String.Empty;
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
//determine content type
switch (Row.ContentType)
{
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
case "application/vnd.ms-excel":
case "application/msexcel":
fileExtension = "xlsx";
break;
case "application/vnd.ms-excel.sheet.macroEnabled.12":
fileExtension = "xlsm";
break;
default:
break;
}
Row.fileContentName = String.Concat(Row.DocumentId, ".", fileExtension);
//A VB6 application saves excel files as BLOB objects. For download it subtracts one element from
//byte array. Following same dance here
var blobLength = Convert.ToInt32(Row.FileStreamCol.Length);
fileContent = Row.FileStreamCol.GetBlobData(0, blobLength - 1);
Row.fileContent.AddBlobData(fileContent);
}
Derived column transform;
Export column settings;
Checking Force Truncate overwrites extracted files.
This works fine but it’s reparative for configurations and logger and violates DRY principal. A good alternative is this;
Register these assemblies in your startup (startup.cs) to take advantage of dependency feature;
public void ConfigureServices(IServiceCollection services)
{
//Add functionality to inject IOptions<T>
services.AddOptions();
//Add our Config object so it can be injected
services.Configure<ConnectionString>(Configuration.GetSection("ConnectionStrings"));
services.Configure<AppConfig>(Configuration.GetSection("AppConfig"));
}
UPDATE @10/18/2022
For .NET 6, the configuration is;
builder.Services.AddOptions();
builder.Services.Configure<ConnectionString>(builder.Configuration.GetSection("ConnectionStrings"));
builder.Services.Configure<AppConfig>(builder.Configuration.GetSection("AppConfig"));
Create AppConfig POCO class;
public class AppConfig
{
public string ApplicationName { get; set; }
public string Version { get; set; }
}
Create a base class and declare these services as properties.
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using ConfigDemo.Models;
public abstract class BaseController<T> : Controller where T : BaseController<T>
{
private ILogger<T> _logger;
private IConfiguration _configuration;
private IOptions<ConnectionString> _appConnectionString;
private IOptions<AppConfig> _appConfiguration;
protected ILogger<T> Logger => _logger ?? (_logger = HttpContext?.RequestServices.GetService<ILogger<T>>());
protected IConfiguration Configuration => _configuration ?? (_configuration = HttpContext?.RequestServices.GetService<IConfiguration>());
protected IOptions<ConnectionString> AppConnectionString => _appConnectionString ?? (_appConnectionString = (HttpContext?.RequestServices.GetService<IOptions<ConnectionString>>()));
protected IOptions<AppConfig> AppConfiguration => _appConfiguration ?? (_appConfiguration = (HttpContext?.RequestServices.GetService<IOptions<AppConfig>>()));
protected string DisplayMessage
{
get { return TempData["DisplayMessage"] == null ? String.Empty : TempData["DisplayMessage"].ToString(); }
set { TempData["DisplayMessage"] = value; }
}
}
We have these values in our appsettings.json file that we would like to use in our application;
Finally create our first HomeController by inheriting from base controller to read config values;
public class HomeController : BaseController<HomeController>
{
//Navigate to URL, for example https://localhost:44320/home/simpleconfig
public string Index()
{
Logger.LogInformation("I am using dependency injection created in the base cotroller");
return "Navigate to URL to show an example";
}
//using configuration
public ViewResult SimpleConfig()
{
var configValue = Configuration.GetSection("AppConfig").GetChildren();
string result = configValue.Select(i => i.Value).Aggregate((i, j) => i + "," + j );
// generate the view
return View("Result",
(object)String.Format("Simple Config value: {0}", result));
}
//using strong type
public ViewResult ConfigValueFromConfig()
{
string configValue = AppConfiguration.Value.ApplicationName;
// generate the view
return View("Result",
(object)String.Format("App Config value: {0}", configValue));
}
Run your application and navigate to action method like this;
You can package the assembly into a nuget package, Create a Lib folder inside your solution to hold the nuget package, then, create a nuget.config file to set the package sources to include the Lib folder inside your solution.
The following links contains more details about creating nuget package and hosting it locally:
In the nuget package explorer add a lib folder, inside that add a .net folder dnxcore50 and add the above DLLs. Click on tools analyse package and save the nuget.
In the visual studio 2015 solution, you can refer local packages. Tools – Nuget Package Manager – Package Manager Settings – Package source add the local package path.
After which you will be able to add the nuget package using nuget package manager and select local package as source
The command-line interface (CLI) tools for Entity Framework Core perform design-time development tasks. For example, they create migrations, apply migrations, and generate code for a model based on an existing database.
dotnet ef can be installed as either a global or local tool. Most developers prefer installing dotnet ef as a global tool using the following command:
dotnet tool install –global dotnet-ef
Update the tool tool using the following command:
dotnet tool update –global dotnet-ef
Before you can use the tools on a specific project, you’ll need to add the Microsoft.EntityFrameworkCore.Design package to it.