From 0d159402b5e126a9e748e03b7d6f693716b05422 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Mon, 7 Oct 2019 11:07:00 +0200 Subject: [PATCH] TableDA.GetData: Convert to coroutine of TableRow. --- .../Code/BusinessLogic/TableBL.cs | 19 ++++++++++++++----- .../Code/DataAccess/TableDA.cs | 14 ++++++++------ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/VAR.DatabaseExplorer/Code/BusinessLogic/TableBL.cs b/VAR.DatabaseExplorer/Code/BusinessLogic/TableBL.cs index 3e5cca2..5bf0ee3 100644 --- a/VAR.DatabaseExplorer/Code/BusinessLogic/TableBL.cs +++ b/VAR.DatabaseExplorer/Code/BusinessLogic/TableBL.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; @@ -41,13 +42,13 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic return tables; } - public static void Table_ExportData(Table t, TextWriter txtWriter, string conectionString) + public static void Table_ExportData(Table t, TextWriter txtWriter, string conectionString, Action notifyLenght = null) { if (t.Type != "BASE TABLE") { return; } string tableName = string.Format("{0}.{1}", t.Schema, t.Name); txtWriter.WriteLine(string.Format("PRINT '*** Importing data of {0}....';", tableName)); - DataTable dtData = TableDA.GetData(conectionString, tableName); + IEnumerable rows = TableDA.GetData(conectionString, tableName); if (t.Columns.Any(c => c.Indentity)) { txtWriter.WriteLine(string.Format("-- {0}", tableName)); @@ -55,7 +56,11 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic txtWriter.WriteLine(string.Format("DBCC CHECKIDENT ('{0}', RESEED, 0);", tableName)); txtWriter.WriteLine(string.Format("SET IDENTITY_INSERT {0} ON;", tableName)); txtWriter.WriteLine("GO"); - DataTableHelper.DataTable_GenerateInserts(txtWriter, dtData, tableName); + foreach (TableRow row in rows) + { + int lenght = TableRowHelper.TableRow_GenerateInsert(txtWriter, row, tableName); + notifyLenght?.Invoke(lenght); + } txtWriter.WriteLine("GO"); txtWriter.WriteLine(string.Format("SET IDENTITY_INSERT {0} OFF;", tableName)); txtWriter.WriteLine("GO"); @@ -66,7 +71,11 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic txtWriter.WriteLine(string.Format("-- {0}", tableName)); txtWriter.WriteLine(string.Format("DELETE FROM {0};", tableName)); txtWriter.WriteLine("GO"); - DataTableHelper.DataTable_GenerateInserts(txtWriter, dtData, tableName); + foreach (TableRow row in rows) + { + int lenght = TableRowHelper.TableRow_GenerateInsert(txtWriter, row, tableName); + notifyLenght?.Invoke(lenght); + } txtWriter.WriteLine("GO"); txtWriter.WriteLine(string.Empty); } diff --git a/VAR.DatabaseExplorer/Code/DataAccess/TableDA.cs b/VAR.DatabaseExplorer/Code/DataAccess/TableDA.cs index d262f01..6e12b2e 100644 --- a/VAR.DatabaseExplorer/Code/DataAccess/TableDA.cs +++ b/VAR.DatabaseExplorer/Code/DataAccess/TableDA.cs @@ -81,17 +81,19 @@ namespace VAR.DatabaseExplorer.Code.DataAccess return definitions; } - public static DataTable GetData(string connectionString, string tableFullName) + public static IEnumerable GetData(string connectionString, string tableFullName) { var cnx = new SqlConnection(connectionString); string strCmd = string.Format("SELECT * FROM {0}", tableFullName); - var da = new SqlDataAdapter(strCmd, cnx); - var dt = new DataTable(); + var cmd = new SqlCommand(strCmd, cnx); cnx.Open(); - da.Fill(dt); + SqlDataReader reader = cmd.ExecuteReader(); + while (reader.Read()) + { + TableRow row = TableRowHelper.ConvertFromDataRecord(reader); + yield return row; + } cnx.Close(); - - return dt; } } } \ No newline at end of file