Tuesday, November 12, 2019

Hosting .NET Core on Windows

Image result for .NET Core

Tuesday, November 20, 2018

.NET Core: Get IConfiguration object from appsettings.json without injection

IConfiguration Configuration = (IConfiguration)new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();

var connStr = Configuration["ConnectionString:Default"];

Friday, October 26, 2018

Generate Class from SELECT Statement for MySQL / MariaDb


select 'Table' into @table; #table name
select 'Database' into @schema; #database name
select concat('public class ',@table,'{') union
select concat('public ',tps.dest,' ',column_name,'{get;set;}') from  information_schema.columns c
join( #datatypes mapping
select 'char' as orign ,'string' as dest union all
select 'varchar' ,'string' union all
select 'longtext' ,'string' union all
select 'datetime' ,'DateTime?' union all
select 'text' ,'string' union all
select 'bit' ,'int?' union all
select 'bigint' ,'int?' union all
select 'int' ,'int?' union all
select 'double' ,'double?' union all
select 'decimal' ,'double?' union all
select 'date' ,'DateTime?' union all
select 'tinyint' ,'bool?'
) tps on c.data_type like tps.orign
where table_schema=@schema and table_name=@table union
select '}';

.NET Core MVC: Get values from Configuration file

The following code initially exists in the Startup.cs file:

public Startup(IConfiguration configuration)
{
Configuration = configuration;
}


This implies that the configuration will be injected in other classes found in your project. As a result, you can get the configuration from any class files.
Example: Accessing configuration in controller:
1. define a property of type IConfiguration in the class
2. inject IConfiguration parameter in your constructor
3. access the Configuration property defined from any method


public IConfiguration Configuration;

public HomeController(IConfiguration configuration)
{
Configuration = configuration;
}

public IActionResult Index()
{
ViewData["test"] = Configuration["Logging:LogLevel:Default"];


Sample appsettings.json file:

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "ConnectionStrings": {
    "DefaultConnection": "server=localhost;port=3307;database=test;user=root;password=;Persist Security Info=False;SSL Mode =None;"
  },
  "AllowedHosts": "*"
}

Tuesday, October 23, 2018

Connect to a Microsoft Access Database using .NET Core


Install the following packages via NuGET:
  • Microsoft.Office.Interop.Access
  • Microsoft.Office.Interop.Access.Dao

Include the assemblies as follows:

using Microsoft.Office.Interop.Access;

using Microsoft.Office.Interop.Access.Dao;


Sample of retrieving data in Main program:

var accessApp = new Application();
accessApp.OpenCurrentDatabase(@"C:\Users\OlivierSin\Desktop\RAS.mdb", false, "******");
Database cdb = accessApp.CurrentDb();

Microsoft.Office.Interop.Access.Dao.Recordset result = cdb.OpenRecordset("SELECT username FROM users", Microsoft.Office.Interop.Access.Dao.RecordsetTypeEnum.dbOpenSnapshot);

while(!result.EOF)
{
Console.WriteLine(result.Fields["username"].Value);
result.MoveNext();
}
result.Close();
accessApp.Quit();