TableRow: Replacement of DataRow.

This commit is contained in:
2019-10-07 11:05:19 +02:00
parent 3dbcf02099
commit 1527fea2bc
3 changed files with 126 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
namespace VAR.DatabaseExplorer.Code.DataTransfer
{
public class TableRow
{
public List<TableCell> Cells { get; } = new List<TableCell>();
}
public class TableCell
{
public string Name { get; set; }
public object Data { get; set; }
public Type DataType { get; set; }
}
}

View File

@@ -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;
}
}
}

View File

@@ -90,12 +90,14 @@
<Compile Include="Code\DataAccess\TableDA.cs" />
<Compile Include="Code\DataTransfer\Column.cs" />
<Compile Include="Code\DataTransfer\Database.cs" />
<Compile Include="Code\DataTransfer\TableRow.cs" />
<Compile Include="Code\DataTransfer\Procedure.cs" />
<Compile Include="Code\DataTransfer\ProcedureDefinition.cs" />
<Compile Include="Code\DataTransfer\Server.cs" />
<Compile Include="Code\DataTransfer\Table.cs" />
<Compile Include="Code\DataTransfer\User.cs" />
<Compile Include="Code\DataTransfer\ViewDefinition.cs" />
<Compile Include="Code\TableRowHelper.cs" />
<Compile Include="Controls\CustomListView.cs">
<SubType>Component</SubType>
</Compile>