CSharp · Mvc
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace HelloWorldMvcApp
{
public class Customer
{
public int CustomerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
}
@model IEnumerable<HelloWorldMvcApp.Customer>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.CustomerID }) |
@Html.ActionLink("Details", "Details", new { id=item.CustomerID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.CustomerID })
</td>
</tr>
}
</table>
// Dapper Plus
// Doc: https://dapper-tutorial.net/step-by-step-tutorial
// @nuget: Dapper -Version 1.60.6
// @nuget: Z.Dapper.Plus
using Dapper;
using Z.Dapper.Plus;
using Z.BulkOperations;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace HelloWorldMvcApp
{
public class HomeController : Controller
{
public ActionResult Index()
{
CreateDatabase();
List<Customer> customers = new List<Customer>();
using (IDbConnection db = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
{
customers = db.Query<Customer>("Select * From Customers").ToList();
}
return View(customers);
}
public static void CreateDatabase()
{
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
{
connection.Open();
using (var command = connection.CreateCommand())
{
command.CommandText = @"
CREATE TABLE [dbo].[Customers] (
[CustomerID] INT IDENTITY (1, 1) NOT NULL,
[FirstName] NVARCHAR (MAX) NULL,
[LastName] NVARCHAR (MAX) NULL,
[Email] NVARCHAR (MAX) NULL,
CONSTRAINT [PK_dbo.Customers] PRIMARY KEY CLUSTERED ([CustomerID] ASC)
);
INSERT INTO Customers (FirstName, LastName, Email) Values ('Carson', 'Alexander', '[email protected]');
INSERT INTO Customers (FirstName, LastName, Email) Values ('Meredith', 'Alonso', '[email protected]');
INSERT INTO Customers (FirstName, LastName, Email) Values ('Arturo', 'Anand', '[email protected]');
INSERT INTO Customers (FirstName, LastName, Email) Values ('Gytis', 'Barzdukas', '[email protected]');
INSERT INTO Customers (FirstName, LastName, Email) Values ('Yan', 'Li', '[email protected]');
";
command.ExecuteNonQuery();
}
}
}
}
}