From 1527fea2bcccd47dc32114de48799d1f2b8d6aa6 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Mon, 7 Oct 2019 11:05:19 +0200 Subject: [PATCH] TableRow: Replacement of DataRow. --- .../Code/DataTransfer/TableRow.cs | 17 +++ VAR.DatabaseExplorer/Code/TableRowHelper.cs | 107 ++++++++++++++++++ .../VAR.DatabaseExplorer.csproj | 2 + 3 files changed, 126 insertions(+) create mode 100644 VAR.DatabaseExplorer/Code/DataTransfer/TableRow.cs create mode 100644 VAR.DatabaseExplorer/Code/TableRowHelper.cs diff --git a/VAR.DatabaseExplorer/Code/DataTransfer/TableRow.cs b/VAR.DatabaseExplorer/Code/DataTransfer/TableRow.cs new file mode 100644 index 0000000..39791a2 --- /dev/null +++ b/VAR.DatabaseExplorer/Code/DataTransfer/TableRow.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; + +namespace VAR.DatabaseExplorer.Code.DataTransfer +{ + public class TableRow + { + public List Cells { get; } = new List(); + } + + public class TableCell + { + public string Name { get; set; } + public object Data { get; set; } + public Type DataType { get; set; } + } +} diff --git a/VAR.DatabaseExplorer/Code/TableRowHelper.cs b/VAR.DatabaseExplorer/Code/TableRowHelper.cs new file mode 100644 index 0000000..4973666 --- /dev/null +++ b/VAR.DatabaseExplorer/Code/TableRowHelper.cs @@ -0,0 +1,107 @@ +using System; +using System.Data; +using System.Globalization; +using System.IO; +using System.Text; +using VAR.DatabaseExplorer.Code.DataTransfer; + +namespace VAR.DatabaseExplorer.Code +{ + public class TableRowHelper + { + public static TableRow ConvertFromDataRecord(IDataRecord record) + { + var row = new TableRow(); + int columnNumber = record.FieldCount; + for (int i = 0; i < columnNumber; i++) + { + TableCell cell = new TableCell + { + Name = record.GetName(i), + Data = record.GetValue(i), + DataType = record.GetFieldType(i), + }; + row.Cells.Add(cell); + } + return row; + } + + public static int TableRow_GenerateInsert(TextWriter txtWriter, TableRow row, string destTable) + { + var nfi = new NumberFormatInfo { NumberDecimalSeparator = "." }; + + var sbColumns = new StringBuilder(); + foreach (TableCell cell in row.Cells) + { + // El nombre de la columna + if (sbColumns.Length > 0) { sbColumns.Append(", "); } + sbColumns.AppendFormat("[{0}]", cell.Name); + } + var sbValues = new StringBuilder(); + foreach (TableCell cell in row.Cells) + { + // El valor de la columna + if (sbValues.Length > 0) { sbValues.Append(", "); } + object valor = cell.Data; + if (valor == DBNull.Value || valor == null) + { + // NULOS + sbValues.Append("NULL"); + } + else + { + string type = cell.DataType.Name.ToLower(); + if (type == "string") + { + // Cadenas + sbValues.AppendFormat("N'{0}'", ((string)valor).Replace("'", "''")); + } + else if (type == "short" || type == "int16") + { + // short + sbValues.Append(((short)valor).ToString(nfi)); + } + else if (type == "int" || type == "int32") + { + // int + sbValues.Append(((int)valor).ToString(nfi)); + } + else if (type == "long" || type == "int64") + { + // long + sbValues.Append(((long)valor).ToString(nfi)); + } + else if (type == "decimal") + { + // Decimales + sbValues.Append(((decimal)valor).ToString(nfi)); + } + else if (type == "bit" || type == "bool" || type == "boolean") + { + // Booleanos + sbValues.Append(((bool)valor) ? "1" : "0"); + } + else if (type == "byte[]") + { + // Arrays de bytes (imágenes, archivos etc) + sbValues.AppendFormat("0x{0}", BitConverter.ToString(((byte[])valor)).Replace("-", "")); + } + else if (type == "datetime") + { + // DateTime + sbValues.AppendFormat("'{0}'", ((DateTime)valor).ToString("yyyy-MM-dd HH:mm:ss")); + } + else + { + // Otros + sbValues.AppendFormat("'{0}'", valor.ToString()); + } + } + } + string strLine = string.Format("INSERT INTO {0} ({1}) VALUES ({2});", + destTable, sbColumns.ToString(), sbValues.ToString()); + txtWriter.WriteLine(strLine); + return strLine.Length; + } + } +} diff --git a/VAR.DatabaseExplorer/VAR.DatabaseExplorer.csproj b/VAR.DatabaseExplorer/VAR.DatabaseExplorer.csproj index 6ecda9a..3e90f34 100644 --- a/VAR.DatabaseExplorer/VAR.DatabaseExplorer.csproj +++ b/VAR.DatabaseExplorer/VAR.DatabaseExplorer.csproj @@ -90,12 +90,14 @@ + + Component