Corregir clases para serializado XML correcto

This commit is contained in:
2013-11-21 23:49:03 +01:00
parent 9665cce497
commit 77f7787e23
4 changed files with 187 additions and 150 deletions

View File

@@ -5,68 +5,26 @@ using System.Xml.Serialization;
namespace ServerExplorer namespace ServerExplorer
{ {
[Serializable]
public class Config public class Config
{ {
public String ConString = ""; #region Data parameters
public String pathRaiz = ".";
public String PathRaiz
{
get { return pathRaiz; }
set { pathRaiz = value; }
}
public String pathDAL = ".";
public String PathDAL
{
get
{
if (pathDAL == ".")
{
return pathRaiz;
}
else
{
return pathDAL;
}
}
set { pathDAL = value; }
}
public String pathDTO = ".";
public String PathDTO
{
get
{
if (pathDTO == ".")
{
return pathRaiz;
}
else
{
return pathDTO;
}
}
set { pathDTO = value; }
}
public String pathExtra = ".";
public String PathExtra
{
get
{
if (pathExtra == ".")
{
return pathRaiz;
}
else
{
return pathExtra;
}
}
set { pathExtra = value; }
}
public String NamespaceDAL = "DAL";
public String NamespaceDTO = "DTO";
public List<TablaInfo> Tablas = new List<TablaInfo>();
public Config() { } private string conString = "";
[XmlAttribute("ConnectionString")]
public string ConnectionString
{
get { return conString; }
set { conString = value; }
}
private List<TablaInfo> tablas = new List<TablaInfo>();
[XmlArray("Tablas")]
public List<TablaInfo> Tablas { get { return tablas; } }
#endregion
#region Persistence methods
public void Guardar(String fichero) public void Guardar(String fichero)
{ {
@@ -85,18 +43,26 @@ namespace ServerExplorer
return config; return config;
} }
#endregion
} }
[Serializable]
public class TablaInfo public class TablaInfo
{ {
public String Esquema = ""; private string esquema = string.Empty;
public String Nombre = ""; [XmlAttribute("Esquema")]
public string Esquema
public TablaInfo() { }
public TablaInfo(String esquema, String nombre)
{ {
Esquema = esquema; get { return esquema; }
Nombre = nombre; set { esquema = value; }
}
private string nombre = string.Empty;
[XmlAttribute("Nombre")]
public string Nombre
{
get { return nombre; }
set { nombre = value; }
} }
} }
} }

View File

@@ -2,19 +2,23 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Xml.Serialization;
namespace ServerExplorer namespace ServerExplorer
{ {
[Serializable]
public class DatabaseDesc public class DatabaseDesc
{ {
public string Nombre = ""; private string nombre = string.Empty;
public List<TablaDesc> Tablas = new List<TablaDesc>(); [XmlAttribute("Nombre")]
public string Nombre
public DatabaseDesc() { }
public DatabaseDesc(string nombre)
{ {
Nombre = nombre; get { return nombre; }
set { nombre = value; }
} }
private List<TablaDesc> tablas = new List<TablaDesc>();
[XmlArray("Tablas")]
public List<TablaDesc> Tablas { get { return tablas; } }
} }
} }

View File

@@ -5,16 +5,38 @@ using System.Text;
using System.Data; using System.Data;
using System.Data.Sql; using System.Data.Sql;
using System.Data.SqlClient; using System.Data.SqlClient;
using System.Xml.Serialization;
namespace ServerExplorer namespace ServerExplorer
{ {
[Serializable]
public class TablaDesc public class TablaDesc
{ {
public String Esquema = String.Empty; #region Data parameters
public String Nombre = String.Empty;
public List<ColumnaDesc> Columnas = new List<ColumnaDesc>();
public TablaDesc() { } private String esquema = String.Empty;
[XmlAttribute("Esquema")]
public string Esquema
{
get { return esquema; }
set { esquema = value; }
}
private String nombre = String.Empty;
[XmlAttribute("Nombre")]
public string Nombre
{
get { return nombre; }
set { nombre = value; }
}
private List<ColumnaDesc> columnas = new List<ColumnaDesc>();
[XmlArray("Columnas")]
public List<ColumnaDesc> Columnas { get { return columnas; } }
#endregion
#region FillDesc
// Obtener una columna existente // Obtener una columna existente
private ColumnaDesc GetCol(String nombre) private ColumnaDesc GetCol(String nombre)
@@ -85,7 +107,7 @@ namespace ServerExplorer
// Establecer datos de la columna // Establecer datos de la columna
col.Nombre = (String)dr["Columna"]; col.Nombre = (String)dr["Columna"];
col.Tipo = (String)dr["Tipo"]; col.Tipo = ((String)dr["Tipo"]).ToLower();
if (dr["Tamanho"] != DBNull.Value) if (dr["Tamanho"] != DBNull.Value)
{ {
col.Tamanho = (int)dr["Tamanho"]; col.Tamanho = (int)dr["Tamanho"];
@@ -99,8 +121,12 @@ namespace ServerExplorer
} }
} }
} }
#endregion
} }
#region TipoCol
public enum TipoCol public enum TipoCol
{ {
Unset, Unset,
@@ -113,92 +139,130 @@ namespace ServerExplorer
Otro Otro
} }
#endregion
[Serializable]
public class ColumnaDesc public class ColumnaDesc
{ {
public string Nombre = String.Empty; #region Data properties
public string Tipo = String.Empty;
public int Tamanho = -1;
public bool Primaria = false;
public ColumnaDesc() { } private string nombre = String.Empty;
[XmlAttribute("Nombre")]
public string Nombre
{
get { return nombre; }
set { nombre = value; }
}
private TipoCol tipo = TipoCol.Unset; private string tipo = String.Empty;
[XmlAttribute("Tipo")]
public string Tipo
{
get { return tipo; }
set { tipo = value; }
}
private int tamanho = -1;
[XmlAttribute("Tamanho")]
public int Tamanho
{
get { return tamanho; }
set { tamanho = value; }
}
private bool primaria = false;
[XmlAttribute("Primaria")]
public bool Primaria
{
get { return primaria; }
set { primaria = value; }
}
#endregion
#region GetTipo
private TipoCol _tipo = TipoCol.Unset;
public TipoCol GetTipo() public TipoCol GetTipo()
{ {
string stipo = Tipo.ToLower(); string strTipo = Tipo.ToLower();
if (this.tipo != TipoCol.Unset) if (_tipo != TipoCol.Unset)
{ {
return this.tipo; return _tipo;
} }
// Numericos // Numericos
if ( if (
stipo == "bigint" || strTipo == "bigint" ||
stipo == "int" || strTipo == "int" ||
stipo == "smallint" || strTipo == "smallint" ||
stipo == "tinyint" || strTipo == "tinyint" ||
stipo == "bigint" strTipo == "bigint"
) )
{ {
return TipoCol.Numerico; _tipo = TipoCol.Numerico;
} }
// Aproximados numericos // Aproximados numericos
if ( if (
stipo == "float" || strTipo == "float" ||
stipo == "real" strTipo == "real"
) )
{ {
return TipoCol.AproxNumerico; _tipo = TipoCol.AproxNumerico;
} }
// Tiempo // Tiempo
if ( if (
stipo == "date" || strTipo == "date" ||
stipo == "datetimeoffset" || strTipo == "datetimeoffset" ||
stipo == "datetime2" || strTipo == "datetime2" ||
stipo == "smalldatetime" || strTipo == "smalldatetime" ||
stipo == "datetime" || strTipo == "datetime" ||
stipo == "time" strTipo == "time"
) )
{ {
return TipoCol.Tiempo; _tipo = TipoCol.Tiempo;
} }
// Texto // Texto
if ( if (
stipo == "char" || strTipo == "char" ||
stipo == "varchar" || strTipo == "varchar" ||
stipo == "text" || strTipo == "text" ||
stipo == "nchar" || strTipo == "nchar" ||
stipo == "nvarchar" || strTipo == "nvarchar" ||
stipo == "ntext" strTipo == "ntext"
) )
{ {
return TipoCol.Texto; _tipo = TipoCol.Texto;
} }
// Binario // Binario
if ( if (
stipo == "binary" || strTipo == "binary" ||
stipo == "varbinary" || strTipo == "varbinary" ||
stipo == "image" strTipo == "image"
) )
{ {
return TipoCol.Binario; _tipo = TipoCol.Binario;
} }
// Booleano // Booleano
if ( if (
stipo == "bit" strTipo == "bit"
) )
{ {
return TipoCol.Booleano; _tipo = TipoCol.Booleano;
} }
// Otro // Otro
return TipoCol.Otro; _tipo = TipoCol.Otro;
return _tipo;
} }
#endregion
} }
} }

View File

@@ -18,14 +18,14 @@ namespace ServerExplorer
private FrmBaseDatos instancia; private FrmBaseDatos instancia;
private SqlConnection cnx; private SqlConnection cnx;
private Config config; private Config config;
private String table_schema = null; private String tableSchema = null;
private String table_name = null; private String tableName = null;
private void inicializar() private void Initialize()
{ {
// Establecer conexion // Establecer conexion
txtConString.Text = config.ConString; txtConString.Text = config.ConnectionString;
cnx = new SqlConnection(config.ConString); cnx = new SqlConnection(config.ConnectionString);
// Obtener lista de tablas // Obtener lista de tablas
cnx.Open(); cnx.Open();
@@ -40,32 +40,35 @@ namespace ServerExplorer
item.SubItems.Add((String)dr["TABLE_SCHEMA"]); item.SubItems.Add((String)dr["TABLE_SCHEMA"]);
item.SubItems.Add((String)dr["TABLE_TYPE"]); item.SubItems.Add((String)dr["TABLE_TYPE"]);
} }
tablas_alistview(); TablesToListView();
// Limpiar Columnas // Limpiar Columnas
lsvColumnas.Items.Clear(); lsvColumnas.Items.Clear();
} }
// Metodo para copiar las tablas seleccionadas /// <summary>
// en el listview a la configuracion /// Metodo para copiar las tablas seleccionadas en el listview a la configuracion
private void tablas_delistview() /// </summary>
private void TablesFromListView()
{ {
config.Tablas.Clear(); config.Tablas.Clear();
foreach (ListViewItem item in lsvTablas.Items) foreach (ListViewItem item in lsvTablas.Items)
{ {
if (item.Checked) if (item.Checked)
{ {
config.Tablas.Add(new TablaInfo( config.Tablas.Add(new TablaInfo
item.SubItems[1].Text, // Esquema {
item.SubItems[0].Text // Nombre tabla Esquema = item.SubItems[1].Text,
)); Nombre = item.SubItems[0].Text
});
} }
} }
} }
// Metodo para seleccionar las tablas /// <summary>
// en le listview desde la configuracion /// Metodo para seleccionar las tablas en le listview desde la configuracion
private void tablas_alistview() /// </summary>
private void TablesToListView()
{ {
int i, j, n; int i, j, n;
@@ -105,7 +108,7 @@ namespace ServerExplorer
public DatabaseDesc CrearDatabaseDesc() public DatabaseDesc CrearDatabaseDesc()
{ {
DatabaseDesc db = new DatabaseDesc(cnx.Database); DatabaseDesc db = new DatabaseDesc { Nombre = cnx.Database };
foreach (TablaInfo t in config.Tablas) foreach (TablaInfo t in config.Tablas)
{ {
TablaDesc td = new TablaDesc(); TablaDesc td = new TablaDesc();
@@ -115,13 +118,13 @@ namespace ServerExplorer
return (db); return (db);
} }
public FrmBaseDatos(String ConString) public FrmBaseDatos(String ConnectionString)
{ {
InitializeComponent(); InitializeComponent();
instancia = this; instancia = this;
config = new Config(); config = new Config();
config.ConString = ConString; config.ConnectionString = ConnectionString;
} }
public FrmBaseDatos(Config config) public FrmBaseDatos(Config config)
@@ -136,7 +139,7 @@ namespace ServerExplorer
private void frmBaseDatos_Load(object sender, EventArgs e) private void frmBaseDatos_Load(object sender, EventArgs e)
{ {
inicializar(); Initialize();
// Establecer titulos // Establecer titulos
lblTituloDB.Text = cnx.Database; lblTituloDB.Text = cnx.Database;
@@ -157,11 +160,11 @@ namespace ServerExplorer
ListViewItem item = lsvTablas.SelectedItems[0]; ListViewItem item = lsvTablas.SelectedItems[0];
// Recordar tabla seleccionada // Recordar tabla seleccionada
table_schema = item.SubItems[1].Text.ToString(); tableSchema = item.SubItems[1].Text.ToString();
table_name = item.SubItems[0].Text.ToString(); tableName = item.SubItems[0].Text.ToString();
// Establecer titulo de la lista de columnas // Establecer titulo de la lista de columnas
lblTituloTabla.Text = table_schema + "." + table_name; lblTituloTabla.Text = tableSchema + "." + tableName;
// Obtener descripcion de tabla // Obtener descripcion de tabla
TablaDesc td = new TablaDesc(); TablaDesc td = new TablaDesc();
@@ -199,7 +202,7 @@ namespace ServerExplorer
if (dialogo.ShowDialog() == DialogResult.OK) if (dialogo.ShowDialog() == DialogResult.OK)
{ {
config = Config.Cargar(dialogo.FileName); config = Config.Cargar(dialogo.FileName);
inicializar(); Initialize();
} }
} }
@@ -208,7 +211,7 @@ namespace ServerExplorer
SaveFileDialog dialogo = new SaveFileDialog(); SaveFileDialog dialogo = new SaveFileDialog();
if (dialogo.ShowDialog() == DialogResult.OK) if (dialogo.ShowDialog() == DialogResult.OK)
{ {
tablas_delistview(); TablesFromListView();
config.Guardar(dialogo.FileName); config.Guardar(dialogo.FileName);
} }
} }
@@ -226,9 +229,9 @@ namespace ServerExplorer
DataTable dt; DataTable dt;
SqlDataAdapter da; SqlDataAdapter da;
if (table_schema == null || table_name == null) { return; } if (tableSchema == null || tableName == null) { return; }
nombreTabla = "[" + table_schema + "].[" + table_name + "]"; nombreTabla = "[" + tableSchema + "].[" + tableName + "]";
// Obtener un datatable con todos los registros de la tabla. // Obtener un datatable con todos los registros de la tabla.
da = new SqlDataAdapter( da = new SqlDataAdapter(
@@ -267,7 +270,7 @@ namespace ServerExplorer
form.Show();*/ form.Show();*/
// Crear y mostrar el formulario de los procedimientos. // Crear y mostrar el formulario de los procedimientos.
FrmProcedimientos form = new FrmProcedimientos(config.ConString); FrmProcedimientos form = new FrmProcedimientos(config.ConnectionString);
form.MdiParent = this.MdiParent; form.MdiParent = this.MdiParent;
form.Show(); form.Show();
} }
@@ -275,7 +278,7 @@ namespace ServerExplorer
private void btnExec_Click(object sender, EventArgs e) private void btnExec_Click(object sender, EventArgs e)
{ {
// Crear y mostrar el formulario de exec. // Crear y mostrar el formulario de exec.
FrmExec form = new FrmExec(config.ConString); FrmExec form = new FrmExec(config.ConnectionString);
form.MdiParent = this.MdiParent; form.MdiParent = this.MdiParent;
form.Show(); form.Show();
} }
@@ -288,7 +291,7 @@ namespace ServerExplorer
private void btnGenerar_Click(object sender, EventArgs e) private void btnGenerar_Click(object sender, EventArgs e)
{ {
tablas_delistview(); TablesFromListView();
DatabaseDesc db = CrearDatabaseDesc(); DatabaseDesc db = CrearDatabaseDesc();
XmlSerializer seriador = new XmlSerializer(typeof(DatabaseDesc)); XmlSerializer seriador = new XmlSerializer(typeof(DatabaseDesc));
@@ -303,11 +306,11 @@ namespace ServerExplorer
this.Parent.Enabled = false; this.Parent.Enabled = false;
// Obtener informacion. // Obtener informacion.
tablas_delistview(); TablesFromListView();
DatabaseDesc db = CrearDatabaseDesc(); DatabaseDesc db = CrearDatabaseDesc();
// Escribir documentacion // Escribir documentacion
DocGen.GenerarDocumentacion(config, db); DocGen.GenerarDocumentacion(db);
// Hace sensible la ventana // Hace sensible la ventana
this.Parent.Enabled = true; this.Parent.Enabled = true;
@@ -328,7 +331,7 @@ namespace ServerExplorer
DatabaseDesc db = CrearDatabaseDesc(); DatabaseDesc db = CrearDatabaseDesc();
// Escribir documentacion // Escribir documentacion
DocGen.GenerarDocumentacion(config, db); DocGen.GenerarDocumentacion(db);
// Hace sensible la ventana // Hace sensible la ventana
this.Enabled = true; this.Enabled = true;