Browse Source

Update demo& Add CreateIndex(+4)

pull/14/head
sunkaixuan 3 years ago
parent
commit
8a76bf0f73
  1. 9
      Src/Asp.Net/SqlServerTest/Demo/Demo0_SqlSugarClient.cs
  2. 6
      Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs
  3. 1
      Src/Asp.Net/SqlSugar/Interface/IDbMaintenance.cs

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

@ -16,7 +16,7 @@ namespace OrmTest
SqlSugarClient();//Create db SqlSugarClient();//Create db
DbContext();//Optimizing SqlSugarClient usage DbContext();//Optimizing SqlSugarClient usage
SingletonPattern();//Singleten Pattern SingletonPattern();//Singleten Pattern
//DistributedTransactionExample(); The demo requires three different databases DistributedTransactionExample();
MasterSlave();//Read-write separation MasterSlave();//Read-write separation
CustomAttribute(); CustomAttribute();
} }
@ -229,7 +229,7 @@ namespace OrmTest
new ConnectionConfig(){ ConfigId="1", DbType=DbType.SqlServer, ConnectionString=Config.ConnectionString,InitKeyType=InitKeyType.Attribute,IsAutoCloseConnection=true }, new ConnectionConfig(){ ConfigId="1", DbType=DbType.SqlServer, ConnectionString=Config.ConnectionString,InitKeyType=InitKeyType.Attribute,IsAutoCloseConnection=true },
new ConnectionConfig(){ ConfigId="2", DbType=DbType.SqlServer, ConnectionString=Config.ConnectionString2 ,InitKeyType=InitKeyType.Attribute ,IsAutoCloseConnection=true} new ConnectionConfig(){ ConfigId="2", DbType=DbType.SqlServer, ConnectionString=Config.ConnectionString2 ,InitKeyType=InitKeyType.Attribute ,IsAutoCloseConnection=true}
}); });
var db1 = db.Ado.Connection.Database;
//use db1 //use db1
db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Order), typeof(OrderItem));// db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Order), typeof(OrderItem));//
db.Insertable(new Order() { Name = "order1", CreateTime = DateTime.Now }).ExecuteCommand(); db.Insertable(new Order() { Name = "order1", CreateTime = DateTime.Now }).ExecuteCommand();
@ -237,11 +237,16 @@ namespace OrmTest
//use db2 //use db2
db.ChangeDatabase("2"); db.ChangeDatabase("2");
var db2 = db.Ado.Connection.Database;
db.DbMaintenance.CreateDatabase();//Create Database2 db.DbMaintenance.CreateDatabase();//Create Database2
db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Order), typeof(OrderItem)); db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Order), typeof(OrderItem));
db.Insertable(new Order() { Name = "order1", CreateTime = DateTime.Now }).ExecuteCommand(); db.Insertable(new Order() { Name = "order1", CreateTime = DateTime.Now }).ExecuteCommand();
Console.WriteLine(db.CurrentConnectionConfig.DbType + ":" + db.Queryable<Order>().Count()); Console.WriteLine(db.CurrentConnectionConfig.DbType + ":" + db.Queryable<Order>().Count());
if (db2 == db1)
{
return;
}
// Example 1 // Example 1
Console.WriteLine("Example 1"); Console.WriteLine("Example 1");
try try

6
Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs

@ -325,6 +325,12 @@ namespace SqlSugar
this.Context.Ado.ExecuteCommand(sql); this.Context.Ado.ExecuteCommand(sql);
return true; return true;
} }
public virtual bool CreateIndex(string tableName, string[] columnNames, string IndexName, bool isUnique = false)
{
string sql = string.Format("CREATE {3} INDEX {2} ON {0}({1})", tableName, string.Join(",", columnNames), IndexName, isUnique ? "UNIQUE" : "");
this.Context.Ado.ExecuteCommand(sql);
return true;
}
public virtual bool IsAnyIndex(string indexName) public virtual bool IsAnyIndex(string indexName)
{ {
string sql = string.Format(this.IsAnyIndexSql, indexName); string sql = string.Format(this.IsAnyIndexSql, indexName);

1
Src/Asp.Net/SqlSugar/Interface/IDbMaintenance.cs

@ -29,6 +29,7 @@ namespace SqlSugar
#region DDL #region DDL
bool AddDefaultValue(string tableName,string columnName,string defaultValue); bool AddDefaultValue(string tableName,string columnName,string defaultValue);
bool CreateIndex(string tableName, string [] columnNames, bool isUnique=false); bool CreateIndex(string tableName, string [] columnNames, bool isUnique=false);
bool CreateIndex(string tableName, string[] columnNames, string IndexName, bool isUnique = false);
bool DropTable(string tableName); bool DropTable(string tableName);
bool TruncateTable(string tableName); bool TruncateTable(string tableName);
bool TruncateTable<T>(); bool TruncateTable<T>();

Loading…
Cancel
Save