From 8a76bf0f736f7ac6f8314cd56e537accc9d98ec0 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Mon, 14 Mar 2022 10:52:11 +0800 Subject: [PATCH] Update demo& Add CreateIndex(+4) --- Src/Asp.Net/SqlServerTest/Demo/Demo0_SqlSugarClient.cs | 9 +++++++-- .../SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs | 6 ++++++ Src/Asp.Net/SqlSugar/Interface/IDbMaintenance.cs | 1 + 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Src/Asp.Net/SqlServerTest/Demo/Demo0_SqlSugarClient.cs b/Src/Asp.Net/SqlServerTest/Demo/Demo0_SqlSugarClient.cs index df09d0875..80b06d7c4 100644 --- a/Src/Asp.Net/SqlServerTest/Demo/Demo0_SqlSugarClient.cs +++ b/Src/Asp.Net/SqlServerTest/Demo/Demo0_SqlSugarClient.cs @@ -16,7 +16,7 @@ namespace OrmTest SqlSugarClient();//Create db DbContext();//Optimizing SqlSugarClient usage SingletonPattern();//Singleten Pattern - //DistributedTransactionExample(); The demo requires three different databases + DistributedTransactionExample(); MasterSlave();//Read-write separation 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="2", DbType=DbType.SqlServer, 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(); @@ -237,11 +237,16 @@ namespace OrmTest //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().Count()); + if (db2 == db1) + { + return; + } // Example 1 Console.WriteLine("Example 1"); try diff --git a/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs b/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs index 100c02c88..12bf743fa 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs @@ -325,6 +325,12 @@ namespace SqlSugar this.Context.Ado.ExecuteCommand(sql); 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) { string sql = string.Format(this.IsAnyIndexSql, indexName); diff --git a/Src/Asp.Net/SqlSugar/Interface/IDbMaintenance.cs b/Src/Asp.Net/SqlSugar/Interface/IDbMaintenance.cs index b28ccd38a..1a00a863b 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IDbMaintenance.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IDbMaintenance.cs @@ -29,6 +29,7 @@ namespace SqlSugar #region DDL bool AddDefaultValue(string tableName,string columnName,string defaultValue); 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 TruncateTable(string tableName); bool TruncateTable();