FrmExec: Limpiar código y añadir soporte para generar inserts con imágenes y booleanos

This commit is contained in:
2013-11-27 00:03:20 +01:00
parent 101838d653
commit d5ac17952b
2 changed files with 105 additions and 84 deletions

View File

@@ -52,9 +52,9 @@
// //
// txtCommand // txtCommand
// //
this.txtCommand.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 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.Left)
| System.Windows.Forms.AnchorStyles.Right))); | System.Windows.Forms.AnchorStyles.Right)));
this.txtCommand.Font = new System.Drawing.Font("Lucida Console", 8.25F); this.txtCommand.Font = new System.Drawing.Font("Lucida Console", 8.25F);
this.txtCommand.Location = new System.Drawing.Point(3, 3); this.txtCommand.Location = new System.Drawing.Point(3, 3);
this.txtCommand.Multiline = true; this.txtCommand.Multiline = true;
@@ -62,24 +62,29 @@
this.txtCommand.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.txtCommand.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtCommand.Size = new System.Drawing.Size(604, 57); this.txtCommand.Size = new System.Drawing.Size(604, 57);
this.txtCommand.TabIndex = 1; this.txtCommand.TabIndex = 1;
this.txtCommand.TabWidth = 8;
// //
// dgvDatos // dgvDatos
// //
this.dgvDatos.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) this.dgvDatos.AllowUserToAddRows = false;
| System.Windows.Forms.AnchorStyles.Left) this.dgvDatos.AllowUserToDeleteRows = false;
| System.Windows.Forms.AnchorStyles.Right))); 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.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";
this.dgvDatos.ReadOnly = true;
this.dgvDatos.Size = new System.Drawing.Size(604, 338); this.dgvDatos.Size = new System.Drawing.Size(604, 338);
this.dgvDatos.TabIndex = 2; this.dgvDatos.TabIndex = 2;
this.dgvDatos.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.dgvDatos_CellFormatting); this.dgvDatos.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.dgvDatos_CellFormatting);
// //
// 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";
@@ -117,7 +122,6 @@
this.Controls.Add(this.splitContainer1); this.Controls.Add(this.splitContainer1);
this.Name = "FrmExec"; this.Name = "FrmExec";
this.Text = "frmExec"; this.Text = "frmExec";
this.Load += new System.EventHandler(this.frmExec_Load);
((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();

View File

@@ -13,8 +13,13 @@ namespace ServerExplorer.UI
{ {
public partial class FrmExec : Form public partial class FrmExec : Form
{ {
#region Declarations
private string cnxString; private string cnxString;
#endregion
#region Form life cycle
public FrmExec(string cnxString) public FrmExec(string cnxString)
{ {
@@ -23,11 +28,54 @@ namespace ServerExplorer.UI
this.cnxString = cnxString; this.cnxString = cnxString;
} }
private void frmExec_Load(object sender, EventArgs e) #endregion
{
#region Events
private void btnExec_Click(object sender, EventArgs e)
{
try
{
DataTable dt = Exec();
dgvDatos.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
} }
private void btnGenInserts_Click(object sender, EventArgs e)
{
try
{
// Obtener el nombre de la tabla destino
string destTable = Microsoft.VisualBasic.Interaction.InputBox("Nombre de la tabla destino", "tabla destino", String.Empty,
this.Top + (this.Height / 2), this.Left + (this.Width / 2));
DataTable dt = GenerarInserts(destTable);
dgvDatos.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void dgvDatos_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.DesiredType == typeof(Image))
{
if (e.Value is DBNull || (e.Value is byte[] && ((byte[])e.Value).Length <= 0))
{
e.Value = new Bitmap(1, 1);
}
}
}
#endregion
#region Private methods
private DataTable Exec() private DataTable Exec()
{ {
@@ -48,57 +96,24 @@ namespace ServerExplorer.UI
return dt; return dt;
} }
private void btnExec_Click(object sender, EventArgs e) private DataTable GenerarInserts(string destTable)
{ {
try DataTable dataTable = new DataTable();
{ DataTable destDataTable;
// Ejecutar consulta NumberFormatInfo nfi = new NumberFormatInfo { NumberDecimalSeparator = "." };
DataTable dt = Exec();
// Mostrar resultados dataTable = Exec();
dgvDatos.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnGenInserts_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataTable ddt;
string destname = String.Empty;
NumberFormatInfo nfi = new NumberFormatInfo();
// Preparar un conversor de formato numerico que use puntos para los decimales
nfi.NumberDecimalSeparator = ".";
// Obtener el nombre de la tabla destino
destname = Microsoft.VisualBasic.Interaction.InputBox("Nombre de la tabla destino", "tabla destino", String.Empty,
this.Top + (this.Height / 2), this.Left + (this.Width / 2));
try
{
// Ejecutar consulta
dt = Exec();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
// Preparar la datatable destino // Preparar la datatable destino
ddt = new DataTable(); destDataTable = new DataTable();
ddt.Columns.Add("Comando", typeof(String)); destDataTable.Columns.Add("Comando", typeof(String));
// Recorrer la tabla de datos // Recorrer la tabla de datos
foreach (DataRow dr in dt.Rows) foreach (DataRow dr in dataTable.Rows)
{ {
string strColumns = String.Empty; string strColumns = String.Empty;
string strValues = String.Empty; string strValues = String.Empty;
foreach (DataColumn dc in dt.Columns) foreach (DataColumn dc in dataTable.Columns)
{ {
// El nombre de la columna // El nombre de la columna
if (strColumns != String.Empty) if (strColumns != String.Empty)
@@ -109,53 +124,55 @@ namespace ServerExplorer.UI
if (strValues != String.Empty) if (strValues != String.Empty)
strValues += ", "; strValues += ", ";
object valor = dr[dc]; object valor = dr[dc];
if (valor == DBNull.Value) if (valor == DBNull.Value || valor == null)
{ {
// NULOS // NULOS
strValues += "NULL"; strValues += "NULL";
} }
else else
if (dc.DataType.Name.ToLower() == "string") {
string type = dc.DataType.Name.ToLower();
if (type == "string")
{ {
// Cadenas // Cadenas
strValues += "'" + ((string)valor).Replace("'", "''") + "'"; 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 else
if (dc.DataType.Name.ToLower() == "decimal") {
{ // Otros
// Decimales strValues += valor.ToString();
strValues += ((decimal)valor).ToString(nfi); }
}
else }
{
// Otros
strValues += valor.ToString();
}
} }
// Insertar fila a la datatable destino // Insertar fila a la datatable destino
ddt.Rows.Add(new object[] { destDataTable.Rows.Add(
"INSERT INTO " + destname + new object[] {
" (" + strColumns + ") "+ String.Format("INSERT INTO {0} ({1}) VALUES ({2})",
"VALUES (" + strValues + ")" }); destTable, strColumns, strValues)
});
} }
// Establecer el resultado return destDataTable;
dgvDatos.DataSource = ddt;
}
private void dgvDatos_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.DesiredType == typeof(Image))
{
if (e.Value is DBNull || (e.Value is byte[] && ((byte[])e.Value).Length <= 0))
{
e.Value = new Bitmap(1, 1);
}
}
} }
#endregion
} }
} }