VS2019正式版企业版注册码序列码密钥:
Visual Studio 2019 Enterprise
BF8Y8-GN2QH-T84XB-QVY3B-RC4DF
Visual Studio 2019 Professional
NYWVH-HT4XC-R2WYW-9Y3CM-X4V3Y
案例参考学习:
Asp.Net Core Web网站应用-C#介绍 - 简书
SQL语句命令汇总_yunlin2000的博客-CSDN博客
基于Asp.Net Core进行网页编程,从数据库中读取学生看书的数据,并展示到网页中。要求先展示列表,然后点击具体项目,进行展示。可进行一定的数据汇总。
题意分析
俗话说,题目越短越困难。还是像以往一样,我们把要求拆解来看,具体有以下几个要求:
①基于Asp.Net Core进行网页开发
②从数据库中读取数据
③将数据展示到网页中
④先展示列表,然后点击具体项目进行展示
⑤可进行一定的数据汇总
开发环境:
完整版安装MySqlmysql-installer-community-8.0.29.0:Vs2017安装MySql数据库_yunlin2000的博客-CSDN博客
完整代码
后端代码(Exercise_web\Program.cs):建立数据库并写入数据
using System;
using MySql.Data.MySqlClient;
namespace Exercise_web
{
public class Program
{
public static void Main(string[] args)
{
//准备被写入的数据(也可通过控制台让用户自行输入)
string[] number = { "2938", "0001", "0002", "0003", "2938", "6666", "0001", "0003", "0003", "2938" };
string[] student = { "流星", "杨小刚", "李小芳", "赵小明", "流星", "赖永炫(老师新年快乐!)", "杨小刚", "赵小明", "赵小明", "流星" };
string[] sex = { "男", "男", "女", "女", "男", "男", "男", "女", "女", "男" };
string[] birth = { "2002.2.20", "2002.12.20", "2002.7.16","2002.1.5","2002.2.20","~","2002.12.20","2002.1.5","2002.1.5","2002.2.20" };
string[] book = {"数据库系统原理与算法","数据结构与算法","概率论与数理统计","离散数学","C#程序设计教程","Taxi Demand Prediction with LSTM-based Combination Model.","汇编语言程序设计","C#程序设计教程","计算机网络与因特网","Unity3D游戏开发实战" };
//开始操作数据库
string connetStr = "server = 127.0.0.1; user id = root; password=zzaaqq; database = library;";
//server=127.0.0.1/localhost 代表本机,端口号port默认是3306可以不写
MySqlConnection connect = new MySqlConnection(connetStr);
try //CRUD
{
connect.Open();
//connect.Open(); //打开通道,建立连接
string cmd = "";
MySqlCommand mycmd = new MySqlCommand(cmd, connect);
//清空(测试时使用)
cmd = "delete from studentdata";
mycmd.CommandText = cmd;
mycmd.ExecuteNonQuery();
//建表
cmd = @"create table if not exists studentdata
(
number char(4),
student char(20),
sex char(3),
birth char(10),
book char(120)
)";
mycmd.CommandText = cmd;
mycmd.ExecuteNonQuery(); //对于非查询的语句使用这一指令
//插入数据
for(int i=0;i<number.Length;i++)
{
//按照列次序依次写入数据
cmd = String.Format("insert into studentdata(number,student,sex,birth,book) values (\'{0}\',\'{1}\',\'{2}\',\'{3}\',\'{4}\')", number[i], student[i], sex[i], birth[i], book[i]);
mycmd.CommandText = cmd;
mycmd.ExecuteNonQuery();
}
//查询数据
cmd = "select * from studentdata";
mycmd.CommandText = cmd;
MySqlDataReader reader = mycmd.ExecuteReader();
while (reader.Read())//有点迭代器,读取直到最后一行返回false
{
for (int i = 0; i < 5; i++)
Console.Write(reader[i].ToString() + " ");
Console.WriteLine();
}
}
catch(MySqlException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
connect.Close();
}
Console.WriteLine("Finish...");
while(true)
{
Console.Read();
}
}
}
}
错误解决:
FileNotFoundException: Could not load file or assembly 'System.Configuration.ConfigurationManager
NuGet
Install-Package System.Configuration.ConfigurationManager
前端代码(实验六 第二题 前端\Controllers\HomeController.cs):初始创立时的页面
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using test01.Models;
namespace test01.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
return View();
}
public IActionResult Privacy()
{
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
C#代码中“@字符串”的含义:
1、C# string 字符串的前面可以加 @ 可以将转义字符(\)当作普通字符对待,比如:
- string str = @"C:\Windows";
如果我们去掉 @ 的话,应该是:
- string str = "C:\\Windows";
2、@ 字符串中,我们用两个连续英文双引号表示一个英文双引号,如下字符串的实际内容为:="=,字符串长度为 3。
- string str = @"=""=";
3、@ 字符串中可以任意换行,换行符及缩进空格都计算在字符串长度之内。
- string str = @"<script type=""text/javascript"">
- <!--
- -->
- </script>";
- Console.WriteLine(str);
4、由于 @ 的这种特性,我们常将其应用到 SQL 字符串中。
- string sql = @"select * from tbl";
5、@ 只在连续一段字符串中有效,@"abc" + "\\",用 + 将两个字符串连接起来,第二个字符串中没有用 @ 标识,其中的 \ 就成为转义字符。
6、Environment.NewLine换行
在 C# 中,我们用字符串 "\r\n" 表示回车换行符。
string str = "第一行\r\n第二行";
但是我们更推荐 Environment.NewLine(名称空间为 System),Environment 是类,NewLine 是字符串属性,用于获取当前环境中定义的回车换行符字符串。
string str = "第一行" + Environment.NewLine + "第二行";
在 Windows 环境中,C# 语言 Environment.NewLine == "\r\n" 结果为 true。