mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 22:11:36 +08:00
Update ClickHouse Demo
This commit is contained in:
parent
7f257fbc51
commit
296a143880
@ -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();
|
||||||
}
|
}
|
||||||
@ -219,131 +218,7 @@ namespace OrmTest
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
private static void DistributedTransactionExample()
|
|
||||||
{
|
|
||||||
Console.WriteLine("");
|
|
||||||
Console.WriteLine("#### Distributed TransactionExample Start ####");
|
|
||||||
SqlSugarClient db = new SqlSugarClient(new List<ConnectionConfig>()
|
|
||||||
{
|
|
||||||
new ConnectionConfig(){ ConfigId="1", DbType=DbType.ClickHouse, ConnectionString=Config.ConnectionString,InitKeyType=InitKeyType.Attribute,IsAutoCloseConnection=true },
|
|
||||||
new ConnectionConfig(){ ConfigId="2", DbType=DbType.ClickHouse, 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