ExportSchema: Security Schema script generation.
This commit is contained in:
@@ -135,5 +135,21 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
|
|||||||
}
|
}
|
||||||
} while (exportedOnLoopCount > 0);
|
} while (exportedOnLoopCount > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void Database_ExportSchemas(StreamWriter txtWriter, string connectionString, Database database)
|
||||||
|
{
|
||||||
|
// Preparar cabecera del script
|
||||||
|
txtWriter.WriteLine("SET NOCOUNT ON;");
|
||||||
|
txtWriter.WriteLine("GO");
|
||||||
|
txtWriter.WriteLine(string.Empty);
|
||||||
|
|
||||||
|
List<string> schemas = DatabaseDA.GetSchemas(connectionString);
|
||||||
|
foreach (string schema in schemas)
|
||||||
|
{
|
||||||
|
txtWriter.WriteLine(string.Format("CREATE SCHEMA [{0}];", schema));
|
||||||
|
txtWriter.WriteLine("GO");
|
||||||
|
txtWriter.WriteLine(string.Empty);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Data.SqlClient;
|
using System.Data.SqlClient;
|
||||||
|
using System.Linq;
|
||||||
using VAR.DatabaseExplorer.Code.DataTransfer;
|
using VAR.DatabaseExplorer.Code.DataTransfer;
|
||||||
|
|
||||||
namespace VAR.DatabaseExplorer.Code.DataAccess
|
namespace VAR.DatabaseExplorer.Code.DataAccess
|
||||||
@@ -53,5 +54,24 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
|
|||||||
};
|
};
|
||||||
return database;
|
return database;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static List<string> GetSchemas(string connectionString)
|
||||||
|
{
|
||||||
|
var cnx = new SqlConnection(connectionString);
|
||||||
|
string strCmd = string.Format(@"
|
||||||
|
select s.name as schema_name
|
||||||
|
from sys.schemas s
|
||||||
|
where s.principal_id = 1 AND schema_id != 1
|
||||||
|
order by s.name
|
||||||
|
");
|
||||||
|
var da = new SqlDataAdapter(strCmd, cnx);
|
||||||
|
var dt = new DataTable();
|
||||||
|
cnx.Open();
|
||||||
|
da.Fill(dt);
|
||||||
|
cnx.Close();
|
||||||
|
|
||||||
|
List<string> schemas = dt.AsEnumerable().Select(dr => Convert.ToString(dr["schema_name"])).ToList();
|
||||||
|
return schemas;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -130,6 +130,11 @@ namespace VAR.DatabaseExplorer.UI
|
|||||||
Database database = DatabaseBL.Database_GetSchema(connectionString: _connectionString, fillTableDefinitions: true, fillProcDefinitions: true);
|
Database database = DatabaseBL.Database_GetSchema(connectionString: _connectionString, fillTableDefinitions: true, fillProcDefinitions: true);
|
||||||
string fixedDatabaseName = database.Name.Replace(' ', '_');
|
string fixedDatabaseName = database.Name.Replace(' ', '_');
|
||||||
|
|
||||||
|
// Schemas
|
||||||
|
var streamWriterSchemas = new StreamWriter("01." + fixedDatabaseName + ".Schemas.sql");
|
||||||
|
DatabaseBL.Database_ExportSchemas(streamWriterSchemas, _connectionString, database);
|
||||||
|
streamWriterSchemas.Close();
|
||||||
|
|
||||||
// Tables
|
// Tables
|
||||||
var streamWriterTables = new StreamWriter(fixedDatabaseName + ".Tables.sql");
|
var streamWriterTables = new StreamWriter(fixedDatabaseName + ".Tables.sql");
|
||||||
DatabaseBL.Database_ExportTables(streamWriterTables, _connectionString, database);
|
DatabaseBL.Database_ExportTables(streamWriterTables, _connectionString, database);
|
||||||
|
|||||||
Reference in New Issue
Block a user