banner



How To Add Dal File In Windows Form Application

Introduction

In this article, I will discuss the complete procedure of crud operation in Asp.Cyberspace Core five Web API projection. Nosotros will send the complete JSON object to the POST endpoint and then we will update the data in the database using PUT Endpoint. Basically, in this project we will perform the consummate Crud operation using Asp.Net 5 Architecture. We volition perform the following operation like POST PUT DELETE GET operations. POST Method is used for sending the JSON Data to database PUT Method is used to update the Data in Database DELETE Method is used to delete the information from the database either based on Id OR Compete Data from Database Go Method is used for Getting all the record from the database either on the bases of Id or All Data in the form of JSON objects. I will apply the repository pattern in this project CRUDAspNetCore5Web API

Resources can be downloaded from Article Resource Department.

Projection Setup

Add together Asp.Cyberspace Core five Projection

Outset the Asp.Internet Core project using the Visual Studio in Asp.cyberspace Core with the latest version .Net Core Version 5.

CRUD Operation

CRUD Operation

Add together DAL_CRUD Asp.cyberspace Core Course Library

Add the .Net class Library for DAL (Data Access Layer) in Information Access Layer we will connect our ORM With Database and we will define some methods for doing our all logic in the business access layer.

CRUD Operation

Add together BAL_CRUD Asp.net Cadre Course Library

Add the .Internet class Library for DAL (Business Access Layer). In the Business organisation Access layer, we will use the basic structure of our ORM that is already defined in the information admission layer. In our business organisation admission layer, we volition perform our all logic about our Grime Operations similar Mail GET PUT AND Delete.

CRUD Operation

CRUD Operation

Information Admission Layer

In the Data Admission layer we will perform the following steps,

Application Db Context

First, add the connection string in the awarding Setting.JSON file and so connect the connection cord with Application Db Class and then call the string connection in Startup. cs class for establishing the connexion with the database.

CRUD Operation

  1. using  CRUD_DAL.Models;
  2. using  Microsoft.EntityFrameworkCore;
  3. using  System;
  4. using  Organization.Collections.Generic;
  5. using  System.Text;
  6. namespace  CRUD_DAL.Information
  7. {
  8. public grade  ApplicationDbContext : DbContext
  9.     {
  10. public  ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
  11.         {
  12.         }
  13. protected  override void  OnModelCreating(ModelBuilder builder)
  14.         {
  15.             base.OnModelCreating(builder);
  16.         }
  17. public  DbSet<Person> Persons { get; set; }
  18.     }
  19. }

Application Setting.JSON file

  1. {
  2. "Logging" : {
  3. "LogLevel" : {
  4. "Default" : "Data" ,
  5. "Microsoft" : "Warning" ,
  6. "Microsoft.Hosting.Lifetime" : "Information"
  7.     }
  8.   },
  9. "AllowedHosts" : "*" ,
  10. "ConnectionStrings" : {
  11. "DefaultConnection" : "Server=SARDARMUDASSARA\\SQLEXPRESS;Database=CRUDAspNetCore5WebAPI;Trusted_Connection=True"
  12.   }
  13. }

Start-Up File

  1. using  CRUD_BAL.Service;
  2. using  CRUD_DAL.Information;
  3. using  CRUD_DAL.Interface;
  4. using  CRUD_DAL.Models;
  5. using  CRUD_DAL.Repository;
  6. using  Microsoft.AspNetCore.Builder;
  7. using  Microsoft.AspNetCore.Hosting;
  8. using  Microsoft.AspNetCore.HttpsPolicy;
  9. using  Microsoft.AspNetCore.Mvc;
  10. using  Microsoft.EntityFrameworkCore;
  11. using  Microsoft.Extensions.Configuration;
  12. using  Microsoft.Extensions.DependencyInjection;
  13. using  Microsoft.Extensions.Hosting;
  14. using  Microsoft.Extensions.Logging;
  15. using  Microsoft.OpenApi.Models;
  16. using  Organisation;
  17. using  System.Collections.Generic;
  18. using  Arrangement.Linq;
  19. using  Organization.Threading.Tasks;
  20. namespace  CRUDAspNetCore5WebAPI
  21. {
  22. public form  Startup
  23.     {
  24. private readonly  IConfiguration _configuration;
  25. public  Startup(IConfiguration configuration)
  26.         {
  27.             _configuration = configuration;
  28.         }
  29. public void  ConfigureServices(IServiceCollection services)
  30.         {
  31.             services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(_configuration.GetConnectionString("DefaultConnection" )));
  32.             services.AddControllers();
  33.             services.AddHttpClient();
  34.             services.AddTransient<IRepository<Person>, RepositoryPerson>();
  35.             services.AddTransient<PersonService, PersonService>();
  36.             services.AddControllers();
  37.             services.AddSwaggerGen(c =>
  38.             {
  39.                 c.SwaggerDoc("v1" , new  OpenApiInfo { Title = "CRUDAspNetCore5WebAPI" , Version = "v1"  });
  40.             });
  41.         }
  42. public void  Configure(IApplicationBuilder app, IWebHostEnvironment env)
  43.         {
  44. if  (env.IsDevelopment())
  45.             {
  46.                 app.UseDeveloperExceptionPage();
  47.                 app.UseSwagger();
  48.                 app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json" , "CRUDAspNetCore5WebAPI v1" ));
  49.             }
  50.             app.UseHttpsRedirection();
  51.             app.UseRouting();
  52.             app.UseAuthorization();
  53.             app.UseEndpoints(endpoints =>
  54.             {
  55.                 endpoints.MapControllers();
  56.             });
  57.         }
  58.     }
  59. }

Application Db Context Grade

  1. using  CRUD_DAL.Models;
  2. using  Microsoft.EntityFrameworkCore;
  3. using  System;
  4. using  System.Collections.Generic;
  5. using  Organisation.Text;
  6. namespace  CRUD_DAL.Data
  7. {
  8. public class  ApplicationDbContext : DbContext
  9.     {
  10. public  ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base (options)
  11.         {
  12.         }
  13. protected override void  OnModelCreating(ModelBuilder builder)
  14.         {
  15. base .OnModelCreating(builder);
  16.         }
  17. public  DbSet<Person> Persons { go ; set ; }
  18.     }
  19. }

Model

Add the model grade in which define the entity for the database and his attributes like Id Name, and Person Telephone No.

CRUD Operation

  1. using  Arrangement;
  2. using  System.Collections.Generic;
  3. using  Organization.ComponentModel.DataAnnotations;
  4. using  System.ComponentModel.DataAnnotations.Schema;
  5. using  System.Text;
  6. namespace  CRUD_DAL.Models
  7. {
  8.     [Tabular array("Persons" , Schema = "dbo" )]
  9. public class  Person
  10.     {
  11.         [Cardinal]
  12.         [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  13. public int  Id { become ; set ; }
  14.         [Required]
  15.         [Display(Name ="UserName" )]
  16. public cord  UserName { go ; set ; }
  17.         [Required]
  18.         [Brandish(Proper name ="UserPassword" )]
  19. public string  UserPassword { become ; set ; }
  20.         [Required]
  21.         [Display(Proper noun ="UserEmail" )]
  22. public string  UserEmail { go ; set ; }
  23.         [Required]
  24.         [Display(Proper name ="CreatedOn" )]
  25. public  DateTime CreatedOn { get ; set ; } = DateTime.Now;
  26.         [Required]
  27.         [Display(Name ="IsDeleted" )]
  28. public bool  IsDeleted { go ; set ; } = false ;
  29.     }
  30. }

Migration

Now Add the migration for the cosmos of the database and afterwards adding migration Update the database. Select the DAL projection for the migration of Data

CRUD Operation

CRUD Operation

CRUD Operation

Interface

Add together the interface binder and then add the Interface in the information admission layer In Interface and Define the Methods that you want to perform in the repository blueprint.

  1. using  System;
  2. using  System.Collections.Generic;
  3. using  System.Text;
  4. using  System.Threading.Tasks;
  5. namespace  CRUD_DAL.Interface
  6. {
  7. public interface  IRepository<T>
  8.     {
  9. public  Task<T> Create(T _object);
  10. public void  Update(T _object);
  11. public  IEnumerable<T> GetAll();
  12. public  T GetById( int  Id);
  13. public void  Delete(T _object);
  14.     }
  15. }

Repository

Add the Repository Person Class Which Implements the Interface and performs the office defined in the interface like getting All Data, Delete All Data Update Information, Update data past userId similar this.

CRUD Operation

  1. using  CRUD_DAL.Data;
  2. using  CRUD_DAL.Interface;
  3. using  CRUD_DAL.Models;
  4. using  System;
  5. using  System.Collections.Generic;
  6. using  System.Linq;
  7. using  Arrangement.Text;
  8. using  Organisation.Threading.Tasks;
  9. namespace  CRUD_DAL.Repository
  10. {
  11. public class  RepositoryPerson : IRepository<Person>
  12.     {
  13.         ApplicationDbContext _dbContext;
  14. public  RepositoryPerson(ApplicationDbContext applicationDbContext)
  15.         {
  16.             _dbContext = applicationDbContext;
  17.         }
  18. public  async Chore<Person> Create(Person _object)
  19.         {
  20.             var obj = await _dbContext.Persons.AddAsync(_object);
  21.             _dbContext.SaveChanges();
  22. return  obj.Entity;
  23.         }
  24. public void  Delete(Person _object)
  25.         {
  26.             _dbContext.Remove(_object);
  27.             _dbContext.SaveChanges();
  28.         }
  29. public  IEnumerable<Person> GetAll()
  30.         {
  31. endeavour
  32.             {
  33. return  _dbContext.Persons.Where(x => 10.IsDeleted == faux ).ToList();
  34.             }
  35. take hold of  (Exception ee)
  36.             {
  37. throw ;
  38.             }
  39.         }
  40. public  Person GetById( int  Id)
  41.         {
  42. return  _dbContext.Persons.Where(x => x.IsDeleted == false  && x.Id == Id).FirstOrDefault();
  43.         }
  44. public void  Update(Person _object)
  45.         {
  46.             _dbContext.Persons.Update(_object);
  47.             _dbContext.SaveChanges();
  48.         }
  49.     }
  50. }

Business concern Access Layer

Add Project Reference of Data Access Layer

Before Staring the Work on Person Service Class Kickoff Add the Reference of Data Access grade library project in the Business access layer.

CRUD Operation

CRUD Operation

Person Service

In-Person Service We will apply the Data Admission Class Library in which we have already defined the Methods similar Update Data Delete Data etc. But in business access, we will write all the business organization logic like getting all the data with a specific Id, update the complete object using userId, Soft Deletion of data.

Add together the service folder in the business access layer then add the person service grade.

CRUD Operation

  1. using  CRUD_DAL.Interface;
  2. using  CRUD_DAL.Models;
  3. using  System;
  4. using  Organization.Collections.Generic;
  5. using  System.Linq;
  6. using  Organisation.Text;
  7. using  Arrangement.Threading.Tasks;
  8. namespace  CRUD_BAL.Service
  9. {
  10. public class  PersonService
  11.     {
  12. private readonly  IRepository<Person> _person;
  13. public  PersonService(IRepository<Person> perosn)
  14.         {
  15.             _person = perosn;
  16.         }
  17. public  IEnumerable<Person> GetPersonByUserId( cord  UserId)
  18.         {
  19. return  _person.GetAll().Where(ten => ten.UserEmail == UserId).ToList();
  20.         }
  21. public  IEnumerable<Person> GetAllPersons()
  22.         {
  23. try
  24.             {
  25. return  _person.GetAll().ToList();
  26.             }
  27. catch  (Exception)
  28.             {
  29. throw ;
  30.             }
  31.         }
  32. public  Person GetPersonByUserName( string  UserName)
  33.         {
  34. return  _person.GetAll().Where(x => x.UserEmail == UserName).FirstOrDefault();
  35.         }
  36. public  async Task<Person> AddPerson(Person Person)
  37.         {
  38. return  await _person.Create(Person);
  39.         }
  40. public bool  DeletePerson( string  UserEmail)
  41.         {
  42. try
  43.             {
  44.                 var DataList = _person.GetAll().Where(10 => x.UserEmail == UserEmail).ToList();
  45. foreach  (var detail in  DataList)
  46.                 {
  47.                     _person.Delete(item);
  48.                 }
  49. return true ;
  50.             }
  51. catch  (Exception)
  52.             {
  53. return true ;
  54.             }
  55.         }
  56. public bool  UpdatePerson(Person person)
  57.         {
  58. try
  59.             {
  60.                 var DataList = _person.GetAll().Where(ten => x.IsDeleted !=true ).ToList();
  61. foreach  (var item in  DataList)
  62.                 {
  63.                     _person.Update(item);
  64.                 }
  65. return truthful ;
  66.             }
  67. catch  (Exception)
  68.             {
  69. render true ;
  70.             }
  71.         }
  72.     }
  73. }

API in Asp.Internet Core five Web API

Add together Project Reference of Business concern Access Layer

Before Staring in API Controllers commencement add together the business access layer BAL Class library project reference in API Project.

CRUD Operation

CRUD Operation

Add the Transient in Startup Class

Modify the startup class like add the transient in the Startup class for adding the dependency injection transient services created whenever they are injected or requested.

  1. using  CRUD_BAL.Service;
  2. using  CRUD_DAL.Data;
  3. using  CRUD_DAL.Interface;
  4. using  CRUD_DAL.Models;
  5. using  CRUD_DAL.Repository;
  6. using  Microsoft.AspNetCore.Builder;
  7. using  Microsoft.AspNetCore.Hosting;
  8. using  Microsoft.AspNetCore.HttpsPolicy;
  9. using  Microsoft.AspNetCore.Mvc;
  10. using  Microsoft.EntityFrameworkCore;
  11. using  Microsoft.Extensions.Configuration;
  12. using  Microsoft.Extensions.DependencyInjection;
  13. using  Microsoft.Extensions.Hosting;
  14. using  Microsoft.Extensions.Logging;
  15. using  Microsoft.OpenApi.Models;
  16. using  System;
  17. using  Arrangement.Collections.Generic;
  18. using  System.Linq;
  19. using  System.Threading.Tasks;
  20. namespace  CRUDAspNetCore5WebAPI
  21. {
  22. public class  Startup
  23.     {
  24. private readonly  IConfiguration _configuration;
  25. public  Startup(IConfiguration configuration)
  26.         {
  27.             _configuration = configuration;
  28.         }
  29. public void  ConfigureServices(IServiceCollection services)
  30.         {
  31.             services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(_configuration.GetConnectionString("DefaultConnection" )));
  32.             services.AddControllers();
  33.             services.AddHttpClient();
  34.             services.AddTransient<IRepository<Person>, RepositoryPerson>();
  35.             services.AddTransient<PersonService, PersonService>();
  36.             services.AddControllers();
  37.             services.AddSwaggerGen(c =>
  38.             {
  39.                 c.SwaggerDoc("v1" , new  OpenApiInfo { Title = "CRUDAspNetCore5WebAPI" , Version = "v1"  });
  40.             });
  41.         }
  42. public void  Configure(IApplicationBuilder app, IWebHostEnvironment env)
  43.         {
  44. if  (env.IsDevelopment())
  45.             {
  46.                 app.UseDeveloperExceptionPage();
  47.                 app.UseSwagger();
  48.                 app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json" , "CRUDAspNetCore5WebAPI v1" ));
  49.             }
  50.             app.UseHttpsRedirection();
  51.             app.UseRouting();
  52.             app.UseAuthorization();
  53.             app.UseEndpoints(endpoints =>
  54.             {
  55.                 endpoints.MapControllers();
  56.             });
  57.         }
  58.     }
  59. }

API Controller

In API Controller We will Ascertain Our GET PUT POST AND DELETE Endpoints for achieving our CRUD operation Goals.

CRUD Operation

CRUD Operation

CRUD Operation

CRUD Operation

CRUD Operation

  1. using  CRUD_BAL.Service;
  2. using  CRUD_DAL.Interface;
  3. using  CRUD_DAL.Models;
  4. using  Microsoft.AspNetCore.Http;
  5. using  Microsoft.AspNetCore.Mvc;
  6. using  Newtonsoft.Json;
  7. using  Organisation;
  8. using  System.Collections.Generic;
  9. using  System.Linq;
  10. using  Organization.Threading.Tasks;
  11. namespace  CRUDAspNetCore5WebAPI.Controllers
  12. {
  13.     [Route("api/[controller]" )]
  14.     [ApiController]
  15. public form  PersonDetailsController : ControllerBase
  16.     {
  17. private readonly  PersonService _personService;
  18. private readonly  IRepository<Person> _Person;
  19. public  PersonDetailsController(IRepository<Person> Person, PersonService ProductService)
  20.         {
  21.             _personService = ProductService;
  22.             _Person = Person;
  23.         }
  24.         [HttpPost("AddPerson" )]
  25. public  async Task<Object> AddPerson([FromBody] Person person)
  26.         {
  27. try
  28.             {
  29.                 await _personService.AddPerson(person);
  30. return true ;
  31.             }
  32. take hold of  (Exception)
  33.             {
  34. render faux ;
  35.             }
  36.         }
  37.         [HttpDelete("DeletePerson" )]
  38. public bool  DeletePerson( string  UserEmail)
  39.         {
  40. try
  41.             {
  42.                 _personService.DeletePerson(UserEmail);
  43. return true ;
  44.             }
  45. catch  (Exception)
  46.             {
  47. return fake ;
  48.             }
  49.         }
  50.         [HttpPut("UpdatePerson" )]
  51. public bool  UpdatePerson(Person Object)
  52.         {
  53. try
  54.             {
  55.                 _personService.UpdatePerson(Object);
  56. return truthful ;
  57.             }
  58. catch  (Exception)
  59.             {
  60. render false ;
  61.             }
  62.         }
  63.         [HttpGet("GetAllPersonByName" )]
  64. public  Object GetAllPersonByName( string  UserEmail)
  65.         {
  66.             var data = _personService.GetPersonByUserName(UserEmail);
  67.             var json = JsonConvert.SerializeObject(data, Formatting.Indented,
  68. new  JsonSerializerSettings()
  69.                 {
  70.                     ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
  71.                 }
  72.             );
  73. render  json;
  74.         }
  75.         [HttpGet("GetAllPersons" )]
  76. public  Object GetAllPersons()
  77.         {
  78.             var information = _personService.GetAllPersons();
  79.             var json = JsonConvert.SerializeObject(data, Formatting.Indented,
  80. new  JsonSerializerSettings()
  81.                 {
  82.                     ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
  83.                 }
  84.             );
  85. return  json;
  86.         }
  87.     }
  88. }

TESTING THE API

for Testing of API, I will Discuss the consummate procedure in a separate commodity

Project Resource Guideline

  • First, download the given Project from my GitHub repository / from article resource.
  • Open the project using visual studio
  • Go to parcel managing director panel
  • Add the migration
  • Update the database
  • Run the project
  • Enjoy the beauty of web service API with swagger UI

"Happy API Evolution"

Determination

As a software engineer, my opinion about RESTful web services is that it is a lightweight, maintainable, and scalable service that is built on the Rest architecture. RESTful compages provides us the best manner to develop cross platforms applications using the Asp.net Core Web API. I suggest all the online communities that develop your Web application using RESTfull Spider web compages and follow the best practices of coding for high efficiency of the application.

How To Add Dal File In Windows Form Application,

Source: https://www.c-sharpcorner.com/article/crud-operation-in-asp-net-core-5-web-api/

Posted by: bastarachemeself.blogspot.com

0 Response to "How To Add Dal File In Windows Form Application"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel