FrmBaseDatos: btnExportData Exports the data of the selected tables.

This commit is contained in:
2017-01-27 13:37:25 +01:00
parent b1b74fc450
commit 0e9d204a8a
3 changed files with 101 additions and 10 deletions

View File

@@ -108,7 +108,7 @@ namespace ServerExplorer.Code
} }
// Insertar fila a la datatable destino // Insertar fila a la datatable destino
listCmds.Add(String.Format("INSERT INTO {0} ({1}) VALUES ({2})", listCmds.Add(String.Format("INSERT INTO {0} ({1}) VALUES ({2});",
destTable, sbColumns.ToString(), sbValues.ToString())); destTable, sbColumns.ToString(), sbValues.ToString()));
} }

View File

@@ -48,6 +48,7 @@
this.colEsquema = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.colEsquema = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.colNombreTabla = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.colNombreTabla = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.colTipo = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.colTipo = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.btnExportData = new System.Windows.Forms.Button();
this.lsvColumnas = new ServerExplorer.Controls.CustomListView(); this.lsvColumnas = new ServerExplorer.Controls.CustomListView();
this.colNombreColumna = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.colNombreColumna = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.colTipoDatos = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.colTipoDatos = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
@@ -107,14 +108,14 @@
// menuCargar // menuCargar
// //
this.menuCargar.Name = "menuCargar"; this.menuCargar.Name = "menuCargar";
this.menuCargar.Size = new System.Drawing.Size(152, 22); this.menuCargar.Size = new System.Drawing.Size(116, 22);
this.menuCargar.Text = "Cargar"; this.menuCargar.Text = "Cargar";
this.menuCargar.Click += new System.EventHandler(this.menuCargar_Click); this.menuCargar.Click += new System.EventHandler(this.menuCargar_Click);
// //
// menuGuardar // menuGuardar
// //
this.menuGuardar.Name = "menuGuardar"; this.menuGuardar.Name = "menuGuardar";
this.menuGuardar.Size = new System.Drawing.Size(152, 22); this.menuGuardar.Size = new System.Drawing.Size(116, 22);
this.menuGuardar.Text = "Guardar"; this.menuGuardar.Text = "Guardar";
this.menuGuardar.Click += new System.EventHandler(this.menuGuardar_Click); this.menuGuardar.Click += new System.EventHandler(this.menuGuardar_Click);
// //
@@ -224,10 +225,10 @@
this.splitContainer1.Panel1.Controls.Add(this.btnExec); this.splitContainer1.Panel1.Controls.Add(this.btnExec);
this.splitContainer1.Panel1.Controls.Add(this.lsvTablas); this.splitContainer1.Panel1.Controls.Add(this.lsvTablas);
this.splitContainer1.Panel1.Controls.Add(this.btnProcs); this.splitContainer1.Panel1.Controls.Add(this.btnProcs);
this.splitContainer1.Panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.splitContainer1_Panel1_Paint);
// //
// splitContainer1.Panel2 // splitContainer1.Panel2
// //
this.splitContainer1.Panel2.Controls.Add(this.btnExportData);
this.splitContainer1.Panel2.Controls.Add(this.lblTituloTabla); this.splitContainer1.Panel2.Controls.Add(this.lblTituloTabla);
this.splitContainer1.Panel2.Controls.Add(this.btnDocGen); this.splitContainer1.Panel2.Controls.Add(this.btnDocGen);
this.splitContainer1.Panel2.Controls.Add(this.lsvColumnas); this.splitContainer1.Panel2.Controls.Add(this.lsvColumnas);
@@ -283,6 +284,16 @@
this.colTipo.Text = "Tipo"; this.colTipo.Text = "Tipo";
this.colTipo.Width = 71; this.colTipo.Width = 71;
// //
// btnExportData
//
this.btnExportData.Location = new System.Drawing.Point(84, 485);
this.btnExportData.Name = "btnExportData";
this.btnExportData.Size = new System.Drawing.Size(75, 23);
this.btnExportData.TabIndex = 15;
this.btnExportData.Text = "ExportData";
this.btnExportData.UseVisualStyleBackColor = true;
this.btnExportData.Click += new System.EventHandler(this.btnExportData_Click);
//
// lsvColumnas // lsvColumnas
// //
this.lsvColumnas.AllowSorting = false; this.lsvColumnas.AllowSorting = false;
@@ -345,8 +356,8 @@
this.Controls.Add(this.splitContainer1); this.Controls.Add(this.splitContainer1);
this.Controls.Add(this.btnCopiarConString); this.Controls.Add(this.btnCopiarConString);
this.Controls.Add(this.lblConString); this.Controls.Add(this.lblConString);
this.Controls.Add(this.menuBaseDatos);
this.Controls.Add(this.txtConString); this.Controls.Add(this.txtConString);
this.Controls.Add(this.menuBaseDatos);
this.MainMenuStrip = this.menuBaseDatos; this.MainMenuStrip = this.menuBaseDatos;
this.Name = "FrmBaseDatos"; this.Name = "FrmBaseDatos";
this.Text = "Base de Datos"; this.Text = "Base de Datos";
@@ -390,5 +401,6 @@
private System.Windows.Forms.SplitContainer splitContainer1; private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.ColumnHeader colNullable; private System.Windows.Forms.ColumnHeader colNullable;
private System.Windows.Forms.Button btnRefresh; private System.Windows.Forms.Button btnRefresh;
private System.Windows.Forms.Button btnExportData;
} }
} }

View File

@@ -7,6 +7,7 @@ using ServerExplorer.Code;
using System.Collections.Generic; using System.Collections.Generic;
using ServerExplorer.Code.DataTransfer; using ServerExplorer.Code.DataTransfer;
using ServerExplorer.Code.DataAccess; using ServerExplorer.Code.DataAccess;
using System.IO;
namespace ServerExplorer.UI namespace ServerExplorer.UI
{ {
@@ -271,14 +272,92 @@ namespace ServerExplorer.UI
Enabled = true; Enabled = true;
} }
private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
{
}
private void btnRefresh_Click(object sender, EventArgs e) private void btnRefresh_Click(object sender, EventArgs e)
{ {
Initialize(); Initialize();
} }
private void btnExportData_Click(object sender, EventArgs e)
{
// Hacer insensible la ventana
Parent.Enabled = false;
// Obtener informacion.
TablesFromListView();
DatabaseDesc db = CrearDatabaseDesc();
// Preparar cabecera del script
var listCmds = new List<string>();
listCmds.Add("SET NOCOUNT ON;");
listCmds.Add(string.Empty);
// Comandos de desactivacion de FKs
foreach (TablaDesc t in db.Tablas)
{
string tableName = string.Format("{0}.{1}", t.Esquema, t.Nombre);
listCmds.Add(string.Format("ALTER TABLE {0} NOCHECK CONSTRAINT all;", tableName));
}
listCmds.Add(string.Empty);
// Prepara informacion a exportar
foreach (TablaDesc t in db.Tablas)
{
string tableName = string.Format("{0}.{1}", t.Esquema, t.Nombre);
List<string> listCmdsTableData = ExportData(tableName);
listCmds.AddRange(listCmdsTableData);
}
// Comandos de activacion de FKs
foreach (TablaDesc t in db.Tablas)
{
string tableName = string.Format("{0}.{1}", t.Esquema, t.Nombre);
listCmds.Add(string.Format("ALTER TABLE {0} WITH CHECK CHECK CONSTRAINT all;", tableName));
}
listCmds.Add(string.Empty);
// Escribir script
string fixedDatabaseName = db.Nombre.Replace(' ', '_');
var escritor = new StreamWriter(fixedDatabaseName + ".data.sql");
foreach (string cmd in listCmds)
{
escritor.WriteLine(cmd);
}
escritor.Close();
// Hace sensible la ventana
Parent.Enabled = true;
}
private List<string> ExportData(string tableName)
{
DataTable dtData = Exec(string.Format("SELECT * FROM {0}", tableName));
var listCmds = new List<string>();
List<string> listCmdsInsert = Utiles.DataTable_GenerateInserts(dtData, tableName);
listCmds.Add(string.Format("-- {0}", tableName));
listCmds.Add(string.Format("DELETE FROM {0};", tableName));
listCmds.Add(string.Format("DBCC CHECKIDENT ('{0}', RESEED, 0);", tableName));
listCmds.Add(string.Format("SET IDENTITY_INSERT {0} ON;", tableName));
listCmds.AddRange(listCmdsInsert);
listCmds.Add(string.Format("SET IDENTITY_INSERT {0} OFF;", tableName));
listCmds.Add(string.Empty);
return listCmds;
}
private DataTable Exec(string cmd)
{
var cnx = new SqlConnection(_config.ConnectionString);
var da = new SqlDataAdapter(cmd, cnx);
var dt = new DataTable();
cnx.Open();
da.Fill(dt);
cnx.Close();
return dt;
}
} }
} }