364 lines
12 KiB
C#
364 lines
12 KiB
C#
using System;
|
|
using System.Data;
|
|
using System.Windows.Forms;
|
|
using System.Data.SqlClient;
|
|
using System.Xml.Serialization;
|
|
using ServerExplorer.Code;
|
|
using System.Collections.Generic;
|
|
using ServerExplorer.Code.DataTransfer;
|
|
using ServerExplorer.Code.DataAccess;
|
|
using System.IO;
|
|
|
|
namespace ServerExplorer.UI
|
|
{
|
|
public partial class FrmBaseDatos : Form
|
|
{
|
|
private SqlConnection _cnx;
|
|
private Config _config;
|
|
private String _tableSchema;
|
|
private String _tableName;
|
|
|
|
private void Initialize()
|
|
{
|
|
// Establecer conexion
|
|
txtConString.Text = _config.ConnectionString;
|
|
_cnx = new SqlConnection(_config.ConnectionString);
|
|
|
|
List<Table> tables = TableDA.Table_GetRegs(_config.ConnectionString);
|
|
lsvTablas.Items.Clear();
|
|
foreach (Table table in tables)
|
|
{
|
|
ListViewItem item = lsvTablas.Items.Add(table.Schema);
|
|
item.SubItems.Add(table.Name);
|
|
item.SubItems.Add(table.Type);
|
|
}
|
|
|
|
TablesToListView();
|
|
lsvColumnas.Items.Clear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Metodo para copiar las tablas seleccionadas en el listview a la configuracion
|
|
/// </summary>
|
|
private void TablesFromListView()
|
|
{
|
|
_config.Tablas.Clear();
|
|
foreach (ListViewItem item in lsvTablas.Items)
|
|
{
|
|
if (item.Checked)
|
|
{
|
|
_config.Tablas.Add(new TablaInfo
|
|
{
|
|
Esquema = item.SubItems[0].Text,
|
|
Nombre = item.SubItems[1].Text
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Metodo para seleccionar las tablas en le listview desde la configuracion
|
|
/// </summary>
|
|
private void TablesToListView()
|
|
{
|
|
int j;
|
|
|
|
// Desmarcar todos en caso de no haber ninguna tabla
|
|
if (_config.Tablas.Count == 0)
|
|
{
|
|
foreach (ListViewItem item in lsvTablas.Items)
|
|
item.Checked = false;
|
|
return;
|
|
}
|
|
|
|
// Recorrer items marcando los que estan en la configuracion
|
|
int n = _config.Tablas.Count;
|
|
int i = j = 0;
|
|
foreach (ListViewItem item in lsvTablas.Items)
|
|
{
|
|
item.Checked = false;
|
|
do
|
|
{
|
|
if (String.Compare(_config.Tablas[i].Esquema, item.SubItems[0].Text, StringComparison.Ordinal) == 0 &&
|
|
String.Compare(_config.Tablas[i].Nombre, item.SubItems[1].Text, StringComparison.Ordinal) == 0)
|
|
{
|
|
item.Checked = true;
|
|
break;
|
|
}
|
|
i = (i + 1) % n;
|
|
} while (i != j);
|
|
j = i;
|
|
}
|
|
}
|
|
|
|
|
|
public DatabaseDesc CrearDatabaseDesc()
|
|
{
|
|
var db = new DatabaseDesc { Nombre = _cnx.Database };
|
|
foreach (TablaInfo t in _config.Tablas)
|
|
{
|
|
var td = new TablaDesc();
|
|
td.FillDesc(t.Esquema, t.Nombre, _cnx);
|
|
db.Tablas.Add(td);
|
|
}
|
|
return (db);
|
|
}
|
|
|
|
public FrmBaseDatos(String connectionString)
|
|
{
|
|
InitializeComponent();
|
|
|
|
_config = new Config {ConnectionString = connectionString};
|
|
}
|
|
|
|
public FrmBaseDatos(Config config)
|
|
{
|
|
InitializeComponent();
|
|
|
|
_config = config;
|
|
}
|
|
|
|
private void frmBaseDatos_Load(object sender, EventArgs e)
|
|
{
|
|
Initialize();
|
|
|
|
// Establecer titulos
|
|
lblTituloDB.Text = _cnx.Database;
|
|
lblTituloTabla.Text = "";
|
|
}
|
|
|
|
private void btnCopiarConString_Click(object sender, EventArgs e)
|
|
{
|
|
// Copiar la cadena de conexion al portapapeles
|
|
Clipboard.SetText(txtConString.Text);
|
|
}
|
|
|
|
private void lsvTablas_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
if (lsvTablas.SelectedItems.Count != 1) { return; }
|
|
|
|
// Determinar tabla seleccionada
|
|
ListViewItem item = lsvTablas.SelectedItems[0];
|
|
|
|
// Recordar tabla seleccionada
|
|
_tableSchema = item.SubItems[0].Text;
|
|
_tableName = item.SubItems[1].Text;
|
|
|
|
// Establecer titulo de la lista de columnas
|
|
lblTituloTabla.Text = _tableSchema + @"." + _tableName;
|
|
|
|
// Obtener descripcion de tabla
|
|
var td = new TablaDesc();
|
|
td.FillDesc(_tableSchema, _tableName, _cnx);
|
|
|
|
// Mostrar "columnas" de las tablas
|
|
lsvColumnas.Items.Clear();
|
|
foreach (ColumnaDesc col in td.Columnas)
|
|
{
|
|
ListViewItem subitem = lsvColumnas.Items.Add(col.Nombre);
|
|
subitem.SubItems.Add(col.Tipo);
|
|
subitem.SubItems.Add((col.Tamanho >= 0) ? String.Format("{0}", col.Tamanho) : string.Empty);
|
|
subitem.SubItems.Add(col.Primaria ? "PK" : string.Empty);
|
|
subitem.SubItems.Add(col.Nullable ? "Null" : "NotNull");
|
|
}
|
|
}
|
|
|
|
private void menuCargar_Click(object sender, EventArgs e)
|
|
{
|
|
var dialogo = new OpenFileDialog();
|
|
if (dialogo.ShowDialog() == DialogResult.OK)
|
|
{
|
|
_config = Config.Cargar(dialogo.FileName);
|
|
Initialize();
|
|
}
|
|
}
|
|
|
|
private void menuGuardar_Click(object sender, EventArgs e)
|
|
{
|
|
var dialogo = new SaveFileDialog();
|
|
if (dialogo.ShowDialog() == DialogResult.OK)
|
|
{
|
|
TablesFromListView();
|
|
_config.Guardar(dialogo.FileName);
|
|
}
|
|
}
|
|
|
|
private void menuConfiguracion_Click(object sender, EventArgs e)
|
|
{
|
|
//// Llamar a la ventana de configuracion
|
|
//FrmCodeGenConfig frm = new FrmCodeGenConfig(config);
|
|
//frm.ShowDialog();
|
|
}
|
|
|
|
private void btnVerDatos_Click(object sender, EventArgs e)
|
|
{
|
|
if (string.IsNullOrEmpty(_tableSchema) || string.IsNullOrEmpty(_tableName)) { return; }
|
|
|
|
// Obtener descripcion de tabla
|
|
var td = new TablaDesc();
|
|
td.FillDesc(_tableSchema, _tableName, _cnx);
|
|
|
|
// Crear y mostrar el formulario de los datos.
|
|
var form = new FrmDatos(td, _config.ConnectionString);
|
|
FrmPrincipal.AddForm(form);
|
|
}
|
|
|
|
|
|
private void btnProcs_Click(object sender, EventArgs e)
|
|
{
|
|
// Crear y mostrar el formulario de los procedimientos.
|
|
var form = new FrmProcedimientos(_config.ConnectionString);
|
|
FrmPrincipal.AddForm(form);
|
|
}
|
|
|
|
private void btnExec_Click(object sender, EventArgs e)
|
|
{
|
|
// Crear y mostrar el formulario de exec.
|
|
var form = new FrmExec(_config.ConnectionString);
|
|
FrmPrincipal.AddForm(form);
|
|
}
|
|
|
|
|
|
private void menuBaseDatos_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void btnGenerar_Click(object sender, EventArgs e)
|
|
{
|
|
TablesFromListView();
|
|
DatabaseDesc db = CrearDatabaseDesc();
|
|
|
|
var seriador = new XmlSerializer(typeof(DatabaseDesc));
|
|
seriador.Serialize(Console.Out, db);
|
|
|
|
//CodeGen.GenerarCodigo(config, db);
|
|
}
|
|
|
|
private void btnDocGen_Click(object sender, EventArgs e)
|
|
{
|
|
// Hacer insensible la ventana
|
|
Parent.Enabled = false;
|
|
|
|
// Obtener informacion.
|
|
TablesFromListView();
|
|
DatabaseDesc db = CrearDatabaseDesc();
|
|
|
|
// Escribir documentacion
|
|
DocGen.GenerarDocumentacion(db);
|
|
|
|
// Hace sensible la ventana
|
|
Parent.Enabled = true;
|
|
|
|
/*
|
|
tablas_delistview();
|
|
Thread hilo=new Thread(new ThreadStart(instancia.GenerarDoc));
|
|
hilo.Start();
|
|
*/
|
|
}
|
|
|
|
public void GenerarDoc()
|
|
{
|
|
// Hacer insensible la ventana
|
|
Enabled = false;
|
|
|
|
// Obtener informacion.
|
|
DatabaseDesc db = CrearDatabaseDesc();
|
|
|
|
// Escribir documentacion
|
|
DocGen.GenerarDocumentacion(db);
|
|
|
|
// Hace sensible la ventana
|
|
Enabled = true;
|
|
}
|
|
|
|
private void btnRefresh_Click(object sender, EventArgs e)
|
|
{
|
|
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;
|
|
}
|
|
|
|
}
|
|
}
|