将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);
        }
    }
}
相关推荐
维天说13 小时前
CLI-Switch 2026年3月版历史设计:Hook、TTY 隔离与 JSON 状态
java·服务器·json
一颗小行星-2 天前
为什么有的JSON可以带注释?
json
CedarQR3 天前
万字长文:从零在 RK3588 上部署 PaddleSpeech 中文 TTS 全流程(FastSpeech2 + HiFiGAN)
开发语言·c++·嵌入式硬件·ubuntu·json
Geoking.4 天前
JSON vs JSONL:从数据格式到 AI Agent 的工程实践
人工智能·深度学习·json
ID_180079054734 天前
京东商品详情API能力解析与标准化应用方案(含JSON返回示例)
json
灯澜忆梦5 天前
Go 语言 _JSON---序列化与反序列化
开发语言·golang·json
梦幻通灵6 天前
Notepad++格式化Json两种方案【持续更新】
android·json·notepad++
解局易否结局6 天前
鸿蒙原生开发实战|Native JSON 编解码与高性能数据序列化
华为·json·harmonyos
代码的小搬运工6 天前
网络请求(NSURL、NSURLRequest、NSURLSessionDataTask、协议回调与 JSON 数据的基本流向)
网络·数据库·json
2601_960906727 天前
网络防御等最先进AI模型领域
人工智能·json·hbase·flume·storm