From e030ecd76518546fbb0a4858cfaeab7c65175288 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Mon, 7 Oct 2019 11:07:35 +0200 Subject: [PATCH] DatabaseBL.Database_ExportData: Split data files. --- .../Code/BusinessLogic/DatabaseBL.cs | 27 ++++++++++++++++++- VAR.DatabaseExplorer/UI/FrmBaseDatos.cs | 3 ++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/VAR.DatabaseExplorer/Code/BusinessLogic/DatabaseBL.cs b/VAR.DatabaseExplorer/Code/BusinessLogic/DatabaseBL.cs index 651e18d..406e719 100644 --- a/VAR.DatabaseExplorer/Code/BusinessLogic/DatabaseBL.cs +++ b/VAR.DatabaseExplorer/Code/BusinessLogic/DatabaseBL.cs @@ -67,6 +67,10 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic public static void Database_ExportData(TextWriter txtWriter, string connectionString, Database database) { + const int OneMegaByte = 1 * 1024 * 1024; + const int MaxSizePerBatch = OneMegaByte; + const int MaxSizePerFile = OneMegaByte * 32; + // Preparar cabecera del script txtWriter.WriteLine("SET NOCOUNT ON;"); txtWriter.WriteLine(string.Empty); @@ -84,9 +88,30 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic txtWriter.WriteLine(string.Empty); // Prepara información a exportar + long batchLength = 0; + long fileLength = 0; foreach (Table table in database.Tables) { - TableBL.Table_ExportData(table, txtWriter, connectionString); + TableBL.Table_ExportData(table, txtWriter, connectionString, (lenght) => + { + batchLength += lenght; + fileLength += lenght; + + if (batchLength > MaxSizePerBatch) + { + txtWriter.WriteLine("PRINT '...';"); + txtWriter.WriteLine("GO"); + batchLength = 0; + } + if (fileLength > MaxSizePerFile) + { + txtWriter.WriteLine("PRINT '...';"); + txtWriter.WriteLine("GO"); + SplittingStreamWriter.Split(txtWriter); + fileLength = 0; + batchLength = 0; + } + }); } // Activar todas las FKs diff --git a/VAR.DatabaseExplorer/UI/FrmBaseDatos.cs b/VAR.DatabaseExplorer/UI/FrmBaseDatos.cs index 4ea4ad5..1d86fab 100644 --- a/VAR.DatabaseExplorer/UI/FrmBaseDatos.cs +++ b/VAR.DatabaseExplorer/UI/FrmBaseDatos.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Windows.Forms; +using VAR.DatabaseExplorer.Code; using VAR.DatabaseExplorer.Code.BusinessLogic; using VAR.DatabaseExplorer.Code.DataAccess; using VAR.DatabaseExplorer.Code.DataTransfer; @@ -116,7 +117,7 @@ namespace VAR.DatabaseExplorer.UI Database database = DatabaseBL.Database_GetSchema(connectionString: _connectionString, fillTableDefinitions: true); string fixedDatabaseName = database.Name.Replace(' ', '_'); - var streamWriter = new StreamWriter("05." + fixedDatabaseName + ".Data.sql"); + var streamWriter = new SplittingStreamWriter("05." + fixedDatabaseName + ".{0:000}.Data.sql"); DatabaseBL.Database_ExportData(streamWriter, _connectionString, database); streamWriter.Close();