Install-Package Mysql.Data;
Install-Package Dapper;
using Dapper;
using MySql.Data.MySqlClient;
using System.Data;
using System.Diagnostics;
namespace ConsoleApp9
{
internal class Program
{
static void Main(string[] args)
{
AccessMySQL();
Console.WriteLine($"Finished,Now:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff")},{personsList.Count} items,{GetMemory()}");
}
private static string GetMemory()
{
var memory = Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024;
return $"Memory:{memory.ToString("#,###.00")} M";
}
static List<int> personsList = new List<int>();
private static void AccessMySQL()
{
try
{
string connStr = @"server=localhost;database=mydb;uid=userNameValue;pwd=passwordValue;";
using (var conn = new MySqlConnection(connStr))
{
conn.Open();
string sql = "SELECT id FROM t1 order by id desc limit 1";
var result = conn.Query<int>(sql).FirstOrDefault();
int batchSize = 1000000;
var batchCount = Math.Ceiling((double)result / (double)batchSize);
for (int i = 0; i < batchSize; i++)
{
sql = $"select id from t1 where id >= {i * batchSize} and id < {(i + 1) * batchSize};";
var tempPersons = conn.Query<int>(sql);
personsList.AddRange(tempPersons);
Console.WriteLine($"Now:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff")},{personsList.Count} items,{GetMemory()},{sql}\n\n");
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}