using System; using System.Collections.Generic; using System.Linq; using System.Text; using NewTest.Dao; using Models; using System.Data.SqlClient; using SqlSugar; namespace NewTest.Demos { SerialNumber : IDemos { public void Init() { Console.WriteLine(); using (SqlSugarClient db = SugarFactory.GetInstance())//开启数据库连接 { var dientityValue = db.Insert<Student>(new Student() { }); var name = db.Queryable<Student>().Single(it => it.id == dientityValue.ObjToInt()).name; Console.WriteLine(name); var dientityValue2 = db.Insert<School>(new School() { }); var name2 = db.Queryable<School>().Single(it => it.id == dientityValue2.ObjToInt()).name; Console.WriteLine(name2); ; } } 全局配置类 SugarConfigs { public static List<PubModel.SerialNumber> NumList = new List<PubModel.SerialNumber>(){ , FieldName=, GetNumFunc=()=>{ +DateTime.Now.ToString(); }}, , FieldName=, GetNumFuncWithDb=db=>{ +DateTime.Now.ToString(); }} }; } SqlSugar实例工厂 SugarFactory { SugarFactory() { } public static SqlSugarClient GetInstance() { db = new SqlSugarClient(connection); db.SetSerialNumber(SugarConfigs.NumList); db; } } } }
View Code16、多库并行计算功能
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using SqlSugar; using System.Configuration; using System.Data; using System.Threading.Tasks; using System.Transactions; using System.Threading; namespace WebTest.Demo { /*******************************************************************分布式计算 demo**************************************************************/ Cloud : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { ); int pageCount = 0; using (CloudClient db = CloudDao.GetInstance()) { db.PageMaxHandleNumber = 1500; using (CommittableTransaction trans = new CommittableTransaction())//启用分布式事务 { db.Tran = trans; s = new student() { createTime = DateTime.Now, id = Guid.NewGuid(), name = Guid.NewGuid() + "" }; s.name = "改11"; 根据表达示更新 根据主键更新 trans.Commit(); } db.TranDispose(); taskDataTable = db.Taskable<DataTable>().Tasks; foreach (var dr in taskDataTable)//遍历所有节点数据 { connectionName = dr.Result.ConnectionString; } taskInt = db.Taskable<).Tasks; foreach (var dr in taskInt)//遍历所有节点数据 { connectionName = dr.Result.ConnectionString; } taskEntity = db.Taskable<student>().Tasks; foreach (var dr in taskEntity)//遍历所有节点数据 { connectionName = dr.Result.ConnectionString; } // //Taskable实现分组查询 // var groupList = db.Taskable<DataTable>("SELECT name,COUNT(*) AS [count],AVG(num) as num FROM STUDENT WHERE ID IS NOT NULL GROUP BY NAME ") // .Tasks // .SelectMany(it => it.Result.DataTable.AsEnumerable()) // .Select(dr => new { num = Convert.ToInt32(dr["NUM"]), name = dr["NAME"].ToString(), count = Convert.ToInt32(dr["COUNT"]) }) // .GroupBy(dr => dr.name).Select(dt => new { name = dt.First().name, count = dt.Sum(dtItem => dtItem.count), num = dt.Sum(dtItem => dtItem.num) / dt.Sum(dtItem => dtItem.count) }).ToList(); // //输出结果 // foreach (var it in groupList) // { // var num = it.num; // var name = it.name; // var count = it.count; // } // //简化 // var groupList2 = db.Taskable<DataTable>("SELECT NAME,COUNT(*) AS [COUNT],AVG(NUM) as NUM FROM STUDENT WHERE ID IS NOT NULL GROUP BY NAME ") 将结果集合并到一个集合 //.Select(dr => new { num = Convert.ToInt32(dr["NUM"]), name = dr["NAME"].ToString(), count = Convert.ToInt32(dr["COUNT"]) }) //.GroupBy(dr => dr.name).Select(dt => new { name = dt.First().name, count = dt.Sum(dtItem => dtItem.count), num = dt.Sum(dtItem => dtItem.num) / dt.Sum(dtItem => dtItem.count) }).ToList(); // //再简化 // List<V_Student> groupList3 = db.Taskable<V_Student>("SELECT name,COUNT(*) AS [count],AVG(num) as num FROM STUDENT WHERE ID IS NOT NULL GROUP BY NAME ") 将结果集合并到一个集合 //.GroupBy(dr => dr.name).Select(dt => new V_Student { name = dt.First().name, count = dt.Sum(dtItem => dtItem.count), num = dt.Sum(dtItem => dtItem.num) / dt.Sum(dtItem => dtItem.count) }).ToList(); maxValue = db.Taskable<).Max();minValue = db.Taskable<).Min();dataCount = db.Taskable<).Count();//求出所有节点数据 all = db.Taskable<student>(, }).ToList();dnSingle = DateTime.Now; , timeSingle = (DateTime.Now - dnSingle).TotalSeconds; var dn = DateTime.Now; //单列排序 time = (DateTime.Now - dn).TotalSeconds; dn2 = DateTime.Now; , , 5000, 25, ref pageCount, new List<OrderByDictionary>() { , OrderByType=OrderByType.asc}, // new OrderByDictionary(){ OrderByField="createTime", OrderByType=OrderByType.desc}, // new OrderByDictionary(){ OrderByField="createTime", OrderByType=OrderByType.asc} }, }); var time2 = (DateTime.Now - dn2).TotalSeconds; } } 测试类 student { public Guid id { get; set; } public string name { get; set; } public DateTime createTime { get; set; } public int num { get; set; } } 获取数据库连接对象 CloudDao { private CloudDao() { } public static CloudClient GetInstance() { return new CloudClient(new List<CloudConnectionConfig>() { ].ToString(), Rate=1}, ].ToString(), Rate=1}, ].ToString(), Rate=1}, ].ToString(), Rate=1}, ].ToString(), Rate=1}, ].ToString(), Rate=1}, ].ToString(), Rate=1}, ].ToString(), Rate=1}, ].ToString(), Rate=1}, ].ToString(), Rate=1}, ].ToString(), Rate=1}, ].ToString(), Rate=1}, ].ToString(), Rate=1}, //new CloudConnectionConfig(){ ConnectionString=ConfigurationManager.ConnectionStrings["cy1"].ToString(), Rate=1}, // new CloudConnectionConfig(){ ConnectionString=ConfigurationManager.ConnectionStrings["cy2"].ToString(), Rate=1}, // new CloudConnectionConfig(){ ConnectionString=ConfigurationManager.ConnectionStrings["cy3"].ToString(), Rate=1}, // new CloudConnectionConfig(){ ConnectionString=ConfigurationManager.ConnectionStrings["zq1"].ToString(), Rate=1}, // new CloudConnectionConfig(){ ConnectionString=ConfigurationManager.ConnectionStrings["zq2"].ToString(), Rate=1}, // new CloudConnectionConfig(){ ConnectionString=ConfigurationManager.ConnectionStrings["zq3"].ToString(), Rate=1}, // new CloudConnectionConfig(){ ConnectionString=ConfigurationManager.ConnectionStrings["qp1"].ToString(), Rate=1}, // new CloudConnectionConfig(){ ConnectionString=ConfigurationManager.ConnectionStrings["qp2"].ToString(), Rate=1}, // new CloudConnectionConfig(){ ConnectionString=ConfigurationManager.ConnectionStrings["qp3"].ToString(), Rate=1}, // new CloudConnectionConfig(){ ConnectionString=ConfigurationManager.ConnectionStrings["wy1"].ToString(), Rate=1}, // new CloudConnectionConfig(){ ConnectionString=ConfigurationManager.ConnectionStrings["wy2"].ToString(), Rate=1}, // new CloudConnectionConfig(){ ConnectionString=ConfigurationManager.ConnectionStrings["wy3"].ToString(), Rate=1}, // new CloudConnectionConfig(){ ConnectionString=ConfigurationManager.ConnectionStrings["wwf1"].ToString(), Rate=1}, // new CloudConnectionConfig(){ ConnectionString=ConfigurationManager.ConnectionStrings["wwf2"].ToString(), Rate=1}, // new CloudConnectionConfig(){ ConnectionString=ConfigurationManager.ConnectionStrings["wwf3"].ToString(), Rate=1}, }); } } } }
View Code
GITHUB下载地址:
MSSQL .NET 4.0+
https://github.com/sunkaixuan/SqlSugar
MSSQL .NET Core 版本
https://github.com/sunkaixuan/ASP_NET_CORE_ORM_SqlSugar
Sqlite .net4.0+
https://github.com/sunkaixuan/SqliteSugar
MYSQL .NET 4.0+
ORACLE.NET 4.0+
https://github.com/sunkaixuan/OracleSugar
ORACLE CORE ,MYSQL CORE,SQLITE CORE 待开发