343 lines
10 KiB
C#
343 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
using System.Data.Sql;
|
|
using System.Data.SqlClient;
|
|
using System.Xml.Serialization;
|
|
using System.Threading;
|
|
|
|
namespace ServerExplorer
|
|
{
|
|
public partial class FrmBaseDatos : Form
|
|
{
|
|
private FrmBaseDatos instancia;
|
|
private SqlConnection cnx;
|
|
private Config config;
|
|
private String table_schema = null;
|
|
private String table_name = null;
|
|
|
|
private void inicializar()
|
|
{
|
|
// Establecer conexion
|
|
txtConString.Text = config.ConString;
|
|
cnx = new SqlConnection(config.ConString);
|
|
|
|
// Obtener lista de tablas
|
|
cnx.Open();
|
|
DataTable dt = cnx.GetSchema("Tables");
|
|
cnx.Close();
|
|
|
|
// Mostrar todas las tablas
|
|
lsvTablas.Items.Clear();
|
|
foreach (DataRow dr in dt.Rows)
|
|
{
|
|
ListViewItem item = lsvTablas.Items.Add((String)dr["TABLE_NAME"]);
|
|
item.SubItems.Add((String)dr["TABLE_SCHEMA"]);
|
|
item.SubItems.Add((String)dr["TABLE_TYPE"]);
|
|
}
|
|
tablas_alistview();
|
|
|
|
// Limpiar Columnas
|
|
lsvColumnas.Items.Clear();
|
|
}
|
|
|
|
// Metodo para copiar las tablas seleccionadas
|
|
// en el listview a la configuracion
|
|
private void tablas_delistview()
|
|
{
|
|
config.Tablas.Clear();
|
|
foreach (ListViewItem item in lsvTablas.Items)
|
|
{
|
|
if (item.Checked)
|
|
{
|
|
config.Tablas.Add(new TablaInfo(
|
|
item.SubItems[1].Text, // Esquema
|
|
item.SubItems[0].Text // Nombre tabla
|
|
));
|
|
}
|
|
}
|
|
}
|
|
|
|
// Metodo para seleccionar las tablas
|
|
// en le listview desde la configuracion
|
|
private void tablas_alistview()
|
|
{
|
|
int i, j, n;
|
|
|
|
// 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
|
|
n = config.Tablas.Count;
|
|
i = j = 0;
|
|
foreach (ListViewItem item in lsvTablas.Items)
|
|
{
|
|
item.Checked = false;
|
|
String tablename =
|
|
item.SubItems[1].Text + // Esquema
|
|
item.SubItems[0].Text; // Nombre tabla
|
|
do
|
|
{
|
|
if (config.Tablas[i].Esquema.
|
|
CompareTo(item.SubItems[1].Text) == 0 &&
|
|
config.Tablas[i].Nombre.
|
|
CompareTo(item.SubItems[0].Text) == 0)
|
|
{
|
|
item.Checked = true;
|
|
break;
|
|
}
|
|
i = (i + 1) % n;
|
|
} while (i != j);
|
|
j = i;
|
|
}
|
|
}
|
|
|
|
|
|
public DatabaseDesc CrearDatabaseDesc()
|
|
{
|
|
DatabaseDesc db = new DatabaseDesc(cnx.Database);
|
|
foreach (TablaInfo t in config.Tablas)
|
|
{
|
|
TablaDesc td = new TablaDesc();
|
|
td.FillDesc(t.Esquema, t.Nombre, cnx);
|
|
db.Tablas.Add(td);
|
|
}
|
|
return (db);
|
|
}
|
|
|
|
public FrmBaseDatos(String ConString)
|
|
{
|
|
InitializeComponent();
|
|
|
|
instancia = this;
|
|
config = new Config();
|
|
config.ConString = ConString;
|
|
}
|
|
|
|
public FrmBaseDatos(Config config)
|
|
{
|
|
InitializeComponent();
|
|
|
|
instancia = this;
|
|
this.config = config;
|
|
}
|
|
|
|
|
|
|
|
private void frmBaseDatos_Load(object sender, EventArgs e)
|
|
{
|
|
inicializar();
|
|
|
|
// Establecer titulos
|
|
lblTituloDB.Text = cnx.Database;
|
|
lblTituloTabla.Text = "";
|
|
}
|
|
|
|
private void btnCopiarConString_Click(object sender, EventArgs e)
|
|
{
|
|
// Copiar la cadena de conexion al portapapeles
|
|
System.Windows.Forms.Clipboard.SetText(txtConString.Text);
|
|
}
|
|
|
|
private void lsvTablas_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
if (lsvTablas.SelectedItems.Count == 1)
|
|
{
|
|
// Determinar tabla seleccionada
|
|
ListViewItem item = lsvTablas.SelectedItems[0];
|
|
|
|
// Recordar tabla seleccionada
|
|
table_schema = item.SubItems[1].Text.ToString();
|
|
table_name = item.SubItems[0].Text.ToString();
|
|
|
|
// Establecer titulo de la lista de columnas
|
|
lblTituloTabla.Text = table_schema + "." + table_name;
|
|
|
|
// Obtener descripcion de tabla
|
|
TablaDesc td = new TablaDesc();
|
|
td.FillDesc(item.SubItems[1].Text, item.SubItems[0].Text, 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);
|
|
if (col.Tamanho >= 0)
|
|
{
|
|
subitem.SubItems.Add(String.Format("{0}", col.Tamanho));
|
|
}
|
|
else
|
|
{
|
|
subitem.SubItems.Add("");
|
|
}
|
|
if (col.Primaria)
|
|
{
|
|
subitem.SubItems.Add("PK");
|
|
}
|
|
else
|
|
{
|
|
subitem.SubItems.Add("");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void menuCargar_Click(object sender, EventArgs e)
|
|
{
|
|
OpenFileDialog dialogo = new OpenFileDialog();
|
|
if (dialogo.ShowDialog() == DialogResult.OK)
|
|
{
|
|
config = Config.Cargar(dialogo.FileName);
|
|
inicializar();
|
|
}
|
|
}
|
|
|
|
private void menuGuardar_Click(object sender, EventArgs e)
|
|
{
|
|
SaveFileDialog dialogo = new SaveFileDialog();
|
|
if (dialogo.ShowDialog() == DialogResult.OK)
|
|
{
|
|
tablas_delistview();
|
|
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)
|
|
{
|
|
String nombreTabla;
|
|
DataTable dt;
|
|
SqlDataAdapter da;
|
|
|
|
if (table_schema == null || table_name == null) { return; }
|
|
|
|
nombreTabla = "[" + table_schema + "].[" + table_name + "]";
|
|
|
|
// Obtener un datatable con todos los registros de la tabla.
|
|
da = new SqlDataAdapter(
|
|
"select * from " + nombreTabla,
|
|
cnx);
|
|
dt = new DataTable();
|
|
cnx.Open();
|
|
da.Fill(dt);
|
|
cnx.Close();
|
|
|
|
// Crear y mostrar el formulario de los datos.
|
|
FrmDatos form = new FrmDatos(dt, nombreTabla);
|
|
form.MdiParent = this.MdiParent;
|
|
form.Show();
|
|
|
|
}
|
|
|
|
|
|
private void btnProcs_Click(object sender, EventArgs e)
|
|
{
|
|
/*DataTable dt;
|
|
SqlDataAdapter da;
|
|
|
|
// Obtener un datatable con todos los registros de la tabla.
|
|
da = new SqlDataAdapter(
|
|
"SELECT id,name,crdate FROM sysobjects WHERE type = 'P' AND category = 0 ORDER BY name ",
|
|
cnx);
|
|
dt = new DataTable();
|
|
cnx.Open();
|
|
da.Fill(dt);
|
|
cnx.Close();
|
|
|
|
// Crear y mostrar el formulario de los datos.
|
|
frmDatos form = new frmDatos(dt, "Procedimientos");
|
|
form.MdiParent = this.MdiParent;
|
|
form.Show();*/
|
|
|
|
// Crear y mostrar el formulario de los procedimientos.
|
|
FrmProcedimientos form = new FrmProcedimientos(config.ConString);
|
|
form.MdiParent = this.MdiParent;
|
|
form.Show();
|
|
}
|
|
|
|
private void btnExec_Click(object sender, EventArgs e)
|
|
{
|
|
// Crear y mostrar el formulario de exec.
|
|
FrmExec form = new FrmExec(config.ConString);
|
|
form.MdiParent = this.MdiParent;
|
|
form.Show();
|
|
}
|
|
|
|
|
|
private void menuBaseDatos_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void btnGenerar_Click(object sender, EventArgs e)
|
|
{
|
|
tablas_delistview();
|
|
DatabaseDesc db = CrearDatabaseDesc();
|
|
|
|
//XmlSerializer seriador = new XmlSerializer(typeof(DatabaseDesc));
|
|
//seriador.Serialize(System.Console.Out, db);
|
|
|
|
CodeGen.GenerarCodigo(config, db);
|
|
}
|
|
|
|
private void btnDocGen_Click(object sender, EventArgs e)
|
|
{
|
|
// Hacer insensible la ventana
|
|
this.Parent.Enabled = false;
|
|
|
|
// Obtener informacion.
|
|
tablas_delistview();
|
|
DatabaseDesc db = CrearDatabaseDesc();
|
|
|
|
// Escribir documentacion
|
|
DocGen.GenerarDocumentacion(config, db);
|
|
|
|
// Hace sensible la ventana
|
|
this.Parent.Enabled = true;
|
|
|
|
/*
|
|
tablas_delistview();
|
|
Thread hilo=new Thread(new ThreadStart(instancia.GenerarDoc));
|
|
hilo.Start();
|
|
*/
|
|
}
|
|
|
|
public void GenerarDoc()
|
|
{
|
|
// Hacer insensible la ventana
|
|
this.Enabled = false;
|
|
|
|
// Obtener informacion.
|
|
DatabaseDesc db = CrearDatabaseDesc();
|
|
|
|
// Escribir documentacion
|
|
DocGen.GenerarDocumentacion(config, db);
|
|
|
|
// Hace sensible la ventana
|
|
this.Enabled = true;
|
|
}
|
|
|
|
private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|