FrmExec: Extract DataTable_GenerateInserts method to Utiles.
This commit is contained in:
@@ -1,10 +1,16 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
|
using System.Globalization;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace ServerExplorer.Code
|
namespace ServerExplorer.Code
|
||||||
{
|
{
|
||||||
public static class Utiles
|
public static class Utiles
|
||||||
{
|
{
|
||||||
|
#region File Utils
|
||||||
|
|
||||||
public static void CrearDirectorio(this String path)
|
public static void CrearDirectorio(this String path)
|
||||||
{
|
{
|
||||||
DirectoryInfo info;
|
DirectoryInfo info;
|
||||||
@@ -18,10 +24,97 @@ namespace ServerExplorer.Code
|
|||||||
}
|
}
|
||||||
CrearDirectorio(info);
|
CrearDirectorio(info);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void CrearDirectorio(this DirectoryInfo info)
|
public static void CrearDirectorio(this DirectoryInfo info)
|
||||||
{
|
{
|
||||||
if (info.Parent != null) CrearDirectorio(info.Parent);
|
if (info.Parent != null) CrearDirectorio(info.Parent);
|
||||||
if (!info.Exists) info.Create();
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
55
ServerExplorer/UI/FrmExec.Designer.cs
generated
55
ServerExplorer/UI/FrmExec.Designer.cs
generated
@@ -29,10 +29,11 @@
|
|||||||
private void InitializeComponent()
|
private void InitializeComponent()
|
||||||
{
|
{
|
||||||
this.btnExec = new System.Windows.Forms.Button();
|
this.btnExec = new System.Windows.Forms.Button();
|
||||||
this.txtCommand = new ServerExplorer.Controls.CustomTextBox();
|
|
||||||
this.dgvDatos = new System.Windows.Forms.DataGridView();
|
this.dgvDatos = new System.Windows.Forms.DataGridView();
|
||||||
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
|
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
|
||||||
this.btnGenInserts = new System.Windows.Forms.Button();
|
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();
|
((System.ComponentModel.ISupportInitialize)(this.dgvDatos)).BeginInit();
|
||||||
this.splitContainer1.Panel1.SuspendLayout();
|
this.splitContainer1.Panel1.SuspendLayout();
|
||||||
this.splitContainer1.Panel2.SuspendLayout();
|
this.splitContainer1.Panel2.SuspendLayout();
|
||||||
@@ -50,28 +51,14 @@
|
|||||||
this.btnExec.UseVisualStyleBackColor = true;
|
this.btnExec.UseVisualStyleBackColor = true;
|
||||||
this.btnExec.Click += new System.EventHandler(this.btnExec_Click);
|
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
|
// dgvDatos
|
||||||
//
|
//
|
||||||
this.dgvDatos.AllowUserToAddRows = false;
|
this.dgvDatos.AllowUserToAddRows = false;
|
||||||
this.dgvDatos.AllowUserToDeleteRows = false;
|
this.dgvDatos.AllowUserToDeleteRows = false;
|
||||||
this.dgvDatos.AllowUserToOrderColumns = true;
|
this.dgvDatos.AllowUserToOrderColumns = true;
|
||||||
this.dgvDatos.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
|
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.Left)
|
||||||
| System.Windows.Forms.AnchorStyles.Right)));
|
| System.Windows.Forms.AnchorStyles.Right)));
|
||||||
this.dgvDatos.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
|
this.dgvDatos.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
|
||||||
this.dgvDatos.Location = new System.Drawing.Point(3, 3);
|
this.dgvDatos.Location = new System.Drawing.Point(3, 3);
|
||||||
this.dgvDatos.Name = "dgvDatos";
|
this.dgvDatos.Name = "dgvDatos";
|
||||||
@@ -83,8 +70,8 @@
|
|||||||
// splitContainer1
|
// splitContainer1
|
||||||
//
|
//
|
||||||
this.splitContainer1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
|
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.Left)
|
||||||
| System.Windows.Forms.AnchorStyles.Right)));
|
| System.Windows.Forms.AnchorStyles.Right)));
|
||||||
this.splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel1;
|
this.splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel1;
|
||||||
this.splitContainer1.Location = new System.Drawing.Point(12, 12);
|
this.splitContainer1.Location = new System.Drawing.Point(12, 12);
|
||||||
this.splitContainer1.Name = "splitContainer1";
|
this.splitContainer1.Name = "splitContainer1";
|
||||||
@@ -92,6 +79,7 @@
|
|||||||
//
|
//
|
||||||
// splitContainer1.Panel1
|
// splitContainer1.Panel1
|
||||||
//
|
//
|
||||||
|
this.splitContainer1.Panel1.Controls.Add(this.txtDestTable);
|
||||||
this.splitContainer1.Panel1.Controls.Add(this.btnGenInserts);
|
this.splitContainer1.Panel1.Controls.Add(this.btnGenInserts);
|
||||||
this.splitContainer1.Panel1.Controls.Add(this.txtCommand);
|
this.splitContainer1.Panel1.Controls.Add(this.txtCommand);
|
||||||
this.splitContainer1.Panel1.Controls.Add(this.btnExec);
|
this.splitContainer1.Panel1.Controls.Add(this.btnExec);
|
||||||
@@ -105,8 +93,8 @@
|
|||||||
//
|
//
|
||||||
// btnGenInserts
|
// btnGenInserts
|
||||||
//
|
//
|
||||||
this.btnGenInserts.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
this.btnGenInserts.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
|
||||||
this.btnGenInserts.Location = new System.Drawing.Point(424, 66);
|
this.btnGenInserts.Location = new System.Drawing.Point(3, 66);
|
||||||
this.btnGenInserts.Name = "btnGenInserts";
|
this.btnGenInserts.Name = "btnGenInserts";
|
||||||
this.btnGenInserts.Size = new System.Drawing.Size(102, 23);
|
this.btnGenInserts.Size = new System.Drawing.Size(102, 23);
|
||||||
this.btnGenInserts.TabIndex = 2;
|
this.btnGenInserts.TabIndex = 2;
|
||||||
@@ -114,6 +102,28 @@
|
|||||||
this.btnGenInserts.UseVisualStyleBackColor = true;
|
this.btnGenInserts.UseVisualStyleBackColor = true;
|
||||||
this.btnGenInserts.Click += new System.EventHandler(this.btnGenInserts_Click);
|
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
|
// FrmExec
|
||||||
//
|
//
|
||||||
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
|
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
|
||||||
@@ -121,7 +131,7 @@
|
|||||||
this.ClientSize = new System.Drawing.Size(634, 464);
|
this.ClientSize = new System.Drawing.Size(634, 464);
|
||||||
this.Controls.Add(this.splitContainer1);
|
this.Controls.Add(this.splitContainer1);
|
||||||
this.Name = "FrmExec";
|
this.Name = "FrmExec";
|
||||||
this.Text = "frmExec";
|
this.Text = "Exec";
|
||||||
((System.ComponentModel.ISupportInitialize)(this.dgvDatos)).EndInit();
|
((System.ComponentModel.ISupportInitialize)(this.dgvDatos)).EndInit();
|
||||||
this.splitContainer1.Panel1.ResumeLayout(false);
|
this.splitContainer1.Panel1.ResumeLayout(false);
|
||||||
this.splitContainer1.Panel1.PerformLayout();
|
this.splitContainer1.Panel1.PerformLayout();
|
||||||
@@ -138,5 +148,6 @@
|
|||||||
private System.Windows.Forms.DataGridView dgvDatos;
|
private System.Windows.Forms.DataGridView dgvDatos;
|
||||||
private System.Windows.Forms.SplitContainer splitContainer1;
|
private System.Windows.Forms.SplitContainer splitContainer1;
|
||||||
private System.Windows.Forms.Button btnGenInserts;
|
private System.Windows.Forms.Button btnGenInserts;
|
||||||
|
private System.Windows.Forms.TextBox txtDestTable;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Data.SqlClient;
|
using System.Data.SqlClient;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Globalization;
|
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
|
using ServerExplorer.Code;
|
||||||
|
|
||||||
namespace ServerExplorer.UI
|
namespace ServerExplorer.UI
|
||||||
{
|
{
|
||||||
@@ -45,12 +46,19 @@ namespace ServerExplorer.UI
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Obtener el nombre de la tabla destino
|
DataTable dataTable = Exec();
|
||||||
string destTable = Microsoft.VisualBasic.Interaction.InputBox("Nombre de la tabla destino", "tabla destino", String.Empty,
|
|
||||||
Top + (Height / 2), Left + (Width / 2));
|
|
||||||
|
|
||||||
DataTable dt = GenerarInserts(destTable);
|
List<string> listCmds = Utiles.DataTable_GenerateInserts(dataTable, txtDestTable.Text);
|
||||||
dgvDatos.DataSource = dt;
|
|
||||||
|
// 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)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -88,81 +96,6 @@ namespace ServerExplorer.UI
|
|||||||
return dt;
|
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
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user