C#上位机序列7: 分库分表
C#上位机序列1: 多线程(线程同步,事件触发,信号量,互斥锁,共享内存,消息队列)
using FreeSql; using FreeSql.DataAnnotations; namespace FreeSqlMaster { internal class Program { static IFreeSql freeSql = null; static void Main(string[] args) { // 读写分离 // FreeSql 实现了第3种方案,支持一个【主库】多个【从库】,【从库】的查询策略为随机方式。 // 若某【从库】发生故障,将切换到其他可用【从库】,若已全部不可用则使用【主库】查询。 // 出现故障【从库】被隔离起来间隔性的检查可用状态,以待恢复 var connstr_master = "server=127.0.0.1;port=3306;database=test;user=root;password=root;charset=utf8;SslMode=none;Pooling=true;Min Pool Size=0;Max Pool Size=100;Charset=utf8"; var connstr_slave1 = "server=127.0.0.1;port=3306;database=test1;user=root;password=root;charset=utf8;SslMode=none;Pooling=true;Min Pool Size=0;Max Pool Size=100;Charset=utf8"; ////var connstr2_mysql = "server=127.0.0.1;port=3306;database=test;user=root;password=root;charset=utf8;SslMode=none;Pooling=true;Min Pool Size=0;Max Pool Size=100;Charset=utf8"; freeSql = new FreeSql.FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.MySql, connstr_master) .UseAutoSyncStructure(true) // 自动同步实体结构到数据库 //.UseSlave(connstr_slave1)//, "connstr_sqlserver2") //使用从数据库,支持多个 //.UseSlaveWeight() .Build(); //Student model = new Student() { Name = "1" }; //freeSql.Insert(model).ExecuteAffrows(); ////freeSql.Update<Student>().SetSource(model).ExecuteAffrows(); //var d = freeSql.Select<Student>().Where(x => x.Name == "1").First(); //select.Where(a => a.Id == 1).ToOne(); //读【从库】(默认) //select.Master().WhereId(a => a.Id == 1).ToOne(); //强制读【主库】 // 分表 //freeSql.Select<Log>().Any(); var logRepository = freeSql.GetGuidRepository<Log>(null, oldname => $"{oldname}_201903"); logRepository.Insert( new Log() { Content = "201903" }); logRepository.Update(new Log() { Content = "111", Id = 1 }); logRepository.Delete(new Log() { Id = 1 }); logRepository = freeSql.GetGuidRepository<Log>(null, oldname => $"{oldname}_201904"); logRepository.Insert(new Log() { Content = "201904" }); // 跨表查询 var sql = freeSql.Select<Log>() .AsTable((type, oldname) => "Log_201903") .AsTable((type, oldname) => "Log_201904") .ToList();// a => a.Id // 分库 IdleBus<IFreeSql> ib = new IdleBus<IFreeSql>(TimeSpan.FromMinutes(10)); ib.Register("test", () => new FreeSqlBuilder().UseConnectionString(DataType.MySql, connstr_master).Build()); ib.Register("test1", () => new FreeSqlBuilder().UseConnectionString(DataType.MySql, connstr_slave1).Build()); var lst1 = ib.Get("test").Select<Student>().Limit(10).ToList(); var lst2 = ib.Get("test1").Select<Student>().Limit(10).ToList(); Console.ReadKey(); } public class Student { [Column(Name = "id", IsIdentity = true, IsPrimary = true)] public int Id { get; set; } public string Name { get; set; } } public class Log { [Column(Name = "id", IsIdentity = true, IsPrimary = true)] public int Id { get; set; } public string Content { get; set; } } } }
qq:505645074