Browse Source

Update demo

pull/12/MERGE
sunkaixuan 6 years ago
parent
commit
f27ab97496
  1. 5
      Src/Asp.Net/SqlServerTest/Config.cs
  2. 38
      Src/Asp.Net/SqlServerTest/Demo/Demo0_SqlSugarClient.cs

5
Src/Asp.Net/SqlServerTest/Config.cs

@ -9,8 +9,7 @@ namespace OrmTest
public class Config
{
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";
public static string ConnectionString2 = "server=.;uid=sa;pwd=haosql;database=SQLSUGAR4XTEST2";
public static string ConnectionString3 = "server=.;uid=sa;pwd=haosql;database=SQLSUGAR4XTEST3";
}
}

38
Src/Asp.Net/SqlServerTest/Demo/Demo0_SqlSugarClient.cs

@ -198,41 +198,33 @@ namespace OrmTest
SqlSugarClient db = new SqlSugarClient(new List<ConnectionConfig>()
{
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}
new ConnectionConfig(){ ConfigId=2, DbType=DbType.SqlServer, ConnectionString=Config.ConnectionString2 ,InitKeyType=InitKeyType.Attribute ,IsAutoCloseConnection=true}
});
db.MappingTables.Add(typeof(Order).Name, typeof(Order).Name + "2018");
db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Order));
db.MappingTables.Add(typeof(Order).Name, typeof(Order).Name + "2019");
db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Order));//
//use first(SqlServer)
//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 mysql
db.ChangeDatabase(it => it.DbType == DbType.MySql);
//use db2
db.ChangeDatabase("2");
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());
//SqlServer
db.ChangeDatabase(it => it.DbType == DbType.SqlServer);//use sqlserver
// Example 1
Console.WriteLine("Example 1");
try
{
db.BeginTran();
db.ChangeDatabase(it => it.DbType == DbType.SqlServer);//use sqlserver
db.ChangeDatabase("1");//use db1
db.Deleteable<Order>().ExecuteCommand();
Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
Console.WriteLine(db.Queryable<Order>().Count());
db.ChangeDatabase(it => it.DbType == DbType.MySql);//use mysql
db.ChangeDatabase("2");//use db2
db.Deleteable<Order>().ExecuteCommand();
Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
Console.WriteLine(db.Queryable<Order>().Count());
@ -244,11 +236,11 @@ namespace OrmTest
{
db.RollbackTran();
Console.WriteLine("---Roll back");
db.ChangeDatabase(it => it.DbType == DbType.SqlServer);//use sqlserver
db.ChangeDatabase("1");//use db1
Console.WriteLine(db.CurrentConnectionConfig.DbType);
Console.WriteLine(db.Queryable<Order>().Count());
db.ChangeDatabase(it => it.DbType == DbType.MySql);//use mysql
db.ChangeDatabase("2");//use db2
Console.WriteLine(db.CurrentConnectionConfig.DbType);
Console.WriteLine(db.Queryable<Order>().Count());
}
@ -261,12 +253,12 @@ namespace OrmTest
var result=db.UseTran(() =>
{
db.ChangeDatabase(it => it.DbType == DbType.SqlServer);//use sqlserver
db.ChangeDatabase("1");//use db1
db.Deleteable<Order>().ExecuteCommand();
Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
Console.WriteLine(db.Queryable<Order>().Count());
db.ChangeDatabase(it => it.DbType == DbType.MySql);//use mysql
db.ChangeDatabase("2");//use db2
db.Deleteable<Order>().ExecuteCommand();
Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
Console.WriteLine(db.Queryable<Order>().Count());
@ -275,11 +267,11 @@ namespace OrmTest
});
if (result.IsSuccess == false) {
Console.WriteLine("---Roll back");
db.ChangeDatabase(it => it.DbType == DbType.SqlServer);//use sqlserver
db.ChangeDatabase("1");//use db1
Console.WriteLine(db.CurrentConnectionConfig.DbType);
Console.WriteLine(db.Queryable<Order>().Count());
db.ChangeDatabase(it => it.DbType == DbType.MySql);//use mysql
db.ChangeDatabase("2");//use db2
Console.WriteLine(db.CurrentConnectionConfig.DbType);
Console.WriteLine(db.Queryable<Order>().Count());
}
@ -290,12 +282,12 @@ namespace OrmTest
var result2 = db.UseTranAsync(() =>
{
db.ChangeDatabase(it => it.DbType == DbType.SqlServer);//use sqlserver
db.ChangeDatabase("1");//use db1
db.Deleteable<Order>().ExecuteCommand();
Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
Console.WriteLine(db.Queryable<Order>().Count());
db.ChangeDatabase(it => it.DbType == DbType.MySql);//use mysql
db.ChangeDatabase("2");//use db2
db.Deleteable<Order>().ExecuteCommand();
Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
Console.WriteLine(db.Queryable<Order>().Count());

Loading…
Cancel
Save