首页 > 编程语言 >asp.net core 的一个CURD项目,.NET6

asp.net core 的一个CURD项目,.NET6

时间:2022-12-28 16:46:22浏览次数:69  
标签:core clientInfo String id CURD asp command using public

 

 

 

 

 

项目视频地址

https://www.youtube.com/watch?v=T-e554Zt3n4 

 

 

 

效果图

 

 

 

 

 

第一步

先安装vistal studio 2022

https://visualstudio.microsoft.com/zh-hans/free-developer-offers/

我下载免费的社区版

 

 

安装一直下一步就好,然后到了这里,第一个是要选的ASP.NET和Web开发,其他的看你需要了

 

 

 

第二步,安装SQL Server ,我装的是2019

https://cloud.tencent.com/developer/article/1889410

这这里接受得很详细了

 

第三步,新建一个asp.net core项目

 

 

 

 

 

第四步,先打开Sql Server,在连接数据库,在窗口视图里面打开服务器资源管理器,然后添加连接,选中Microsoft SQL Server这个,

然后点击继续。

后面得服务器名自己填一个,如果没有他会帮你新建的,下面的是连接,就是你安装的时候哪些账号密码,默认账号名是sa。

成功后你可以执行一些sql语句创建表

 

 

 

 

 

 

 

 

 

 

 

 

 

 

第五步是,安装NuGet程序包,在项目右击可以看到添加NuGet程序包

安装这个

 

 

 

 

 

 

 

 

 

 

 

第6步,在项目的Page文件夹下新建了Clients文件夹,在文件夹右击添加---》Razor页面-空。

 

 

 

 

下面我直接把页面的代码粘贴出来就好了

 

Shared文件夹下的添加了一个链接,

_Layout.cshtml文件

 

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - WebApplication1</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
    <link rel="stylesheet" href="~/WebApplication1.styles.css" asp-append-version="true" />
</head>
<body>
    <header>
        <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
            <div class="container">
                <a class="navbar-brand" asp-area="" asp-page="/Index">WebApplication1</a>
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                        aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
                    <ul class="navbar-nav flex-grow-1">
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-page="/Privacy">Privacy</a>
                        </li>
                        //*多了这个*/
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-page="/Clients/Index">Clients</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
    </header>
    <div class="container">
        <main role="main" class="pb-3">
            @RenderBody()
        </main>
    </div>

    <footer class="border-top footer text-muted">
        <div class="container">
            &copy; 2022 - WebApplication1 - <a asp-area="" asp-page="/Privacy">Privacy</a>
        </div>
    </footer>

    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="~/js/site.js" asp-append-version="true"></script>

    @await RenderSectionAsync("Scripts", required: false)
</body>
</html>

 

 

 

下面是Clients文件夹下的4个

Create.cshtml

@page
@model WebApplication1.Pages.Clients.CreateModel
@{
}

<br />
<h2>New Client</h2>

@if (Model.errorMessage.Length > 0)
{
    <div class="alert alert-warning alert-dismissible fade show" role="alert">
        <strong>@Model.errorMessage</strong>
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>

}

<form method="post">
    <div class="row mb-3">
        <label class="col-sm-3 col-form-label">Name</label>
        <div class="col-sm-6">
            <input type="text" class="form-control" name="name" value="@Model.clientInfo.name" />
        </div>
    </div>

    <div class="row mb-3">
        <label class="col-sm-3 col-form-label">Email</label>
        <div class="col-sm-6">
            <input type="text" class="form-control" name="email" value="@Model.clientInfo.email" />
        </div>
    </div>

    <div class="row mb-3">
        <label class="col-sm-3 col-form-label">Phone</label>
        <div class="col-sm-6">
            <input type="text" class="form-control" name="phone" value="@Model.clientInfo.phone" />
        </div>
    </div>

    <div class="row mb-3">
        <label class="col-sm-3 col-form-label">Address</label>
        <div class="col-sm-6">
            <input type="text" class="form-control" name="address" value="@Model.clientInfo.address" />
        </div>
    </div>


    @if (Model.successMessage.Length > 0)
    {
        <div class="row mb-3">
            <div class="col-sm-9">
                <div class="alert alert-success alert-dismissible fade show" role="alert">
                    <strong>@Model.successMessage</strong>
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            </div>
        </div>
    }


    <div class="row mb-3">
        <div class="offset-sm-3 col-sm-3 d-grid">
            <button type="submit" class="btn btn-primary">Submit</button>
        </div>
        <div class="col-sm-3 d-grid">
            <a class="btn btn-outline-primary" href="/" role="button">Cancel</a>
        </div>
    </div>


</form>

 

Create.cshtml.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Data.SqlClient;

namespace WebApplication1.Pages.Clients
{
    public class CreateModel : PageModel
    {
        public ClientInfo clientInfo = new ClientInfo();
        public String errorMessage = "";
        public String successMessage = "";
        public void OnGet()
        {

        }

        public void OnPost() 
        {
            clientInfo.name = Request.Form["name"];
            clientInfo.email = Request.Form["email"];
            clientInfo.phone = Request.Form["phone"];
            clientInfo.address = Request.Form["address"];

            if (clientInfo.name.Length == 0 || clientInfo.email.Length == 0 ||
                clientInfo.phone.Length == 0 || clientInfo.address.Length == 0) 
            {
                errorMessage = "All the fields are required";
                return;
            
            }
            // save the new client into the database 
            try
            {
                String connectionString = "Data Source=DESKTOP-CVIKLA1;Initial Catalog=mystore;User ID=sa;Password=123456";
                using (SqlConnection connention = new SqlConnection(connectionString))
                {
                    connention.Open();
                    String sql = "INSERT INTO clients"+"(name,email,phone,address) VALUES"+"(@name,@email,@phone,@address);";
                    using (SqlCommand command = new SqlCommand(sql, connention))
                    {
                        command.Parameters.AddWithValue("@name", clientInfo.name);
                        command.Parameters.AddWithValue("@email", clientInfo.email);
                        command.Parameters.AddWithValue("@phone", clientInfo.phone);
                        command.Parameters.AddWithValue("@address", clientInfo.address);

                        command.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
                return;
            }
            clientInfo.name = ""; clientInfo.email = ""; clientInfo.phone = ""; clientInfo.address = "";
            successMessage = "New Client Added Correctly";
            Response.Redirect("/Clients/Index");
        }
    }
}

 

 

 

 

 

Delete.cshtml

@page
@using System.Data.SqlClient;
@{
    try
    {
        String id = Request.Query["id"];

        String connectionString = "Data Source=DESKTOP-CVIKLA1;Initial Catalog=mystore;User ID=sa;Password=123456";
        using (SqlConnection connention = new SqlConnection(connectionString))
        {
            connention.Open();
            String sql = "DELETE FROM clients WHERE id=@id";
            using (SqlCommand command = new SqlCommand(sql, connention))
            {
                command.Parameters.AddWithValue("@id", id);

                command.ExecuteNonQuery();
            }
        }
    }
    catch (Exception ex)
    {
        
    }
    Response.Redirect("/Clients/Index");
}

 

Delete.cshtml.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace WebApplication1.Pages.Clients
{
    public class DeleteModel : PageModel
    {
        public void OnGet()
        {
        }
    }
}

 

 

 

 

 

Edit.cshtml

@page
@model WebApplication1.Pages.Clients.EditModel
@{
}


<br />
<h2>Edit Cloent</h2>

@if (Model.errorMessage.Length > 0)
{
    <div class="alert alert-warning alert-dismissible fade show" role="alert">
        <strong>@Model.errorMessage</strong>
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>

}

<form method="post">
    <input type="hidden" name="id" value="@Model.clientInfo.id" />
    <div class="row mb-3">
        <label class="col-sm-3 col-form-label">Name</label>
        <div class="col-sm-6">
            <input type="text" class="form-control" name="name" value="@Model.clientInfo.name" />
        </div>
    </div>

    <div class="row mb-3">
        <label class="col-sm-3 col-form-label">Email</label>
        <div class="col-sm-6">
            <input type="text" class="form-control" name="email" value="@Model.clientInfo.email" />
        </div>
    </div>

    <div class="row mb-3">
        <label class="col-sm-3 col-form-label">Phone</label>
        <div class="col-sm-6">
            <input type="text" class="form-control" name="phone" value="@Model.clientInfo.phone" />
        </div>
    </div>

    <div class="row mb-3">
        <label class="col-sm-3 col-form-label">Address</label>
        <div class="col-sm-6">
            <input type="text" class="form-control" name="address" value="@Model.clientInfo.address" />
        </div>
    </div>


    @if (Model.successMessage.Length > 0)
    {
        <div class="row mb-3">
            <div class="col-sm-9">
                <div class="alert alert-success alert-dismissible fade show" role="alert">
                    <Storng>@Model.successMessage</Storng>
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            </div>
        </div>
    }


    <div class="row mb-3">
        <div class="offset-sm-3 col-sm-3 d-grid">
            <button type="submit" class="btn btn-primary">Submit</button>
        </div>
        <div class="col-sm-3 d-grid">
            <a class="btn btn-outline-primary" href="/" role="button">Cancel</a>
        </div>
    </div>


</form>

 

Edit.cshtml.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Data.SqlClient;

namespace WebApplication1.Pages.Clients
{
    public class EditModel : PageModel
    {
        public ClientInfo clientInfo = new ClientInfo();
        public String errorMessage = "";
        public String successMessage = "";
        public void OnGet()
        {
            String id = Request.Query["id"];

            try
            {
                String connectionString = "Data Source=DESKTOP-CVIKLA1;Initial Catalog=mystore;User ID=sa;Password=123456";
                using (SqlConnection connention = new SqlConnection(connectionString))
                {
                    connention.Open();
                    String sql = "SELECT * FROM clients WHERE id=@id";
                    using (SqlCommand command = new SqlCommand(sql, connention))
                    {
                        command.Parameters.AddWithValue("@id", id);
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                clientInfo.id = "" + reader.GetInt32(0);
                                clientInfo.name = reader.GetString(1);
                                clientInfo.email = reader.GetString(2);
                                clientInfo.phone = reader.GetString(3);
                                clientInfo.address = reader.GetString(4);
                                clientInfo.created_at = reader.GetDateTime(5).ToString();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {

                errorMessage= ex.Message;
            }
        }
        public void OnPost() 
        {
            clientInfo.id = Request.Form["id"];
            clientInfo.name = Request.Form["name"];
            clientInfo.email = Request.Form["email"];
            clientInfo.phone = Request.Form["phone"];
            clientInfo.address = Request.Form["address"];
            if (clientInfo.name.Length == 0 || clientInfo.email.Length == 0 ||
               clientInfo.phone.Length == 0 || clientInfo.address.Length == 0)
            {
                errorMessage = "All the fields are required";
                return;

            }


            try
            {
                String connectionString = "Data Source=DESKTOP-CVIKLA1;Initial Catalog=mystore;User ID=sa;Password=123456";
                using (SqlConnection connention = new SqlConnection(connectionString))
                {
                    connention.Open();
                    String sql = "UPDATE clients " +
             "SET name=@name, email=@email, phone=@phone, address=@address " +
             "WHERE id=@id";
                    using (SqlCommand command = new SqlCommand(sql, connention))
                    {
                        command.Parameters.AddWithValue("@name", clientInfo.name);
                        command.Parameters.AddWithValue("@email", clientInfo.email);
                        command.Parameters.AddWithValue("@phone", clientInfo.phone);
                        command.Parameters.AddWithValue("@address", clientInfo.address);
                        command.Parameters.AddWithValue("@id", clientInfo.id);

                        command.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
                return;
            }
            Response.Redirect("/Clients/Index");
        }
    }
}

 

 

 

 

 

 

Index.cshtml

@page
@model WebApplication1.Pages.Clients.IndexModel
@{
}

<br />
<h2>List of Clients</h2>
<a class="btn btn-primary btn-sm" href="/Clients/Create">New Client</a>
<table class="table">
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Email</th>
            <th>Phone</th>
            <th>Address</th>
            <th>Created</th>
            <th>Created At</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
        @foreach(var item in Model.listClients)
        {
            <tr>
                <td>@item.id</td>
                <td>@item.name</td>
                <td>@item.email</td>
                <td>@item.phone</td>
                <td>@item.address</td>
                <td>@item.created_at</td>
                <td>
                    <a class="btn btn-primary btn-sm" href="/Clients/[email protected]">Edit</a>
                    <a class="btn btn-danger btn-sm" href="/Clients/[email protected]">Delete</a>
                </td>
            </tr>
        }
    </tbody>
</table>

 

Index.cshtml.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication1.Pages.Clients
{
    public class IndexModel : PageModel
    { 
        public List<ClientInfo> listClients = new List<ClientInfo>();
        public void OnGet()
        {
            try
            {
                String connectionString = "Data Source=DESKTOP-CVIKLA1;Initial Catalog=mystore;User ID=sa;Password=123456";
                using (SqlConnection connention = new  SqlConnection(connectionString))
                {
                    connention.Open();
                    String sql = "SELECT * FROM clients";
                    using (SqlCommand command = new SqlCommand(sql, connention)) 
                    {
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read()) 
                            {
                                ClientInfo clientInfo = new ClientInfo();
                                clientInfo.id = "" + reader.GetInt32(0);
                                clientInfo.name = reader.GetString(1);
                                clientInfo.email = reader.GetString(2);
                                clientInfo.phone = reader.GetString(3);
                                clientInfo.address = reader.GetString(4);
                                clientInfo.created_at = reader.GetDateTime(5).ToString();

                                listClients.Add(clientInfo);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {

                Console.WriteLine("Exception" + ex.ToString());
            }
        }
    }

    public class ClientInfo
    {
        public String id;
        public String name;
        public String email;
        public String phone;
        public String address;
        public String created_at;
    }
}

 

 

 

 

以上这么多,可以观看上面视频,有链接,不懂的可以留意哦

 

标签:core,clientInfo,String,id,CURD,asp,command,using,public
From: https://www.cnblogs.com/hechunfeng/p/17010331.html

相关文章