Msb.DatabaseContext 3.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Msb.DatabaseContext --version 3.0.0                
NuGet\Install-Package Msb.DatabaseContext -Version 3.0.0                
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="3.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Msb.DatabaseContext --version 3.0.0                
#r "nuget: Msb.DatabaseContext, 3.0.0"                
#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=3.0.0

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

Configuration in ASP.NET

Add the connection string in Web.Config file

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

Create a Class like DataLayer.cs For MSSQL

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

Create a Class like DataLayer.cs For PostgreSQL

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

Code Example

class PopertyModel
    {
        public int BookingNo { get; set; }
        [PhysicalName("user_name")] //for database table column 
        public string UserName { get; set; }

        [PhysicalName("get_greeting")]
        public string Name { get; set; }
    }
    public class HomeController : Controller
    {
        protected DataLayer _dataLayer =new DataLayer();

        public ActionResult Index()
        {

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

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

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


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

            var data4 = _dataLayer.Execute("SPName").FirstOrDefault();
            var data5 = _dataLayer.Execute("SPName").ToList();

            SQLParameter parameter = new SQLParameter();
            parameter.Add("@BookingNo", "or125");
            var list6 = _dataLayer.Execute("SPName", parameter).ToList();

            DataTable dataTable = Utility.ConvertObjectToDataTable(list1);

            return View();
        }


        // asynchronous
        public async Task<ActionResult> IndexAsync()
        {

            var data1 = await _dataLayer.ExecuteAsync("SPName", new { BookingNo = "45,56" });
            List<PopertyModel> list1 = await _dataLayer.ExecuteAsync("SPName", new { BookingNo = "45,56" }).ToList<PopertyModel>();

            PopertyModel data2 = await _dataLayer.ExecuteAsync("SPName").FirstOrDefault<PopertyModel>();
            List<PopertyModel> list2 = await _dataLayer.ExecuteAsync("SPName").ToList<PopertyModel>();

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


            var data3 = await _dataLayer.ExecuteAsync("SPName", new { BookingNo = "45,56" }).FirstOrDefault();
            var list3 = await _dataLayer.ExecuteAsync("SPName", new { BookingNo = "45,56" }).ToList();

            var data4 = await _dataLayer.ExecuteAsync("SPName").FirstOrDefault();
            var data5 = await _dataLayer.ExecuteAsync("SPName").ToList();

            SQLParameter parameter = new SQLParameter();
            parameter.Add("@BookingNo", "or125");
            var list6 = await _dataLayer.ExecuteAsync("SPName", parameter).ToList();

            DataTable dataTable = Utility.ConvertObjectToDataTable(list1);


            SQLParameter parameter = new SQLParameter();
            parameter.Add("name", "MSBWP");
            var dataSet = await _dataLayer.ExecuteAsync<object>("get_greeting", parameter).ToList();


            return View();
        }
}

Configuration in ASP.NET CORE

add the line in appsettings.json file

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

Create a Class like DataLayer.cs For MSSQL

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

Create a Class like DataLayer.cs 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

  class PopertyModel
    {
        public int BookingNo { get; set; }
        [PhysicalName("user_name")] //for database table column 
        public string UserName { get; set; }

        [PhysicalName("get_greeting")]
        public string Name { get; set; }
    }
    public class HomeController : Controller
    {

        private readonly DataLayer _dataLayer;

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

        public IActionResult Index()
        {

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

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

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


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

            var data4 = _dataLayer.Execute("SPName").FirstOrDefault();
            var data5 = _dataLayer.Execute("SPName").ToList();

            SQLParameter parameter = new SQLParameter();
            parameter.Add("@BookingNo", "or125");
            var list6 = _dataLayer.Execute("SPName", parameter).ToList();

            DataTable dataTable = Utility.ConvertObjectToDataTable(list1);

            return View();
        }


        public async Task<IActionResult> IndexAsync()
        {

            var data1 = await _dataLayer.ExecuteAsync("SPName", new { BookingNo = "45,56" });
            List<PopertyModel> list1 = await _dataLayer.ExecuteAsync("SPName", new { BookingNo = "45,56" }).ToList<PopertyModel>();

            PopertyModel data2 = await _dataLayer.ExecuteAsync("SPName").FirstOrDefault<PopertyModel>();
            List<PopertyModel> list2 = await _dataLayer.ExecuteAsync("SPName").ToList<PopertyModel>();

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


            var data3 = await _dataLayer.ExecuteAsync("SPName", new { BookingNo = "45,56" }).FirstOrDefault();
            var list3 = await _dataLayer.ExecuteAsync("SPName", new { BookingNo = "45,56" }).ToList();

            var data4 = await _dataLayer.ExecuteAsync("SPName").FirstOrDefault();
            var data5 = await _dataLayer.ExecuteAsync("SPName").ToList();

            SQLParameter parameter = new SQLParameter();
            parameter.Add("@BookingNo", "or125");
            var list6 = await _dataLayer.ExecuteAsync("SPName", parameter).ToList();

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

    }
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.