Msb.DatabaseContext 2.0.6

There is a newer version of this package available.
See the version list below for details.
dotnet add package Msb.DatabaseContext --version 2.0.6                
NuGet\Install-Package Msb.DatabaseContext -Version 2.0.6                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Msb.DatabaseContext" Version="2.0.6" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Msb.DatabaseContext --version 2.0.6                
#r "nuget: Msb.DatabaseContext, 2.0.6"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Msb.DatabaseContext as a Cake Addin
#addin nuget:?package=Msb.DatabaseContext&version=2.0.6

// Install Msb.DatabaseContext as a Cake Tool
#tool nuget:?package=Msb.DatabaseContext&version=2.0.6                

Configuration in ASP.NET

Add the connection string in Web.Config file


  <appSettings>
   
   <add key="ConnectionString" value="Your database connection string"/>
  </appSettings>

Next Create a Class like DataLayer.cs For MSSQL

public class DataLayer : DatabaseContext
    {
        public DataLayer() : base()
        {
            ConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
        }
    }

DataLayer.cs For PostgreSQL

public class DataLayer : DatabaseContextNpgsql
    {
        public DataLayer() : base()
        {
            ConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
        }
    }

Code Example

    public class HomeController : Controller
    {

       private readonly DatabaseContext database = new DatabaseContext();
        public ActionResult Index()
        {

            ResultData data1 = database.Execute("SPName", new { BookingNo = "45,56" }).FirstOrDefault<ResultData>();
            List<ResultData> list1 = database.Execute("SPName", new { BookingNo = "45,56" }).ToList<ResultData>();

            ResultData data2 = database.Execute("SPName").FirstOrDefault<ResultData>();
            List<ResultData> list2 = database.Execute("SPName").ToList<ResultData>();

            string scaler = database.Execute<object,string>("SP_MSB",new { V=450 });


            var data1 = database.Execute("SPName", new { BookingNo = "45,56" }).FirstOrDefault();
            var list1 = database.Execute("SPName", new { BookingNo = "45,56" }).ToList();

            var data2 = database.Execute("SPName").FirstOrDefault();
            var list2 = database.Execute("SPName").ToList();
            
            
            //New feature
            SQLParameter parameter = new SQLParameter();
            parameter.Add("@BookingNo", "or125");
            var list1 = database.Execute("SPName", parameter).ToList();


            //Convert class object to DataTable

            DataTable dataTable= Utility.ConvertObjectToDataTable(list1);


            return View();
        }
}

##Note :

ResultData is class

Configuration in ASP.NET CORE

add the line in appsettings.json file

 "ConnectionStrings": {
    "ConnectionString": "Data Source=Your connenction string"
  }

Next Create a Class like DataLayer.cs

public class DataLayer : DatabaseContext
    {
        public DataLayer(IConfiguration iConfiguration)
        {
            ConnectionString = iConfiguration.GetConnectionString("ConnectionString");
        }
    }

For PostgreSQL

public class DataLayer : DatabaseContextNpgsql
    {
        public DataLayer(IConfiguration iConfiguration)
        {
            ConnectionString = iConfiguration.GetConnectionString("ConnectionString");
        }
    }

if you used another application(Desktop,Console)

public class DataLayer : DatabaseContext
    {
        public DataLayer()
        {
            ConnectionString = "Your connenction string";
        }
    }

Add new line on ConfigureServices methon in Startup.cs/Program.cs file

services.AddSingleton<DataLayer>();
//if used DOT NET  6 version
builder.Services.AddSingleton<DataLayer>();

Example Code

 public class HomeController : Controller
    {

          private readonly DataLayer _dataLayer;

        public HomeController(DataLayer dataLayer)
        {
            _dataLayer = dataLayer;
        }

        public IActionResult Index()
        {
            ResultData data1 = _dataLayer.Execute("SPName", new { BookingNo = "45,56" }).FirstOrDefault<ResultData>();
            List<ResultData> list1 = _dataLayer.Execute("SPName", new { BookingNo = "45,56" }).ToList<ResultData>();

            ResultData data2 = _dataLayer.Execute("SPName").FirstOrDefault<ResultData>();
            List<ResultData> list2 = _dataLayer.Execute("SPName").ToList<ResultData>();

            string scaler = _dataLayer.Execute<object,string>("SP_MSB",new { V=450 });


            var data1 = _dataLayer.Execute("SPName", new { BookingNo = "45,56" }).FirstOrDefault();
            var list1 = _dataLayer.Execute("SPName", new { BookingNo = "45,56" }).ToList();

            var data2 = _dataLayer.Execute("SPName").FirstOrDefault();
            var list2 = _dataLayer.Execute("SPName").ToList();
            
            //New feature
            SQLParameter parameter = new SQLParameter();
            parameter.Add("@BookingNo", "or125");
            var list1 = _dataLayer.Execute("SPName", parameter).ToList();


            //Convert class object to DataTable

            DataTable dataTable= Utility.ConvertObjectToDataTable(list1);
        }
}

Note :

ResultData is class

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.