diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2021-04-06 09:59:51 +0300 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2021-04-06 09:59:51 +0300 |
| commit | 4b05c48c25b88fe42e1d03b1dd768225464e812c (patch) | |
| tree | 5702b57eda013ea73a8dc17112db2a2d26690571 /Software/Visual_Studio/Tango.Core | |
| parent | 5be2f6157bd390b0415de652be83687ad0d1a38b (diff) | |
| download | Tango-4b05c48c25b88fe42e1d03b1dd768225464e812c.tar.gz Tango-4b05c48c25b88fe42e1d03b1dd768225464e812c.zip | |
Fixed ClearDataBase.
Diffstat (limited to 'Software/Visual_Studio/Tango.Core')
| -rw-r--r-- | Software/Visual_Studio/Tango.Core/DB/DbManager.cs | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/Software/Visual_Studio/Tango.Core/DB/DbManager.cs b/Software/Visual_Studio/Tango.Core/DB/DbManager.cs index d6eb521bd..c23b6a39a 100644 --- a/Software/Visual_Studio/Tango.Core/DB/DbManager.cs +++ b/Software/Visual_Studio/Tango.Core/DB/DbManager.cs @@ -192,13 +192,44 @@ namespace Tango.Core.DB SqlCommand cmd = new SqlCommand("EXEC sys.sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'", _connection); cmd.ExecuteNonQuery(); - cmd = new SqlCommand("EXEC sys.sp_msforeachtable 'DELETE FROM ?'", _connection); + cmd = new SqlCommand("EXEC sys.sp_msforeachtable 'SET QUOTED_IDENTIFIER ON; DELETE FROM ?'", _connection); cmd.ExecuteNonQuery(); cmd = new SqlCommand("EXEC sys.sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'", _connection); cmd.ExecuteNonQuery(); } + public void DropAllTables() + { + if (!_connection.ConnectionString.ToLower().Contains("initial catalog")) + { + throw new InvalidOperationException("Clear db can only be used when 'initial catalog' is specified at the connection string."); + } + + SqlCommand cmd = new SqlCommand(@"USE TANGO +DECLARE @sql NVARCHAR(2000) + +WHILE(EXISTS(SELECT 1 from INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_TYPE='FOREIGN KEY')) +BEGIN + SELECT TOP 1 @sql=('ALTER TABLE ' + TABLE_SCHEMA + '.[' + TABLE_NAME + '] DROP CONSTRAINT [' + CONSTRAINT_NAME + ']') + FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS + WHERE CONSTRAINT_TYPE = 'FOREIGN KEY' + EXEC(@sql) + PRINT @sql +END + +WHILE(EXISTS(SELECT * from INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME != '__MigrationHistory' AND TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME != 'database_firewall_rules')) +BEGIN + SELECT TOP 1 @sql=('DROP TABLE ' + TABLE_SCHEMA + '.[' + TABLE_NAME + ']') + FROM INFORMATION_SCHEMA.TABLES + WHERE TABLE_NAME != '__MigrationHistory' AND TABLE_NAME != 'database_firewall_rules' + EXEC(@sql) + PRINT @sql +END", _connection); + + cmd.ExecuteNonQuery(); + } + public void CreateLoginAndUser(String userName, String password, String database) { |
