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

View File

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