当前位置: 首页 > news >正文

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

C#是世界上最好的语言

新建项目 如下图所示选择框红的项目
在这里插入图片描述
然后新建 文件夹 Common 并新建类文件 名字任意 文件内容如下 因为要连接的是mysql数据库 所以需要安装 MySql.Data.MySqlClient 依赖;


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 文件夹 并新建类 类名任意 文件内容如下

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控制类 文件代码如下

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 标红的部分代码添加进去
在这里插入图片描述
代码如下

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#是世界上最好的语言

http://www.lryc.cn/news/288659.html

相关文章:

  • yolov8 opencv dnn部署自己的模型
  • 插槽(64-67)
  • C# LING查询语法学习,扩展方法的使用
  • 自然语言推断:微调BERT
  • 立创EDA学习:设计收尾工作
  • ShardingSphere之ShardingJDBC客户端分库分表上
  • rust for循环步长-1,反向逆序遍历
  • 编译与运行环境(C语言)
  • 再谈Android View绘制流程
  • 分布式定时任务系列8:XXL-job源码分析之远程调用
  • python+Qt5 UOS 摄相头+麦克风测试,摄相头自动解析照片二维条码,麦克风解析音频文件
  • MongoDB日期存储与查询、@Query、嵌套字段查询实战总结
  • Windows版本Node.js常见问题及操作解决方式(小白入门必备)
  • 09.Elasticsearch应用(九)
  • ROS2常用命令工具
  • Linux之快速入门
  • C语言——操作符详解1
  • C++学习| QT快速入门
  • Android App开发-简单控件(1)——文本显示
  • [GYCTF2020]Ezsqli1
  • 【npm包】如何发布自己的npm包
  • 《WebKit技术内幕》学习之十五(2):Web前端的未来
  • 【教学类-综合练习-11】20240116 大4班 最后一次
  • 【阻塞队列】阻塞队列的模拟实现及在生产者和消费者模型上的应用
  • Cocos Creator使用VS Code调试代码配置
  • 【投稿优惠|EI优质会议】2024年材料化学与清洁能源国际学术会议(IACMCCE 2024)
  • ubuntu设置右键打开terminator、code
  • PHP AES加解密:用代码为数据加上保护的盾牌
  • Socket实现服务器和客户端
  • 智能GPT图书管理系统(SpringBoot2+Vue2)、接入GPT接口,支持AI智能图书馆