ExportData: Split command batches of 1MiB.

This commit is contained in:
2019-08-02 08:48:59 +02:00
parent f0b5945c0a
commit 1356ee0e51
2 changed files with 18 additions and 5 deletions

View File

@@ -31,7 +31,9 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
txtWriter.WriteLine(string.Format("DELETE FROM {0};", tableFullName)); txtWriter.WriteLine(string.Format("DELETE FROM {0};", tableFullName));
txtWriter.WriteLine(string.Format("DBCC CHECKIDENT ('{0}', RESEED, 0);", tableFullName)); txtWriter.WriteLine(string.Format("DBCC CHECKIDENT ('{0}', RESEED, 0);", tableFullName));
txtWriter.WriteLine(string.Format("SET IDENTITY_INSERT {0} ON;", tableFullName)); txtWriter.WriteLine(string.Format("SET IDENTITY_INSERT {0} ON;", tableFullName));
txtWriter.WriteLine("GO");
DataTableHelper.DataTable_GenerateInserts(txtWriter, dtData, tableFullName); DataTableHelper.DataTable_GenerateInserts(txtWriter, dtData, tableFullName);
txtWriter.WriteLine("GO");
txtWriter.WriteLine(string.Format("SET IDENTITY_INSERT {0} OFF;", tableFullName)); txtWriter.WriteLine(string.Format("SET IDENTITY_INSERT {0} OFF;", tableFullName));
txtWriter.WriteLine("GO"); txtWriter.WriteLine("GO");
txtWriter.WriteLine(string.Empty); txtWriter.WriteLine(string.Empty);
@@ -43,6 +45,7 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
txtWriter.WriteLine(string.Format("-- {0}", tableFullName)); txtWriter.WriteLine(string.Format("-- {0}", tableFullName));
txtWriter.WriteLine(string.Format("DELETE FROM {0};", tableFullName)); txtWriter.WriteLine(string.Format("DELETE FROM {0};", tableFullName));
txtWriter.WriteLine("GO");
DataTableHelper.DataTable_GenerateInserts(txtWriter, dtData, tableFullName); DataTableHelper.DataTable_GenerateInserts(txtWriter, dtData, tableFullName);
txtWriter.WriteLine("GO"); txtWriter.WriteLine("GO");
txtWriter.WriteLine(string.Empty); txtWriter.WriteLine(string.Empty);

View File

@@ -10,6 +10,8 @@ namespace VAR.DatabaseExplorer.Code
{ {
public static void DataTable_GenerateInserts(TextWriter txtWriter, DataTable dataTable, string destTable) 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 nfi = new NumberFormatInfo { NumberDecimalSeparator = "." };
var sbColumns = new StringBuilder(); var sbColumns = new StringBuilder();
@@ -19,7 +21,7 @@ namespace VAR.DatabaseExplorer.Code
if (sbColumns.Length > 0) { sbColumns.Append(", "); } if (sbColumns.Length > 0) { sbColumns.Append(", "); }
sbColumns.AppendFormat("[{0}]", dc.ColumnName); sbColumns.AppendFormat("[{0}]", dc.ColumnName);
} }
int size = 0;
// Recorrer la tabla de datos // Recorrer la tabla de datos
foreach (DataRow dr in dataTable.Rows) foreach (DataRow dr in dataTable.Rows)
{ {
@@ -69,12 +71,12 @@ namespace VAR.DatabaseExplorer.Code
} }
else if (type == "byte[]") 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("-", "")); sbValues.AppendFormat("0x{0}", BitConverter.ToString(((byte[])valor)).Replace("-", ""));
} }
else if (type == "datetime") else if (type == "datetime")
{ {
// DateTIme // DateTime
sbValues.AppendFormat("'{0}'", ((DateTime)valor).ToString("yyyy-MM-dd HH:mm:ss")); sbValues.AppendFormat("'{0}'", ((DateTime)valor).ToString("yyyy-MM-dd HH:mm:ss"));
} }
else else
@@ -86,8 +88,16 @@ namespace VAR.DatabaseExplorer.Code
} }
// Insertar fila a la datatable destino // Insertar fila a la datatable destino
txtWriter.WriteLine(string.Format("INSERT INTO {0} ({1}) VALUES ({2});", string strLine = string.Format("INSERT INTO {0} ({1}) VALUES ({2});",
destTable, sbColumns.ToString(), sbValues.ToString())); destTable, sbColumns.ToString(), sbValues.ToString());
if ((size + strLine.Length) >= MaxSizePerBatch)
{
txtWriter.WriteLine("PRINT '...';");
txtWriter.WriteLine("GO");
size = 0;
}
size += strLine.Length;
txtWriter.WriteLine(strLine);
} }
} }
} }