FrmExec: Extract DataTable_GenerateInserts method to Utiles.
This commit is contained in:
@@ -1,10 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace ServerExplorer.Code
|
||||
{
|
||||
public static class Utiles
|
||||
{
|
||||
#region File Utils
|
||||
|
||||
public static void CrearDirectorio(this String path)
|
||||
{
|
||||
DirectoryInfo info;
|
||||
@@ -18,10 +24,97 @@ namespace ServerExplorer.Code
|
||||
}
|
||||
CrearDirectorio(info);
|
||||
}
|
||||
|
||||
public static void CrearDirectorio(this DirectoryInfo info)
|
||||
{
|
||||
if (info.Parent != null) CrearDirectorio(info.Parent);
|
||||
if (!info.Exists) info.Create();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region DataTable Utils
|
||||
|
||||
public static List<string> DataTable_GenerateInserts(DataTable dataTable, string destTable)
|
||||
{
|
||||
var nfi = new NumberFormatInfo { NumberDecimalSeparator = "." };
|
||||
|
||||
var listCmds = new List<string>();
|
||||
|
||||
// Recorrer la tabla de datos
|
||||
foreach (DataRow dr in dataTable.Rows)
|
||||
{
|
||||
var sbColumns = new StringBuilder();
|
||||
var sbValues = new StringBuilder();
|
||||
foreach (DataColumn dc in dataTable.Columns)
|
||||
{
|
||||
// El nombre de la columna
|
||||
if (sbColumns.Length > 0) { sbColumns.Append(", "); }
|
||||
sbColumns.AppendFormat("[{0}]", dc.ColumnName);
|
||||
|
||||
// El valor de la columna
|
||||
if (sbValues.Length > 0) { sbValues.Append(", "); }
|
||||
object valor = dr[dc];
|
||||
if (valor == DBNull.Value || valor == null)
|
||||
{
|
||||
// NULOS
|
||||
sbValues.Append("NULL");
|
||||
}
|
||||
else
|
||||
{
|
||||
string type = dc.DataType.Name.ToLower();
|
||||
if (type == "string")
|
||||
{
|
||||
// Cadenas
|
||||
sbValues.AppendFormat("'{0}'", ((string)valor).Replace("'", "''"));
|
||||
}
|
||||
else if (type == "int" || type == "int32" || type == "int64")
|
||||
{
|
||||
// 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 (imagenes, 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());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Insertar fila a la datatable destino
|
||||
listCmds.Add(String.Format("INSERT INTO {0} ({1}) VALUES ({2})",
|
||||
destTable, sbColumns.ToString(), sbValues.ToString()));
|
||||
}
|
||||
|
||||
return listCmds;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
59
ServerExplorer/UI/FrmExec.Designer.cs
generated
59
ServerExplorer/UI/FrmExec.Designer.cs
generated
@@ -29,10 +29,11 @@
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.btnExec = new System.Windows.Forms.Button();
|
||||
this.txtCommand = new ServerExplorer.Controls.CustomTextBox();
|
||||
this.dgvDatos = new System.Windows.Forms.DataGridView();
|
||||
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
|
||||
this.btnGenInserts = new System.Windows.Forms.Button();
|
||||
this.txtCommand = new ServerExplorer.Controls.CustomTextBox();
|
||||
this.txtDestTable = new System.Windows.Forms.TextBox();
|
||||
((System.ComponentModel.ISupportInitialize)(this.dgvDatos)).BeginInit();
|
||||
this.splitContainer1.Panel1.SuspendLayout();
|
||||
this.splitContainer1.Panel2.SuspendLayout();
|
||||
@@ -50,28 +51,14 @@
|
||||
this.btnExec.UseVisualStyleBackColor = true;
|
||||
this.btnExec.Click += new System.EventHandler(this.btnExec_Click);
|
||||
//
|
||||
// txtCommand
|
||||
//
|
||||
this.txtCommand.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
|
||||
| System.Windows.Forms.AnchorStyles.Left)
|
||||
| System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.txtCommand.Font = new System.Drawing.Font("Lucida Console", 8.25F);
|
||||
this.txtCommand.Location = new System.Drawing.Point(3, 3);
|
||||
this.txtCommand.Multiline = true;
|
||||
this.txtCommand.Name = "txtCommand";
|
||||
this.txtCommand.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
|
||||
this.txtCommand.Size = new System.Drawing.Size(604, 57);
|
||||
this.txtCommand.TabIndex = 1;
|
||||
this.txtCommand.TabWidth = 8;
|
||||
//
|
||||
// dgvDatos
|
||||
//
|
||||
this.dgvDatos.AllowUserToAddRows = false;
|
||||
this.dgvDatos.AllowUserToDeleteRows = false;
|
||||
this.dgvDatos.AllowUserToOrderColumns = true;
|
||||
this.dgvDatos.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
|
||||
| System.Windows.Forms.AnchorStyles.Left)
|
||||
| System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.dgvDatos.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
|
||||
| System.Windows.Forms.AnchorStyles.Left)
|
||||
| System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.dgvDatos.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
|
||||
this.dgvDatos.Location = new System.Drawing.Point(3, 3);
|
||||
this.dgvDatos.Name = "dgvDatos";
|
||||
@@ -82,9 +69,9 @@
|
||||
//
|
||||
// splitContainer1
|
||||
//
|
||||
this.splitContainer1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
|
||||
| System.Windows.Forms.AnchorStyles.Left)
|
||||
| System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.splitContainer1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
|
||||
| System.Windows.Forms.AnchorStyles.Left)
|
||||
| System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel1;
|
||||
this.splitContainer1.Location = new System.Drawing.Point(12, 12);
|
||||
this.splitContainer1.Name = "splitContainer1";
|
||||
@@ -92,6 +79,7 @@
|
||||
//
|
||||
// splitContainer1.Panel1
|
||||
//
|
||||
this.splitContainer1.Panel1.Controls.Add(this.txtDestTable);
|
||||
this.splitContainer1.Panel1.Controls.Add(this.btnGenInserts);
|
||||
this.splitContainer1.Panel1.Controls.Add(this.txtCommand);
|
||||
this.splitContainer1.Panel1.Controls.Add(this.btnExec);
|
||||
@@ -105,8 +93,8 @@
|
||||
//
|
||||
// btnGenInserts
|
||||
//
|
||||
this.btnGenInserts.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.btnGenInserts.Location = new System.Drawing.Point(424, 66);
|
||||
this.btnGenInserts.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
|
||||
this.btnGenInserts.Location = new System.Drawing.Point(3, 66);
|
||||
this.btnGenInserts.Name = "btnGenInserts";
|
||||
this.btnGenInserts.Size = new System.Drawing.Size(102, 23);
|
||||
this.btnGenInserts.TabIndex = 2;
|
||||
@@ -114,6 +102,28 @@
|
||||
this.btnGenInserts.UseVisualStyleBackColor = true;
|
||||
this.btnGenInserts.Click += new System.EventHandler(this.btnGenInserts_Click);
|
||||
//
|
||||
// txtCommand
|
||||
//
|
||||
this.txtCommand.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
|
||||
| System.Windows.Forms.AnchorStyles.Left)
|
||||
| System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.txtCommand.Font = new System.Drawing.Font("Lucida Console", 8.25F);
|
||||
this.txtCommand.Location = new System.Drawing.Point(3, 3);
|
||||
this.txtCommand.Multiline = true;
|
||||
this.txtCommand.Name = "txtCommand";
|
||||
this.txtCommand.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
|
||||
this.txtCommand.Size = new System.Drawing.Size(604, 57);
|
||||
this.txtCommand.TabIndex = 1;
|
||||
this.txtCommand.TabWidth = 8;
|
||||
//
|
||||
// txtDestTable
|
||||
//
|
||||
this.txtDestTable.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
|
||||
this.txtDestTable.Location = new System.Drawing.Point(111, 68);
|
||||
this.txtDestTable.Name = "txtDestTable";
|
||||
this.txtDestTable.Size = new System.Drawing.Size(126, 20);
|
||||
this.txtDestTable.TabIndex = 3;
|
||||
//
|
||||
// FrmExec
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
|
||||
@@ -121,7 +131,7 @@
|
||||
this.ClientSize = new System.Drawing.Size(634, 464);
|
||||
this.Controls.Add(this.splitContainer1);
|
||||
this.Name = "FrmExec";
|
||||
this.Text = "frmExec";
|
||||
this.Text = "Exec";
|
||||
((System.ComponentModel.ISupportInitialize)(this.dgvDatos)).EndInit();
|
||||
this.splitContainer1.Panel1.ResumeLayout(false);
|
||||
this.splitContainer1.Panel1.PerformLayout();
|
||||
@@ -138,5 +148,6 @@
|
||||
private System.Windows.Forms.DataGridView dgvDatos;
|
||||
private System.Windows.Forms.SplitContainer splitContainer1;
|
||||
private System.Windows.Forms.Button btnGenInserts;
|
||||
private System.Windows.Forms.TextBox txtDestTable;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Drawing;
|
||||
using System.Globalization;
|
||||
using System.Windows.Forms;
|
||||
using ServerExplorer.Code;
|
||||
|
||||
namespace ServerExplorer.UI
|
||||
{
|
||||
@@ -45,12 +46,19 @@ namespace ServerExplorer.UI
|
||||
{
|
||||
try
|
||||
{
|
||||
// Obtener el nombre de la tabla destino
|
||||
string destTable = Microsoft.VisualBasic.Interaction.InputBox("Nombre de la tabla destino", "tabla destino", String.Empty,
|
||||
Top + (Height / 2), Left + (Width / 2));
|
||||
DataTable dataTable = Exec();
|
||||
|
||||
DataTable dt = GenerarInserts(destTable);
|
||||
dgvDatos.DataSource = dt;
|
||||
List<string> listCmds = Utiles.DataTable_GenerateInserts(dataTable, txtDestTable.Text);
|
||||
|
||||
// Preparar la datatable destino
|
||||
var destDataTable = new DataTable();
|
||||
destDataTable.Columns.Add("Cmd", typeof(String));
|
||||
foreach (string cmd in listCmds)
|
||||
{
|
||||
destDataTable.Rows.Add(new object[] { cmd });
|
||||
}
|
||||
|
||||
dgvDatos.DataSource = destDataTable;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -88,81 +96,6 @@ namespace ServerExplorer.UI
|
||||
return dt;
|
||||
}
|
||||
|
||||
private DataTable GenerarInserts(string destTable)
|
||||
{
|
||||
var nfi = new NumberFormatInfo { NumberDecimalSeparator = "." };
|
||||
|
||||
DataTable dataTable = Exec();
|
||||
|
||||
// Preparar la datatable destino
|
||||
var destDataTable = new DataTable();
|
||||
destDataTable.Columns.Add("Comando", typeof(String));
|
||||
|
||||
// Recorrer la tabla de datos
|
||||
foreach (DataRow dr in dataTable.Rows)
|
||||
{
|
||||
string strColumns = String.Empty;
|
||||
string strValues = String.Empty;
|
||||
foreach (DataColumn dc in dataTable.Columns)
|
||||
{
|
||||
// El nombre de la columna
|
||||
if (strColumns != String.Empty)
|
||||
strColumns += ", ";
|
||||
strColumns += "[" + dc.ColumnName + "]";
|
||||
|
||||
// El valor de la columna
|
||||
if (strValues != String.Empty)
|
||||
strValues += ", ";
|
||||
object valor = dr[dc];
|
||||
if (valor == DBNull.Value || valor == null)
|
||||
{
|
||||
// NULOS
|
||||
strValues += "NULL";
|
||||
}
|
||||
else
|
||||
{
|
||||
string type = dc.DataType.Name.ToLower();
|
||||
if (type == "string")
|
||||
{
|
||||
// Cadenas
|
||||
strValues += "'" + ((string)valor).Replace("'", "''") + "'";
|
||||
}
|
||||
else if (type == "decimal")
|
||||
{
|
||||
// Decimales
|
||||
strValues += ((decimal)valor).ToString(nfi);
|
||||
}
|
||||
else if (type == "bit" || type == "bool" || type == "boolean")
|
||||
{
|
||||
// Booleanos
|
||||
strValues += ((bool)valor) ? "1" : "0";
|
||||
}
|
||||
else if (type == "byte[]" )
|
||||
{
|
||||
// Arrays de bytes (imagenes, archivos etc)
|
||||
strValues += "0x" + BitConverter.ToString(((byte[])valor)).Replace("-", "");
|
||||
}
|
||||
else
|
||||
{
|
||||
// Otros
|
||||
strValues += valor.ToString();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Insertar fila a la datatable destino
|
||||
destDataTable.Rows.Add(
|
||||
new object[] {
|
||||
String.Format("INSERT INTO {0} ({1}) VALUES ({2})",
|
||||
destTable, strColumns, strValues)
|
||||
});
|
||||
}
|
||||
|
||||
return destDataTable;
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user