Fixes on ExportData.

This commit is contained in:
2019-07-29 02:40:19 +02:00
parent 70f0eeb7ee
commit f0b5945c0a
3 changed files with 40 additions and 6 deletions

View File

@@ -1,4 +1,5 @@
using System.IO;
using System.Linq;
using VAR.DatabaseExplorer.Code.DataAccess;
using VAR.DatabaseExplorer.Code.DataTransfer;
@@ -71,21 +72,42 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
// Desactivar todas las FKs
txtWriter.WriteLine("-- Disable all constraints");
txtWriter.WriteLine("EXEC sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT all'");
foreach (Table t in database.Tables)
{
if (t.Type != "BASE TABLE") { continue; }
string tableName = string.Format("[{0}].[{1}]", t.Schema, t.Name);
txtWriter.WriteLine(string.Format("ALTER TABLE {0} NOCHECK CONSTRAINT all;", tableName));
}
txtWriter.WriteLine("GO");
txtWriter.WriteLine(string.Empty);
// Prepara informacion a exportar
// Prepara información a exportar
foreach (Table t in database.Tables)
{
if (t.Type != "BASE TABLE") { continue; }
string tableName = string.Format("{0}.{1}", t.Schema, t.Name);
txtWriter.WriteLine(string.Format("PRINT '*** Importing data of {0}....';", tableName));
TableBL.Table_ExportData(txtWriter, connectionString, tableName);
if (t.Columns.Any(c => c.Indentity))
{
TableBL.Table_ExportDataWithIdentity(t, txtWriter, connectionString, tableName);
}
else
{
TableBL.Table_ExportData(t, txtWriter, connectionString, tableName);
}
}
// Activar todas las FKs
txtWriter.WriteLine("-- Enable all constraints");
txtWriter.WriteLine("EXEC sp_MSforeachtable @command1='print ''?''', @command2='ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all'");
foreach (Table t in database.Tables)
{
if (t.Type != "BASE TABLE") { continue; }
string tableName = string.Format("[{0}].[{1}]", t.Schema, t.Name);
txtWriter.WriteLine(string.Format("ALTER TABLE {0} WITH CHECK CHECK CONSTRAINT all;", tableName));
}
txtWriter.WriteLine("GO");
txtWriter.WriteLine(string.Empty);
}

View File

@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using VAR.DatabaseExplorer.Code.DataAccess;
using VAR.DatabaseExplorer.Code.DataTransfer;
@@ -22,7 +23,7 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
return tables;
}
public static void Table_ExportData(TextWriter txtWriter, string conectionString, string tableFullName)
public static void Table_ExportDataWithIdentity(Table t, TextWriter txtWriter, string conectionString, string tableFullName)
{
DataTable dtData = TableDA.GetData(conectionString, tableFullName);
@@ -35,5 +36,16 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
txtWriter.WriteLine("GO");
txtWriter.WriteLine(string.Empty);
}
public static void Table_ExportData(Table t, TextWriter txtWriter, string conectionString, string tableFullName)
{
DataTable dtData = TableDA.GetData(conectionString, tableFullName);
txtWriter.WriteLine(string.Format("-- {0}", tableFullName));
txtWriter.WriteLine(string.Format("DELETE FROM {0};", tableFullName));
DataTableHelper.DataTable_GenerateInserts(txtWriter, dtData, tableFullName);
txtWriter.WriteLine("GO");
txtWriter.WriteLine(string.Empty);
}
}
}

View File

@@ -40,7 +40,7 @@ namespace VAR.DatabaseExplorer.Code
if (type == "string")
{
// Cadenas
sbValues.AppendFormat("'{0}'", ((string)valor).Replace("'", "''"));
sbValues.AppendFormat("N'{0}'", ((string)valor).Replace("'", "''"));
}
else if (type == "short" || type == "int16")
{