diff --git a/Src/Asp.Net/SqlServerTest/Config.cs b/Src/Asp.Net/SqlServerTest/Config.cs index 95d8f3261..e13992dcb 100644 --- a/Src/Asp.Net/SqlServerTest/Config.cs +++ b/Src/Asp.Net/SqlServerTest/Config.cs @@ -11,5 +11,6 @@ namespace OrmTest public static string ConnectionString = "server=.;uid=sa;pwd=haosql;database=SQLSUGAR4XTEST"; public static string ConnectionString2 = "server=.;uid=sa;pwd=haosql;database=sqlsugar4xtest"; public static string ConnectionString3 = "server=.;uid=sa;pwd=haosql;database=sqlsugar4xtesT"; + public static string ConnectionString4 = "server=localhost;Database=SqlSugar4xTest;Uid=root;Pwd=haosql"; } } diff --git a/Src/Asp.Net/SqlServerTest/Demo/Demo1_SqlSugarClient.cs b/Src/Asp.Net/SqlServerTest/Demo/Demo1_SqlSugarClient.cs new file mode 100644 index 000000000..25aaa43e5 --- /dev/null +++ b/Src/Asp.Net/SqlServerTest/Demo/Demo1_SqlSugarClient.cs @@ -0,0 +1,72 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using SqlSugar; +namespace OrmTest +{ + public class Demo1_SqlSugarClient + { + + public static void Init() + { + DistributedTransactionExample(); + } + + private static void DistributedTransactionExample() + { + Console.WriteLine(""); + Console.WriteLine("#### Distributed TransactionExample Start ####"); + SqlSugarClient db = new SqlSugarClient(new List() + { + new ConnectionConfig(){ ConfigId=1, DbType=DbType.SqlServer, ConnectionString=Config.ConnectionString,InitKeyType=InitKeyType.Attribute,IsAutoCloseConnection=true }, + new ConnectionConfig(){ ConfigId=2, DbType=DbType.MySql, ConnectionString=Config.ConnectionString4 ,InitKeyType=InitKeyType.Attribute ,IsAutoCloseConnection=true} + }); + + //use first(SqlServer) + db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Order), typeof(OrderItem));// + db.Insertable(new Order() { Name = "order1", CreateTime = DateTime.Now }).ExecuteCommand(); + Console.WriteLine(db.CurrentConnectionConfig.DbType + ":" + db.Queryable().Count()); + + //use mysql + db.ChangeDatabase(it => it.DbType == DbType.MySql); + db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Order), typeof(OrderItem)); + db.Insertable(new Order() { Name = "order1", CreateTime = DateTime.Now }).ExecuteCommand(); + Console.WriteLine(db.CurrentConnectionConfig.DbType + ":" + db.Queryable().Count()); + + //SqlServer + db.ChangeDatabase(it => it.DbType == DbType.SqlServer);//use sqlserver + try + { + db.BeginAllTran(); + + db.ChangeDatabase(it => it.DbType == DbType.SqlServer);//use sqlserver + db.Deleteable().ExecuteCommand(); + Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType); + Console.WriteLine(db.Queryable().Count()); + + db.ChangeDatabase(it => it.DbType == DbType.MySql);//use mysql + db.Deleteable().ExecuteCommand(); + Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType); + Console.WriteLine(db.Queryable().Count()); + + throw new Exception(); + db.CommitAllTran(); + } + catch + { + db.RollbackAllTran(); + Console.WriteLine("---Roll back"); + db.ChangeDatabase(it => it.DbType == DbType.SqlServer);//use sqlserver + Console.WriteLine(db.CurrentConnectionConfig.DbType); + Console.WriteLine(db.Queryable().Count()); + + db.ChangeDatabase(it => it.DbType == DbType.MySql);//use mysql + Console.WriteLine(db.CurrentConnectionConfig.DbType); + Console.WriteLine(db.Queryable().Count()); + } + + Console.WriteLine("#### Distributed TransactionExample End ####"); + } + } +} diff --git a/Src/Asp.Net/SqlServerTest/Models/Order.cs b/Src/Asp.Net/SqlServerTest/Models/Order.cs new file mode 100644 index 000000000..2af24b100 --- /dev/null +++ b/Src/Asp.Net/SqlServerTest/Models/Order.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace OrmTest +{ + + public class Order + { + [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + + public string Name { get; set; } + public decimal Price { get; set; } + [SqlSugar.SugarColumn(IsNullable =true)] + public DateTime CreateTime { get; set; } + } +} diff --git a/Src/Asp.Net/SqlServerTest/Models/OrderItem.cs b/Src/Asp.Net/SqlServerTest/Models/OrderItem.cs new file mode 100644 index 000000000..ae3262c7c --- /dev/null +++ b/Src/Asp.Net/SqlServerTest/Models/OrderItem.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace OrmTest +{ + [SqlSugar.SugarTable("OrderDetail")] + public class OrderItem + { + [SqlSugar.SugarColumn(IsPrimaryKey =true, IsIdentity =true)] + public int ItemId { get; set; } + public int OrderId { get; set; } + public decimal? Price { get; set; } + [SqlSugar.SugarColumn(IsNullable = true)] + public DateTime? CreateTime { get; set; } + } +} diff --git a/Src/Asp.Net/SqlServerTest/Program.cs b/Src/Asp.Net/SqlServerTest/Program.cs index 04a01e535..071b3c8e0 100644 --- a/Src/Asp.Net/SqlServerTest/Program.cs +++ b/Src/Asp.Net/SqlServerTest/Program.cs @@ -10,6 +10,8 @@ namespace OrmTest { //OldTestMain.Init(); + Demo1_SqlSugarClient.Init(); + Console.WriteLine("all successfully."); Console.ReadKey(); } diff --git a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj index 1e77a51e3..6707bee85 100644 --- a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj +++ b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj @@ -49,6 +49,9 @@ + + + @@ -100,9 +103,9 @@ - True - True SugarTemplate.tt + True + True @@ -145,6 +148,7 @@ +