- Michael Kench
- PowerBuilder
- Wednesday, 8 May 2019 07:18 AM UTC
Hi PB Team,
I am trying to get my head around what the benefits would be for using PB C# objects in a future .net core 2 web site being planned. I am planning on using razor pages without using the entity framework. I am not a fan of LINQ so was planning on sticking with SQL with something like Dapper.
However it appears using Dapper it would involve writing insert/update statements for all rows whether they are changed or not. I'm assuming this is not the case with PB data/model store on posts?
Below is an example code behind page for a editable multi row form I have been testing with for proof of concept. I would be interested in how below would change with PB objects?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Configuration;
using RazorPagesExample.Entity;
using System.Data.SqlClient;
using Dapper;
namespace RazorPagesExample.Pages
{
public class multirowformModel : PageModel
{
public class Proddaplocal
{
public int? Id { get; set; }
public string Name { get; set; }
public string Model { get; set; }
public int Price { get; set; }
public string Country { get; set; }
}
[BindProperty]
public List products { get; set; }
IConfiguration config;
public multirowformModel(IConfiguration configuration)
{
if (configuration != null)
{
config = configuration;
}
}
public void OnGet()
{
var connectionString = config.GetSection("ConnectionStrings").GetSection("ProductContext").Value;
using (var con = new SqlConnection(connectionString))
{
try
{
con.Open();
var query = "SELECT Id,Name,Model,Price,Country FROM Product";
products = con.Query(query).ToList();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
}
public IActionResult OnPostUpdate()
{
var data = products;
if (ModelState.IsValid)
{
var connectionString = config.GetSection("ConnectionStrings").GetSection("ProductContext").Value;
var count = 0;
using (var con = new SqlConnection(connectionString))
{
try
{
con.Open();
foreach (var item in data)
{
if (item.Id == null)
{
var query = "INSERT INTO Product(Name, Model, Price, Country) VALUES(@Name, @Model, @Price, @Country); SELECT CAST(SCOPE_IDENTITY() as INT);";
count = con.Execute(query, item);
}
else
{
var query = "UPDATE Product SET Name = @Name, Model = @Model, Price = @Price, Country = @Country WHERE Id = @Id";
count = con.Execute(query, item);
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
}
return Page();
}
public IActionResult OnPostInsert()
{
var data = products;
if (ModelState.IsValid)
{
data.Add(new Proddaplocal { Name = "Type Name here"});
}
return Page();
}
}
}
Find Questions by Tag
Helpful?
If a reply or comment is helpful for you, please don’t hesitate to click the Helpful button. This action is further confirmation of their invaluable contribution to the Appeon Community.