C#-前后端分离连接mysql数据库封装接口

C#是世界上最好的语言

新建项目 如下图所示选择框红的项目

然后新建 文件夹 Common 并新建类文件 名字任意 文件内容如下 因为要连接的是mysql数据库 所以需要安装 MySql.Data.MySqlClient 依赖;

csharp 复制代码
using MySql.Data.MySqlClient;
using System.Data;



namespace WebApplication1.Common
{
    public class SqlHelp
    {
        //public static string ConStr { get; set; }
        public static DataTable ExecuteTable(string cmdText)
        {
            using (MySqlConnection con = new MySqlConnection("server=127.0.0.1;database=mynodedata;username=root;password=123456;"))
            {
                con.Open();
                MySqlCommand cmd = new MySqlCommand(cmdText, con);
                MySqlDataAdapter sda = new MySqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                return ds.Tables[0];
                // 取数据
            }
        }
    }
}

然后新建 Models 文件夹 并新建类 类名任意 文件内容如下

csharp 复制代码
using WebApplication1.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Numerics;

namespace WebApplication1.Models
{
    public class user
    {
        public Int64 id { get; set; }
        public String name { get; set; }
        public String password { get; set; }
        public String loginname { get; set; }
        public String mobie { get; set; }
        public String role { get; set; }

        public static List<user> GetTextList()
        {
          
            DataTable dt = SqlHelp.ExecuteTable("select * from user");
            List<user> scc1 = new List<user>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                scc1.Add(ToModel(dt.Rows[i]));
            }
            Console.WriteLine(1111);

            return scc1;
        }

        private static user ToModel(DataRow dr)
        {
            // 下面后面的数据都是取自数据库
            user scc = new user();
            scc.id = (Int64)dr["id"];
            scc.name = dr["name"].ToString();
            scc.password = dr["password"].ToString();
            scc.loginname = dr["loginname"].ToString();
            scc.mobie = dr["mobie"].ToString();
            scc.role = dr["role"].ToString();
            return scc;
        }
    }
}

然后在系统自带的Controllers文件夹上新建 api控制类 文件代码如下

csharp 复制代码
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    [EnableCors("any")]// 添加 跨域名【在同一台电脑上面端口不一样也属于跨域】
    [Route("api/[controller]/[action]")]// 后面添加 /[action] 下面是具体写到方法名
    [ApiController]
    public class usermyController : ControllerBase
    {
        [HttpGet]
        public List<user> GetTest01()
        {
            List<user> scc01 = user.GetTextList();
            return scc01;
        }
    }
}

然后需要改一点系统自带的文件Program.cs 标红的部分代码添加进去

代码如下

csharp 复制代码
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddCors(o => o.AddPolicy("any", p => p.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()));
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseRouting();

app.UseCors("any");
app.UseAuthorization();

app.UseEndpoints(endpoints =>{
    endpoints.MapControllers();
});

app.Run();

然后运行项目 API 文档和测试文件

跨域测试

C#是世界上最好的语言

相关推荐
程序员岳焱6 小时前
Java 与 MySQL 性能优化:Java 实现百万数据分批次插入的最佳实践
后端·mysql·性能优化
计算机毕设定制辅导-无忧学长7 小时前
西门子 PLC 与 Modbus 集成:S7-1500 RTU/TCP 配置指南(一)
服务器·数据库·tcp/ip
程序员柳7 小时前
基于微信小程序的校园二手交易平台、微信小程序校园二手商城源代码+数据库+使用说明,layui+微信小程序+Spring Boot
数据库·微信小程序·layui
葬歌倾城7 小时前
JSON的缩进格式方式和紧凑格式方式
c#·json
梦在深巷、7 小时前
MySQL/MariaDB数据库主从复制之基于二进制日志的方式
linux·数据库·mysql·mariadb
IT乌鸦坐飞机8 小时前
ansible部署数据库服务随机启动并创建用户和设置用户有完全权限
数据库·ansible·centos7
IT_10248 小时前
Spring Boot项目开发实战销售管理系统——数据库设计!
java·开发语言·数据库·spring boot·后端·oracle
Johny_Zhao8 小时前
Ubuntu系统安装部署Pandawiki智能知识库
linux·mysql·网络安全·信息安全·云计算·shell·yum源·系统运维·itsm·pandawiki
祁思妙想9 小时前
八股学习(三)---MySQL
数据库·学习·mysql
惊骇世俗王某人9 小时前
1.MySQL之如何定位慢查询
数据库·mysql