将DataTable转化为json

csharp 复制代码
using MySqlConnector;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MysqlConnectorDemo
{
    internal class Program
    {
        public static string GetDataFromUserTable()
        {
            string connectionString = "server=localhost;user id=root;password=***;port=3306;database=test;";
            string query = "select * from sys_user";
            using (MySqlConnection con =new MySqlConnection(connectionString))
            {
                using (MySqlCommand cmd=new MySqlCommand(query,con))
                {
                    con.Open(); 
                    MySqlDataReader reader = cmd.ExecuteReader();
                    DataTable dt=new DataTable();
                    dt.Load(reader);
                    string json = JsonConvert.SerializeObject(dt,Formatting.Indented);
                    return json;
                }
            }
        }
        static void Main(string[] args)
        {
            string json=GetDataFromUserTable();
            Console.WriteLine(json);
        }
    }
}
相关推荐
HelloTonyGo2 小时前
个人游戏笔记本免费“养龙虾”(七)OpenClaw的openclaw.json文件的基本配置
ubuntu·json·配置·读写权限·openclaw
数据知道1 天前
claw-code 源码详细分析:`reference_data` JSON 快照——大型移植里「对照底稿」该怎么治理与演进?
linux·python·ubuntu·json·claude code
不会写DN1 天前
让 gRPC 服务同时支持 HTTP/JSON 的gRPC-Gateway
http·json·gateway
bloglin999992 天前
掌握解析JSON输出的技巧:使用LLM生成结构化数据
json
电商API&Tina3 天前
【京东item_getAPI 】高稳定:API 、非爬虫、不封号、不掉线、大促稳跑
大数据·网络·人工智能·爬虫·python·sql·json
LittroInno3 天前
T-JSON跨平台网络通信协议——边缘认知设备的二次开发实战指南
人工智能·计算机视觉·目标跟踪·json
椰汁菠萝4 天前
Mybatis-plus + PostgreSQL json格式类型转换异常
postgresql·json·mybatis
江西省遂川县常驻深圳大使4 天前
openclaw.json配置示例
服务器·json·openclaw
先做个垃圾出来………4 天前
JSON序列化问题
数据库·json
亚历克斯神4 天前
Flutter 三方库 jwt_io 的鸿蒙化适配指南 - 在鸿蒙系统上构建极致、严谨、全能的 JSON Web Token (JWT) 加解密与身份安全验证引擎
flutter·json·harmonyos