Ask a question
Limited-Time Offer: 50% off IBM TechXchange Conference 2025
IBM’s largest technical learning event is back October 6-9 in Orlando, FL
This blog will help you to get started on ADO .NET Core MVC Web Application to connect and perform DML operations against IBM Data Servers.
Prerequisites:
Steps to follow to create an ADO .NET Core MVC Web Application to access IBM Data Servers:
1. Open Visual Studio 2017 and select File->New->Project and choose 'Web' under templates and select 'ASP .NET Core Web Application' and give a name to your project say 'demoNetCoreIBMDB' and click on OK.
4. Right Click on the project and select 'Edit demoNetCoreIBMDB.csproj' and add the following PackageReferences under 'ItemGroup' element in the csproj
<PackageReference Include="IBM.Data.DB2.Core" Version="1.2.2.100" />
<PackageReference Include="IBM.EntityFrameworkCore" Version="1.2.2.100" />
If you are planning to run the application on Linux, then add the following package reference in csproj:
<PackageReference Include="IBM.Data.DB2.Core-lnx" Version="1.2.2.100" />
<PackageReference Include="IBM.EntityFrameworkCore-lnx" Version="1.2.2.100" />
5. Right click on the Models folder and add two class files and name them as 'Blog.cs' and 'BloggingContext.cs' respectively.
6. Open the Blog.cs file and copy paste the following content into that file :
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace demoNetCoreIBMDB.Models { public class Blog { public Blog() { } public int BlogId { get; set; } public string Url { get; set; } } }
7. Open the BloggingContext.cs file and copy paste the following content into that file:
using Microsoft.EntityFrameworkCore; namespace demoNetCoreIBMDB.Models { public class BloggingContext : DbContext { public BloggingContext(DbContextOptions options) : base(options) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Blog>(entity => { entity.Property(e => e.Url).IsRequired(); }); } public virtual DbSet<Blog> Blog { get; set; } } }
8. Right click on the Controllers folder and select Add ->Controller , and in the next screen choose MVC Controller - Empty and name it as 'BlogsController.cs'
9. Open BlogsController.cs file and copy paste the below code:
using Microsoft.AspNetCore.Mvc; using demoNetCoreIBMDB.Models; using System.Linq; namespace demoNetCoreIBMDB.Controllers { public class BlogsController : Controller { private BloggingContext _context; public BlogsController(BloggingContext context) { _context = context; } public IActionResult Index() { return View(_context.Blog.ToList()); } } }
10. Right click on Views folder and create a new folder named 'Blogs'
12. Open the 'Views/Blogs/Index.cshtml' page and copy paste the below content:
@model IEnumerable<demoNetCoreIBMDB.Models.Blog> @{ ViewBag.Title = "Blogs"; } <h2>Blogs</h2> <table class="table"> <tr> <th>Id</th> <th>Url</th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.BlogId) </td> <td> @Html.DisplayFor(modelItem => item.Url) </td> </tr> } </table>
13. Open the Startup.cs file and
public void ConfigureServices(IServiceCollection services) { var connection = "DATABASE=dbname;SERVER=hostname:port;UID=username;PWD=password;"; // For SSL, use the below connection string format // var connection = "DATABASE=dbname;SERVER=hostname:sslport;UID=username;PWD=password;Security=SSL;"; services.AddDbContext<BloggingContext>(options => options.UseDb2(connection,p=>p.SetServerInfo(IBMDBServerType.<servertype>, IBMDBServerVersion.<serverversion>))); services.AddMvc(); }
app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Blogs}/{action=Index}/{id?}" ) ; });
14. Now build the solution and launch the web application and navigate to Blogs page to get the following output:.
Sample: http://localhost:52123/BlogsBlogsId Url1 https://console.ng.bluemix.net/docs/cfapps/starter_app_usage.html2 https://www.ibm.com/developerworks/community/forums/html/forum?id=11111111-0000-0000-0000-0000000004673 http://blogs.msdn.com/dotnet
Before launching the application on Linux, set the LD_LIBRARY_PATH either to:
<Application_Output_directory/clidriver/lib> for bin-deployment scenarios
OR
$HOME/.nuget/packages/ibm.data.db2.core-lnx/1.2.2.100/build/clidriver/lib
Sample Table creation script used in this example :
For more information about server type setting, please go through this link
To get started on developing ASP .NET Core MVC WEB Applications using IBM Data Server driver on MacOS, please refer this link.