205 lines
5.6 KiB
C#
205 lines
5.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Data;
|
|
using System.Data.Sql;
|
|
using System.Data.SqlClient;
|
|
|
|
namespace ServerExplorer
|
|
{
|
|
public class TablaDesc
|
|
{
|
|
public String Esquema="";
|
|
public String Nombre="";
|
|
public List<ColumnaDesc> Columnas = new List<ColumnaDesc>();
|
|
|
|
public TablaDesc() { }
|
|
|
|
// 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 " +
|
|
" 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"];
|
|
if (dr["Tamanho"] != DBNull.Value)
|
|
{
|
|
col.Tamanho = (int)dr["Tamanho"];
|
|
}
|
|
if (dr["TipoClave"] != DBNull.Value)
|
|
{
|
|
if (((String)dr["TipoClave"]).Contains("PRIMARY"))
|
|
{
|
|
col.Primaria = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public enum TipoCol
|
|
{
|
|
Unset,
|
|
Numerico,
|
|
AproxNumerico,
|
|
Tiempo,
|
|
Texto,
|
|
Binario,
|
|
Booleano,
|
|
Otro
|
|
}
|
|
|
|
public class ColumnaDesc
|
|
{
|
|
public string Nombre = "";
|
|
public string Tipo = "";
|
|
public int Tamanho = -1;
|
|
public bool Primaria = false;
|
|
|
|
public ColumnaDesc() { }
|
|
|
|
private TipoCol tipo = TipoCol.Unset;
|
|
public TipoCol GetTipo()
|
|
{
|
|
string stipo=Tipo.ToLower();
|
|
|
|
if (this.tipo != TipoCol.Unset)
|
|
{
|
|
return this.tipo;
|
|
}
|
|
|
|
// Numericos
|
|
if(
|
|
stipo=="bigint" ||
|
|
stipo=="int" ||
|
|
stipo=="smallint" ||
|
|
stipo=="tinyint" ||
|
|
stipo=="bigint"
|
|
)
|
|
{
|
|
return TipoCol.Numerico;
|
|
}
|
|
|
|
// Aproximados numericos
|
|
if (
|
|
stipo == "float" ||
|
|
stipo == "real"
|
|
)
|
|
{
|
|
return TipoCol.AproxNumerico;
|
|
}
|
|
|
|
// Tiempo
|
|
if (
|
|
stipo == "date" ||
|
|
stipo == "datetimeoffset" ||
|
|
stipo == "datetime2" ||
|
|
stipo == "smalldatetime" ||
|
|
stipo == "datetime" ||
|
|
stipo == "time"
|
|
)
|
|
{
|
|
return TipoCol.Tiempo;
|
|
}
|
|
|
|
// Texto
|
|
if (
|
|
stipo == "char" ||
|
|
stipo == "varchar" ||
|
|
stipo == "text" ||
|
|
stipo == "nchar" ||
|
|
stipo == "nvarchar" ||
|
|
stipo == "ntext"
|
|
)
|
|
{
|
|
return TipoCol.Texto;
|
|
}
|
|
|
|
// Binario
|
|
if (
|
|
stipo == "binary" ||
|
|
stipo == "varbinary" ||
|
|
stipo == "image"
|
|
)
|
|
{
|
|
return TipoCol.Binario;
|
|
}
|
|
|
|
// Booleano
|
|
if (
|
|
stipo == "bit"
|
|
)
|
|
{
|
|
return TipoCol.Booleano;
|
|
}
|
|
|
|
// Otro
|
|
return TipoCol.Otro;
|
|
}
|
|
}
|
|
}
|