Mover código a sus namespaces correspondientes
This commit is contained in:
279
ServerExplorer/Code/TablaDesc.cs
Normal file
279
ServerExplorer/Code/TablaDesc.cs
Normal file
@@ -0,0 +1,279 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Data;
|
||||
using System.Data.Sql;
|
||||
using System.Data.SqlClient;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace ServerExplorer.Code
|
||||
{
|
||||
[Serializable]
|
||||
public class TablaDesc
|
||||
{
|
||||
#region Data parameters
|
||||
|
||||
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)
|
||||
{
|
||||
foreach (ColumnaDesc col in Columnas)
|
||||
{
|
||||
if (col.Nombre.CompareTo(nombre) == 0)
|
||||
{
|
||||
return (col);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void FillDesc(String esquema, String nombre, SqlConnection cnx)
|
||||
{
|
||||
SqlParameter prm;
|
||||
DataTable dt;
|
||||
SqlDataAdapter da;
|
||||
|
||||
// establecer esquema y nombre
|
||||
Esquema = esquema;
|
||||
Nombre = nombre;
|
||||
|
||||
// Preparar comando y parametros
|
||||
da = new SqlDataAdapter(
|
||||
"SELECT col.COLUMN_NAME AS Columna, " +
|
||||
" col.DATA_TYPE AS Tipo, " +
|
||||
" col.CHARACTER_MAXIMUM_LENGTH AS Tamanho, " +
|
||||
" c.CONSTRAINT_TYPE AS TipoClave, " +
|
||||
" col.IS_NULLABLE AS Nullable " +
|
||||
" FROM INFORMATION_SCHEMA.COLUMNS AS col " +
|
||||
" LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS k " +
|
||||
" ON col.COLUMN_NAME=k.COLUMN_NAME AND " +
|
||||
" col.TABLE_NAME=k.TABLE_NAME AND " +
|
||||
" col.TABLE_SCHEMA=k.TABLE_SCHEMA " +
|
||||
" LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS c " +
|
||||
" ON k.CONSTRAINT_NAME=c.CONSTRAINT_NAME " +
|
||||
" WHERE col.TABLE_NAME=@nombreTabla AND " +
|
||||
" col.TABLE_SCHEMA=@nombreEsquema " +
|
||||
" ORDER BY col.ORDINAL_POSITION",
|
||||
cnx);
|
||||
prm = new SqlParameter("@nombreTabla", SqlDbType.VarChar, 100);
|
||||
prm.Value = nombre;
|
||||
da.SelectCommand.Parameters.Add(prm);
|
||||
prm = new SqlParameter("@nombreEsquema", SqlDbType.VarChar, 100);
|
||||
prm.Value = esquema;
|
||||
da.SelectCommand.Parameters.Add(prm);
|
||||
|
||||
// Obtener datatable con las columnas
|
||||
dt = new DataTable();
|
||||
cnx.Open();
|
||||
da.Fill(dt);
|
||||
cnx.Close();
|
||||
|
||||
// Recorrer datatable estableciendo la lista de columnas
|
||||
Columnas.Clear();
|
||||
foreach (DataRow dr in dt.Rows)
|
||||
{
|
||||
ColumnaDesc col;
|
||||
|
||||
// Obtener columna
|
||||
col = GetCol((String)dr["Columna"]);
|
||||
if (col == null)
|
||||
{
|
||||
col = new ColumnaDesc();
|
||||
Columnas.Add(col);
|
||||
}
|
||||
|
||||
// Establecer datos de la columna
|
||||
col.Nombre = (String)dr["Columna"];
|
||||
col.Tipo = ((String)dr["Tipo"]).ToLower();
|
||||
if (dr["Tamanho"] != DBNull.Value)
|
||||
{
|
||||
col.Tamanho = (int)dr["Tamanho"];
|
||||
}
|
||||
if (dr["TipoClave"] != DBNull.Value)
|
||||
{
|
||||
if (((String)dr["TipoClave"]).Contains("PRIMARY"))
|
||||
{
|
||||
col.Primaria = true;
|
||||
}
|
||||
}
|
||||
string strNullable=((String)dr["Nullable"]).ToLower();
|
||||
col.Nullable = (strNullable == "yes");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
#region TipoCol
|
||||
|
||||
public enum TipoCol
|
||||
{
|
||||
Unset,
|
||||
Numerico,
|
||||
AproxNumerico,
|
||||
Tiempo,
|
||||
Texto,
|
||||
Binario,
|
||||
Booleano,
|
||||
Otro
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
[Serializable]
|
||||
public class ColumnaDesc
|
||||
{
|
||||
#region Data properties
|
||||
|
||||
private string nombre = String.Empty;
|
||||
[XmlAttribute("Nombre")]
|
||||
public string Nombre
|
||||
{
|
||||
get { return nombre; }
|
||||
set { nombre = value; }
|
||||
}
|
||||
|
||||
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 nullable = false;
|
||||
[XmlAttribute("Nullable")]
|
||||
public bool Nullable
|
||||
{
|
||||
get { return nullable; }
|
||||
set { nullable = 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 strTipo = Tipo.ToLower();
|
||||
|
||||
if (_tipo != TipoCol.Unset)
|
||||
{
|
||||
return _tipo;
|
||||
}
|
||||
|
||||
// Numericos
|
||||
if (
|
||||
strTipo == "bigint" ||
|
||||
strTipo == "int" ||
|
||||
strTipo == "smallint" ||
|
||||
strTipo == "tinyint" ||
|
||||
strTipo == "bigint"
|
||||
)
|
||||
{
|
||||
_tipo = TipoCol.Numerico;
|
||||
}
|
||||
|
||||
// Aproximados numericos
|
||||
if (
|
||||
strTipo == "float" ||
|
||||
strTipo == "real"
|
||||
)
|
||||
{
|
||||
_tipo = TipoCol.AproxNumerico;
|
||||
}
|
||||
|
||||
// Tiempo
|
||||
if (
|
||||
strTipo == "date" ||
|
||||
strTipo == "datetimeoffset" ||
|
||||
strTipo == "datetime2" ||
|
||||
strTipo == "smalldatetime" ||
|
||||
strTipo == "datetime" ||
|
||||
strTipo == "time"
|
||||
)
|
||||
{
|
||||
_tipo = TipoCol.Tiempo;
|
||||
}
|
||||
|
||||
// Texto
|
||||
if (
|
||||
strTipo == "char" ||
|
||||
strTipo == "varchar" ||
|
||||
strTipo == "text" ||
|
||||
strTipo == "nchar" ||
|
||||
strTipo == "nvarchar" ||
|
||||
strTipo == "ntext"
|
||||
)
|
||||
{
|
||||
_tipo = TipoCol.Texto;
|
||||
}
|
||||
|
||||
// Binario
|
||||
if (
|
||||
strTipo == "binary" ||
|
||||
strTipo == "varbinary" ||
|
||||
strTipo == "image"
|
||||
)
|
||||
{
|
||||
_tipo = TipoCol.Binario;
|
||||
}
|
||||
|
||||
// Booleano
|
||||
if (
|
||||
strTipo == "bit"
|
||||
)
|
||||
{
|
||||
_tipo = TipoCol.Booleano;
|
||||
}
|
||||
|
||||
// Otro
|
||||
_tipo = TipoCol.Otro;
|
||||
|
||||
return _tipo;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user