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);
}
}
}