TableDA.GetData: Convert to coroutine of TableRow.

This commit is contained in:
2019-10-07 11:07:00 +02:00
parent d0261de802
commit 0d159402b5
2 changed files with 22 additions and 11 deletions

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Data; using System.Data;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@@ -41,13 +42,13 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
return tables; 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<int> notifyLenght = null)
{ {
if (t.Type != "BASE TABLE") { return; } if (t.Type != "BASE TABLE") { return; }
string tableName = string.Format("{0}.{1}", t.Schema, t.Name); string tableName = string.Format("{0}.{1}", t.Schema, t.Name);
txtWriter.WriteLine(string.Format("PRINT '*** Importing data of {0}....';", tableName)); txtWriter.WriteLine(string.Format("PRINT '*** Importing data of {0}....';", tableName));
DataTable dtData = TableDA.GetData(conectionString, tableName); IEnumerable<TableRow> rows = TableDA.GetData(conectionString, tableName);
if (t.Columns.Any(c => c.Indentity)) if (t.Columns.Any(c => c.Indentity))
{ {
txtWriter.WriteLine(string.Format("-- {0}", tableName)); 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("DBCC CHECKIDENT ('{0}', RESEED, 0);", tableName));
txtWriter.WriteLine(string.Format("SET IDENTITY_INSERT {0} ON;", tableName)); txtWriter.WriteLine(string.Format("SET IDENTITY_INSERT {0} ON;", tableName));
txtWriter.WriteLine("GO"); 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("GO");
txtWriter.WriteLine(string.Format("SET IDENTITY_INSERT {0} OFF;", tableName)); txtWriter.WriteLine(string.Format("SET IDENTITY_INSERT {0} OFF;", tableName));
txtWriter.WriteLine("GO"); txtWriter.WriteLine("GO");
@@ -66,7 +71,11 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
txtWriter.WriteLine(string.Format("-- {0}", tableName)); txtWriter.WriteLine(string.Format("-- {0}", tableName));
txtWriter.WriteLine(string.Format("DELETE FROM {0};", tableName)); txtWriter.WriteLine(string.Format("DELETE FROM {0};", tableName));
txtWriter.WriteLine("GO"); 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("GO");
txtWriter.WriteLine(string.Empty); txtWriter.WriteLine(string.Empty);
} }

View File

@@ -81,17 +81,19 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
return definitions; return definitions;
} }
public static DataTable GetData(string connectionString, string tableFullName) public static IEnumerable<TableRow> GetData(string connectionString, string tableFullName)
{ {
var cnx = new SqlConnection(connectionString); var cnx = new SqlConnection(connectionString);
string strCmd = string.Format("SELECT * FROM {0}", tableFullName); string strCmd = string.Format("SELECT * FROM {0}", tableFullName);
var da = new SqlDataAdapter(strCmd, cnx); var cmd = new SqlCommand(strCmd, cnx);
var dt = new DataTable();
cnx.Open(); cnx.Open();
da.Fill(dt); SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
TableRow row = TableRowHelper.ConvertFromDataRecord(reader);
yield return row;
}
cnx.Close(); cnx.Close();
return dt;
} }
} }
} }