From f0b5945c0a0c5ddd1cc02864a093e793e8138d64 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Mon, 29 Jul 2019 02:40:19 +0200 Subject: [PATCH] Fixes on ExportData. --- .../Code/BusinessLogic/DatabaseBL.cs | 30 ++++++++++++++++--- .../Code/BusinessLogic/TableBL.cs | 14 ++++++++- VAR.DatabaseExplorer/Code/DataTableHelper.cs | 2 +- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/VAR.DatabaseExplorer/Code/BusinessLogic/DatabaseBL.cs b/VAR.DatabaseExplorer/Code/BusinessLogic/DatabaseBL.cs index 9f81952..902b239 100644 --- a/VAR.DatabaseExplorer/Code/BusinessLogic/DatabaseBL.cs +++ b/VAR.DatabaseExplorer/Code/BusinessLogic/DatabaseBL.cs @@ -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); } diff --git a/VAR.DatabaseExplorer/Code/BusinessLogic/TableBL.cs b/VAR.DatabaseExplorer/Code/BusinessLogic/TableBL.cs index 96534cc..38fc176 100644 --- a/VAR.DatabaseExplorer/Code/BusinessLogic/TableBL.cs +++ b/VAR.DatabaseExplorer/Code/BusinessLogic/TableBL.cs @@ -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); + } } } \ No newline at end of file diff --git a/VAR.DatabaseExplorer/Code/DataTableHelper.cs b/VAR.DatabaseExplorer/Code/DataTableHelper.cs index f7d9702..a2d34f2 100644 --- a/VAR.DatabaseExplorer/Code/DataTableHelper.cs +++ b/VAR.DatabaseExplorer/Code/DataTableHelper.cs @@ -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") {