From 1356ee0e515c8b2eb6a343e8d1e4684343ecb53f Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Fri, 2 Aug 2019 08:48:59 +0200 Subject: [PATCH] ExportData: Split command batches of 1MiB. --- .../Code/BusinessLogic/TableBL.cs | 3 +++ VAR.DatabaseExplorer/Code/DataTableHelper.cs | 20 ++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/VAR.DatabaseExplorer/Code/BusinessLogic/TableBL.cs b/VAR.DatabaseExplorer/Code/BusinessLogic/TableBL.cs index 38fc176..1f3c5c4 100644 --- a/VAR.DatabaseExplorer/Code/BusinessLogic/TableBL.cs +++ b/VAR.DatabaseExplorer/Code/BusinessLogic/TableBL.cs @@ -31,7 +31,9 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic txtWriter.WriteLine(string.Format("DELETE FROM {0};", tableFullName)); txtWriter.WriteLine(string.Format("DBCC CHECKIDENT ('{0}', RESEED, 0);", tableFullName)); txtWriter.WriteLine(string.Format("SET IDENTITY_INSERT {0} ON;", tableFullName)); + txtWriter.WriteLine("GO"); DataTableHelper.DataTable_GenerateInserts(txtWriter, dtData, tableFullName); + txtWriter.WriteLine("GO"); txtWriter.WriteLine(string.Format("SET IDENTITY_INSERT {0} OFF;", tableFullName)); txtWriter.WriteLine("GO"); txtWriter.WriteLine(string.Empty); @@ -43,6 +45,7 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic txtWriter.WriteLine(string.Format("-- {0}", tableFullName)); txtWriter.WriteLine(string.Format("DELETE FROM {0};", tableFullName)); + txtWriter.WriteLine("GO"); DataTableHelper.DataTable_GenerateInserts(txtWriter, dtData, tableFullName); txtWriter.WriteLine("GO"); txtWriter.WriteLine(string.Empty); diff --git a/VAR.DatabaseExplorer/Code/DataTableHelper.cs b/VAR.DatabaseExplorer/Code/DataTableHelper.cs index a2d34f2..595f7af 100644 --- a/VAR.DatabaseExplorer/Code/DataTableHelper.cs +++ b/VAR.DatabaseExplorer/Code/DataTableHelper.cs @@ -10,6 +10,8 @@ namespace VAR.DatabaseExplorer.Code { public static void DataTable_GenerateInserts(TextWriter txtWriter, DataTable dataTable, string destTable) { + const int OneMegaByte = 1 * 1024 * 1024; + const int MaxSizePerBatch = OneMegaByte; var nfi = new NumberFormatInfo { NumberDecimalSeparator = "." }; var sbColumns = new StringBuilder(); @@ -19,7 +21,7 @@ namespace VAR.DatabaseExplorer.Code if (sbColumns.Length > 0) { sbColumns.Append(", "); } sbColumns.AppendFormat("[{0}]", dc.ColumnName); } - + int size = 0; // Recorrer la tabla de datos foreach (DataRow dr in dataTable.Rows) { @@ -69,12 +71,12 @@ namespace VAR.DatabaseExplorer.Code } else if (type == "byte[]") { - // Arrays de bytes (imagenes, archivos etc) + // Arrays de bytes (imágenes, archivos etc) sbValues.AppendFormat("0x{0}", BitConverter.ToString(((byte[])valor)).Replace("-", "")); } else if (type == "datetime") { - // DateTIme + // DateTime sbValues.AppendFormat("'{0}'", ((DateTime)valor).ToString("yyyy-MM-dd HH:mm:ss")); } else @@ -86,8 +88,16 @@ namespace VAR.DatabaseExplorer.Code } // Insertar fila a la datatable destino - txtWriter.WriteLine(string.Format("INSERT INTO {0} ({1}) VALUES ({2});", - destTable, sbColumns.ToString(), sbValues.ToString())); + string strLine = string.Format("INSERT INTO {0} ({1}) VALUES ({2});", + destTable, sbColumns.ToString(), sbValues.ToString()); + if ((size + strLine.Length) >= MaxSizePerBatch) + { + txtWriter.WriteLine("PRINT '...';"); + txtWriter.WriteLine("GO"); + size = 0; + } + size += strLine.Length; + txtWriter.WriteLine(strLine); } } }