转载——Asp.Net Core 连接MySQL

转载自Asp.Net Core 连接MySQL

注意:本文使用ASP.NET Core版本为1.x

首先新建一个项目 Asp.NetCoreConnectorMySQL
在这里插入图片描述

通过简单的修改运行如下
在这里插入图片描述

接下来安装NuGet安装MySQL的库
在这里插入图片描述

查看MySQL里面自带的数据库
在这里插入图片描述

我们来修改json文件(appsettings.json)建立MySQL的连接串
这里写图片描述

新建一个类(这里我的类名是Lexan,你的不一定是Lexan)

        private WorldContext worldcontext { get; set; }
        public string LexanCode { get; set; }
        public string LexanName { get; set; }
        public string LexanContinent { get; set; }
        public string LexanRegion { get; set; }

在这里插入图片描述
再新建一个操作数据库的类(WorldContext)

        public string ConnectionString { get; set; }

        public WorldContext(string connectionString)
        {
            this.ConnectionString = connectionString;
        }
        private MySqlConnection GetConnection()
        {
            return new MySqlConnection(ConnectionString);
        }
        public List<Country> GetAllCountry()
        {
            List<Country> list = new List<Country>();
            //连接数据库
            using (MySqlConnection msconnection=GetConnection())
            {
                msconnection.Open();
                //查找数据库里面的表
                MySqlCommand mscommand = new MySqlCommand("select * from country",msconnection);
                using (MySqlDataReader reader=mscommand.ExecuteReader())
                {
                    //读取数据
                    while (reader.Read())
                    {
                        list.Add(new Country()
                        {
                            Code = reader.GetString("Code"),
                            Name=reader.GetString("Name"),
                            Continent=reader.GetString("Continent"),
                            Region=reader.GetString("Region")
                        });
                    }
                }
            }
            return list;
        }

在这里插入图片描述
再修改Startup.cs文件的ConfigureServices方法

 services.Add(new ServiceDescriptor(typeof(WorldContext),new WorldContext(Configuration.GetConnectionString("DefaultConnection"))));

在这里插入图片描述
新建一个控制器类

WorldContext context = HttpContext.RequestServices.GetService(typeof(Asp.NetCoreConnectorMySQL.Model.WorldContext)) as WorldContext;
            return View(context.GetAllCountrys());

在这里插入图片描述
添加一个MVC视图页
在这里插入图片描述

@model IEnumerable<AspNetCoreConnectionMySQL.Model.Country>
@{ 
    ViewBag.Title = "Lexan";
}
<h1>国家</h1>
<table class="table">
    <tr>
        <th>国家代码</th>
        <th>国家名</th>
        <th>陆地</th>
        <th>区域</th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>@Html.DisplayFor(modelitem=>item.Code)</td>
            <td>@Html.DisplayFor(modelitem=>item.Name)</td>
            <td>@Html.DisplayFor(modelitem=>item.Continent)</td>
            <td>@Html.DisplayFor(modelitem=>item.Region)</td>
        </tr>
    }
</table>

再修改一下项目的属性,然后运行即可
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值