mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 13:06:50 +08:00
Update Gbase demo
This commit is contained in:
parent
d4b4a91908
commit
f62b9e6fad
@ -16,7 +16,6 @@ namespace OrmTest
|
|||||||
SqlSugarClient();//Create db
|
SqlSugarClient();//Create db
|
||||||
DbContext();//Optimizing SqlSugarClient usage
|
DbContext();//Optimizing SqlSugarClient usage
|
||||||
SingletonPattern();//Singleten Pattern
|
SingletonPattern();//Singleten Pattern
|
||||||
DistributedTransactionExample();
|
|
||||||
MasterSlave();//Read-write separation
|
MasterSlave();//Read-write separation
|
||||||
CustomAttribute();
|
CustomAttribute();
|
||||||
}
|
}
|
||||||
@ -227,130 +226,7 @@ where a.tabtype in ('T', 'V') and not (a.tabname like 'sys%') AND a.tabname <>'
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
private static void DistributedTransactionExample()
|
|
||||||
{
|
|
||||||
Console.WriteLine("");
|
|
||||||
Console.WriteLine("#### Distributed TransactionExample Start ####");
|
|
||||||
SqlSugarClient db = new SqlSugarClient(new List<ConnectionConfig>()
|
|
||||||
{
|
|
||||||
new ConnectionConfig(){ ConfigId="1", DbType=DbType.GBase, ConnectionString=Config.ConnectionString,InitKeyType=InitKeyType.Attribute,IsAutoCloseConnection=true },
|
|
||||||
new ConnectionConfig(){ ConfigId="2", DbType=DbType.GBase, ConnectionString=Config.ConnectionString2 ,InitKeyType=InitKeyType.Attribute ,IsAutoCloseConnection=true}
|
|
||||||
});
|
|
||||||
|
|
||||||
var db1 = db.Ado.Connection.Database;
|
|
||||||
//use db1
|
|
||||||
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<Order>().Count());
|
|
||||||
|
|
||||||
//use db2
|
|
||||||
db.ChangeDatabase("2");
|
|
||||||
var db2 = db.Ado.Connection.Database;
|
|
||||||
db.DbMaintenance.CreateDatabase();//Create Database2
|
|
||||||
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<Order>().Count());
|
|
||||||
|
|
||||||
if (db2 == db1)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Example 1
|
|
||||||
Console.WriteLine("Example 1");
|
|
||||||
try
|
|
||||||
{
|
|
||||||
db.BeginTran();
|
|
||||||
|
|
||||||
db.ChangeDatabase("1");//use db1
|
|
||||||
db.Deleteable<Order>().ExecuteCommand();
|
|
||||||
Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
|
|
||||||
Console.WriteLine(db.Queryable<Order>().Count());
|
|
||||||
|
|
||||||
db.ChangeDatabase("2");//use db2
|
|
||||||
db.Deleteable<Order>().ExecuteCommand();
|
|
||||||
Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
|
|
||||||
Console.WriteLine(db.Queryable<Order>().Count());
|
|
||||||
|
|
||||||
throw new Exception();
|
|
||||||
db.CommitTran();
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
db.RollbackTran();
|
|
||||||
Console.WriteLine("---Roll back");
|
|
||||||
db.ChangeDatabase("1");//use db1
|
|
||||||
Console.WriteLine(db.CurrentConnectionConfig.DbType);
|
|
||||||
Console.WriteLine(db.Queryable<Order>().Count());
|
|
||||||
|
|
||||||
db.ChangeDatabase("2");//use db2
|
|
||||||
Console.WriteLine(db.CurrentConnectionConfig.DbType);
|
|
||||||
Console.WriteLine(db.Queryable<Order>().Count());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Example 2
|
|
||||||
Console.WriteLine("Example 2");
|
|
||||||
|
|
||||||
var result=db.UseTran(() =>
|
|
||||||
{
|
|
||||||
|
|
||||||
db.ChangeDatabase("1");//use db1
|
|
||||||
db.Deleteable<Order>().ExecuteCommand();
|
|
||||||
Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
|
|
||||||
Console.WriteLine(db.Queryable<Order>().Count());
|
|
||||||
|
|
||||||
db.ChangeDatabase("2");//use db2
|
|
||||||
db.Deleteable<Order>().ExecuteCommand();
|
|
||||||
Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
|
|
||||||
Console.WriteLine(db.Queryable<Order>().Count());
|
|
||||||
throw new Exception("");
|
|
||||||
|
|
||||||
});
|
|
||||||
if (result.IsSuccess == false) {
|
|
||||||
Console.WriteLine("---Roll back");
|
|
||||||
db.ChangeDatabase("1");//use db1
|
|
||||||
Console.WriteLine(db.CurrentConnectionConfig.DbType);
|
|
||||||
Console.WriteLine(db.Queryable<Order>().Count());
|
|
||||||
|
|
||||||
db.ChangeDatabase("2");//use db2
|
|
||||||
Console.WriteLine(db.CurrentConnectionConfig.DbType);
|
|
||||||
Console.WriteLine(db.Queryable<Order>().Count());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Example 3
|
|
||||||
Console.WriteLine("Example 3");
|
|
||||||
|
|
||||||
var result2 = db.UseTranAsync(async () =>
|
|
||||||
{
|
|
||||||
|
|
||||||
db.ChangeDatabase("1");//use db1
|
|
||||||
await db.Deleteable<Order>().ExecuteCommandAsync();
|
|
||||||
Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
|
|
||||||
Console.WriteLine(db.Queryable<Order>().Count());
|
|
||||||
|
|
||||||
db.ChangeDatabase("2");//use db2
|
|
||||||
await db.Deleteable<Order>().ExecuteCommandAsync();
|
|
||||||
Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
|
|
||||||
Console.WriteLine(db.Queryable<Order>().Count());
|
|
||||||
throw new Exception("");
|
|
||||||
|
|
||||||
});
|
|
||||||
result2.Wait();
|
|
||||||
if (result2.Result.IsSuccess == false)
|
|
||||||
{
|
|
||||||
Console.WriteLine("---Roll back");
|
|
||||||
db.ChangeDatabase("1");//use sqlserver
|
|
||||||
Console.WriteLine(db.CurrentConnectionConfig.DbType);
|
|
||||||
Console.WriteLine(db.Queryable<Order>().Count());
|
|
||||||
|
|
||||||
db.ChangeDatabase("2");//use mysql
|
|
||||||
Console.WriteLine(db.CurrentConnectionConfig.DbType);
|
|
||||||
Console.WriteLine(db.Queryable<Order>().Count());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
Loading…
Reference in New Issue
Block a user