Remove old code, with data objects and business objects joined.
This commit is contained in:
@@ -1,12 +1,94 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
using VAR.DatabaseExplorer.Code.DataAccess;
|
||||
using VAR.DatabaseExplorer.Code.DataTransfer;
|
||||
|
||||
namespace VAR.DatabaseExplorer.Code.BusinessLogic
|
||||
{
|
||||
public class DatabaseBL
|
||||
{
|
||||
internal static Database Database_Get(string connectionString)
|
||||
{
|
||||
Database database = DatabaseDA.GetInfo(connectionString);
|
||||
return database;
|
||||
}
|
||||
|
||||
public static Database Database_GetSchema(string connectionString, bool fillDefinitions = false)
|
||||
{
|
||||
Database database = DatabaseDA.GetInfo(connectionString);
|
||||
database.Tables = TableBL.Table_GetAll(connectionString, fillDefinitions);
|
||||
database.Procedures = ProcedureBL.Procedure_GetAll(connectionString, fillDefinitions);
|
||||
return database;
|
||||
}
|
||||
|
||||
public static void Database_GenerateDocumentation(TextWriter txtWriter, Database database)
|
||||
{
|
||||
// Poner cabecera de la documentacion
|
||||
txtWriter.WriteLine("<!DOCTYPE html>");
|
||||
txtWriter.WriteLine("<html><head><title>" + database.Name + "</title></head>");
|
||||
txtWriter.WriteLine("<body>");
|
||||
|
||||
// Iterar cada tabla
|
||||
foreach (Table table in database.Tables)
|
||||
{
|
||||
// Cabecera de la info de tabla
|
||||
txtWriter.WriteLine("<h2>" + table.Schema + "." + table.Name + "</h2>");
|
||||
|
||||
// Iterar las columnas
|
||||
txtWriter.WriteLine("<table>");
|
||||
txtWriter.WriteLine("<thead><tr><th>Nombre</th><th>Tipo</th><th>Tamaño</th><th>Primaria</th></tr></thead>");
|
||||
txtWriter.WriteLine("<tbody>");
|
||||
foreach (Column c in table.Columns)
|
||||
{
|
||||
txtWriter.WriteLine("<tr><td>" +
|
||||
c.Name + "</td><td>" +
|
||||
c.Type + "</td><td>" +
|
||||
c.Size + "</td><td>" +
|
||||
c.PK + "</td></tr>");
|
||||
}
|
||||
txtWriter.WriteLine("</tbody></table>");
|
||||
}
|
||||
|
||||
// Iterar cada procedimiento
|
||||
foreach (Procedure proc in database.Procedures)
|
||||
{
|
||||
// Cabecera de la info del procedimiento
|
||||
txtWriter.WriteLine("<h2>" + proc.Schema + "." + proc.Name + "</h2>");
|
||||
|
||||
txtWriter.WriteLine("<pre><code>");
|
||||
txtWriter.WriteLine(proc.Definition);
|
||||
txtWriter.WriteLine("</code></pre>");
|
||||
}
|
||||
|
||||
// Poner pie y cerrar fichero
|
||||
txtWriter.WriteLine("</body></html>");
|
||||
}
|
||||
|
||||
public static void Database_ExportData(TextWriter txtWriter, string connectionString, Database database)
|
||||
{
|
||||
// Preparar cabecera del script
|
||||
txtWriter.WriteLine("SET NOCOUNT ON;");
|
||||
txtWriter.WriteLine(string.Empty);
|
||||
|
||||
// Desactivar todas las FKs
|
||||
txtWriter.WriteLine("-- Disable all constraints");
|
||||
txtWriter.WriteLine("EXEC sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT all'");
|
||||
txtWriter.WriteLine("GO");
|
||||
txtWriter.WriteLine(string.Empty);
|
||||
|
||||
// Prepara informacion a exportar
|
||||
foreach (Table t in database.Tables)
|
||||
{
|
||||
string tableName = string.Format("{0}.{1}", t.Schema, t.Name);
|
||||
txtWriter.WriteLine(string.Format("PRINT '*** Importing data of {0}....';", tableName));
|
||||
TableBL.Table_ExportData(txtWriter, connectionString, tableName);
|
||||
}
|
||||
|
||||
// Activar todas las FKs
|
||||
txtWriter.WriteLine("-- Enable all constraints");
|
||||
txtWriter.WriteLine("EXEC sp_MSforeachtable @command1='print ''?''', @command2='ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all'");
|
||||
txtWriter.WriteLine("GO");
|
||||
txtWriter.WriteLine(string.Empty);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
using System.IO;
|
||||
|
||||
namespace VAR.DatabaseExplorer.Code.BusinessLogic
|
||||
{
|
||||
public class DocumentationGeneratorBL
|
||||
{
|
||||
public static void GenerateDocumentation(DatabaseDesc database)
|
||||
{
|
||||
// Abrir el documento que contendra la documentacion
|
||||
string fixedDatabaseName = database.Nombre.Replace(' ', '_');
|
||||
var escritor = new StreamWriter(fixedDatabaseName + ".documentacion.html");
|
||||
|
||||
// Poner cabecera de la documentacion
|
||||
escritor.WriteLine("<!DOCTYPE html>");
|
||||
escritor.WriteLine("<html><head><title>" + database.Nombre + "</title></head>");
|
||||
escritor.WriteLine("<body>");
|
||||
|
||||
// Iterar cada tabla
|
||||
foreach (TablaDesc t in database.Tablas)
|
||||
{
|
||||
// Cabecera de la info de tabla
|
||||
escritor.WriteLine("<h2>" + t.Esquema + "." + t.Nombre + "</h2>");
|
||||
|
||||
// Iterar las columnas
|
||||
escritor.WriteLine("<table>");
|
||||
escritor.WriteLine("<thead><tr><th>Nombre</th><th>Tipo</th><th>Tamaño</th><th>Primaria</th></tr></thead>");
|
||||
escritor.WriteLine("<tbody>");
|
||||
foreach (ColumnaDesc c in t.Columnas)
|
||||
{
|
||||
escritor.WriteLine("<tr><td>" +
|
||||
c.Nombre + "</td><td>" +
|
||||
c.Tipo + "</td><td>" +
|
||||
c.Tamanho + "</td><td>" +
|
||||
c.Primaria + "</td></tr>");
|
||||
}
|
||||
escritor.WriteLine("</tbody></table>");
|
||||
}
|
||||
|
||||
// Poner pie y cerrar fichero
|
||||
escritor.WriteLine("</body></html>");
|
||||
escritor.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
23
VAR.DatabaseExplorer/Code/BusinessLogic/ProcedureBL.cs
Normal file
23
VAR.DatabaseExplorer/Code/BusinessLogic/ProcedureBL.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System.Collections.Generic;
|
||||
using VAR.DatabaseExplorer.Code.DataAccess;
|
||||
using VAR.DatabaseExplorer.Code.DataTransfer;
|
||||
|
||||
namespace VAR.DatabaseExplorer.Code.BusinessLogic
|
||||
{
|
||||
public class ProcedureBL
|
||||
{
|
||||
public static List<Procedure> Procedure_GetAll(string connectionString, bool fillDefinition = false)
|
||||
{
|
||||
List<Procedure> procedures = ProcedureDA.GetAllProcedures(connectionString);
|
||||
if (fillDefinition)
|
||||
{
|
||||
foreach (Procedure procedure in procedures)
|
||||
{
|
||||
procedure.Definition = ProcedureDA.GetProcedureDefinition(connectionString, procedure.Schema, procedure.Name);
|
||||
}
|
||||
}
|
||||
return procedures;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,39 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using VAR.DatabaseExplorer.Code.DataAccess;
|
||||
using VAR.DatabaseExplorer.Code.DataTransfer;
|
||||
|
||||
namespace VAR.DatabaseExplorer.Code.BusinessLogic
|
||||
{
|
||||
public class TableBL
|
||||
{
|
||||
public static List<string> ExportData(string conectionString, string tableName)
|
||||
public static List<Table> Table_GetAll(string connectionString, bool fillColumns = false)
|
||||
{
|
||||
DataTable dtData = TableDA.GetData(conectionString, tableName);
|
||||
|
||||
var listCmds = new List<string>();
|
||||
List<string> listCmdsInsert = DataTableHelper.DataTable_GenerateInserts(dtData, tableName);
|
||||
|
||||
listCmds.Add(string.Format("-- {0}", tableName));
|
||||
listCmds.Add(string.Format("DELETE FROM {0};", tableName));
|
||||
listCmds.Add(string.Format("DBCC CHECKIDENT ('{0}', RESEED, 0);", tableName));
|
||||
listCmds.Add(string.Format("SET IDENTITY_INSERT {0} ON;", tableName));
|
||||
listCmds.AddRange(listCmdsInsert);
|
||||
listCmds.Add(string.Format("SET IDENTITY_INSERT {0} OFF;", tableName));
|
||||
listCmds.Add("GO");
|
||||
listCmds.Add(string.Empty);
|
||||
|
||||
return listCmds;
|
||||
List<Table> tables = TableDA.GetAllTables(connectionString);
|
||||
if (fillColumns)
|
||||
{
|
||||
foreach(Table table in tables)
|
||||
{
|
||||
table.Columns = ColumnDA.GetColumns(connectionString, table.Schema, table.Name);
|
||||
}
|
||||
}
|
||||
return tables;
|
||||
}
|
||||
|
||||
public static void Table_ExportData(TextWriter txtWriter, string conectionString, string tableFullName)
|
||||
{
|
||||
DataTable dtData = TableDA.GetData(conectionString, tableFullName);
|
||||
|
||||
txtWriter.WriteLine(string.Format("-- {0}", tableFullName));
|
||||
txtWriter.WriteLine(string.Format("DELETE FROM {0};", tableFullName));
|
||||
txtWriter.WriteLine(string.Format("DBCC CHECKIDENT ('{0}', RESEED, 0);", tableFullName));
|
||||
txtWriter.WriteLine(string.Format("SET IDENTITY_INSERT {0} ON;", tableFullName));
|
||||
DataTableHelper.DataTable_GenerateInserts(txtWriter, dtData, tableFullName);
|
||||
txtWriter.WriteLine(string.Format("SET IDENTITY_INSERT {0} OFF;", tableFullName));
|
||||
txtWriter.WriteLine("GO");
|
||||
txtWriter.WriteLine(string.Empty);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace VAR.DatabaseExplorer.Code
|
||||
{
|
||||
[Serializable]
|
||||
public class Config
|
||||
{
|
||||
#region Data parameters
|
||||
|
||||
private string _conString = "";
|
||||
[XmlAttribute("ConnectionString")]
|
||||
public string ConnectionString
|
||||
{
|
||||
get { return _conString; }
|
||||
set { _conString = value; }
|
||||
}
|
||||
|
||||
private readonly List<TablaInfo> _tablas = new List<TablaInfo>();
|
||||
[XmlArray("Tablas")]
|
||||
public List<TablaInfo> Tablas { get { return _tablas; } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Persistence methods
|
||||
|
||||
public void Guardar(String fichero)
|
||||
{
|
||||
var seriador = new XmlSerializer(typeof(Config));
|
||||
var escritor = new StreamWriter(fichero);
|
||||
seriador.Serialize(escritor, this);
|
||||
escritor.Close();
|
||||
}
|
||||
|
||||
public static Config Cargar(String fichero)
|
||||
{
|
||||
var seriador = new XmlSerializer(typeof(Config));
|
||||
var lector = new StreamReader(fichero);
|
||||
var config = (Config)seriador.Deserialize(lector);
|
||||
lector.Close();
|
||||
return config;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class TablaInfo
|
||||
{
|
||||
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; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,10 +8,10 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
|
||||
{
|
||||
public class DatabaseDA
|
||||
{
|
||||
public static List<Database> GetAllDatabases(string conexionString)
|
||||
public static List<Database> GetAllDatabases(string connectionString)
|
||||
{
|
||||
var databases = new List<Database>();
|
||||
var cnx = new SqlConnection(conexionString);
|
||||
var cnx = new SqlConnection(connectionString);
|
||||
|
||||
cnx.Open();
|
||||
DataTable dt = cnx.GetSchema("Databases");
|
||||
@@ -29,5 +29,30 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
|
||||
}
|
||||
return databases;
|
||||
}
|
||||
|
||||
|
||||
public static Database GetInfo(string connectionString)
|
||||
{
|
||||
var cnx = new SqlConnection(connectionString);
|
||||
string strCmd = string.Format(@"
|
||||
SELECT
|
||||
[name] DatabaseName,
|
||||
[create_date] CreateDate
|
||||
FROM sys.databases
|
||||
WHERE database_id = DB_ID();
|
||||
");
|
||||
var da = new SqlDataAdapter(strCmd, cnx);
|
||||
var dt = new DataTable();
|
||||
cnx.Open();
|
||||
da.Fill(dt);
|
||||
cnx.Close();
|
||||
|
||||
var database = new Database
|
||||
{
|
||||
Name = Convert.ToString(dt.Rows[0]["DatabaseName"]),
|
||||
CreateDate = Convert.ToDateTime(dt.Rows[0]["CreateDate"]),
|
||||
};
|
||||
return database;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,19 +2,17 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using VAR.DatabaseExplorer.Code.DataTransfer;
|
||||
|
||||
namespace VAR.DatabaseExplorer.Code.DataAccess
|
||||
{
|
||||
public class ProcedureDA
|
||||
{
|
||||
public static List<Procedure> GetAllProcedures(string conexionString)
|
||||
public static List<Procedure> GetAllProcedures(string connectionString)
|
||||
{
|
||||
var procedures = new List<Procedure>();
|
||||
var cnx = new SqlConnection(conexionString);
|
||||
var cnx = new SqlConnection(connectionString);
|
||||
|
||||
var da = new SqlDataAdapter(
|
||||
"SELECT ROUTINE_NAME Name, ROUTINE_SCHEMA [Schema], CREATED CreateDate, ROUTINE_TYPE [Type] FROM INFORMATION_SCHEMA.ROUTINES",
|
||||
@@ -37,10 +35,10 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
|
||||
return procedures;
|
||||
}
|
||||
|
||||
public static string GetProcedureDefinition(string conexionString, string schema, string name)
|
||||
public static string GetProcedureDefinition(string connectionString, string schema, string name)
|
||||
{
|
||||
SqlDataAdapter dataAdapter;
|
||||
var cnx = new SqlConnection(conexionString);
|
||||
var cnx = new SqlConnection(connectionString);
|
||||
cnx.Open();
|
||||
|
||||
if (cnx.ServerVersion.StartsWith("10.") || cnx.ServerVersion.StartsWith("11."))
|
||||
|
||||
@@ -7,10 +7,10 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
|
||||
{
|
||||
public class TableDA
|
||||
{
|
||||
public static List<Table> GetAllTables(string conexionString)
|
||||
public static List<Table> GetAllTables(string connectionString)
|
||||
{
|
||||
var tables = new List<Table>();
|
||||
var cnx = new SqlConnection(conexionString);
|
||||
var cnx = new SqlConnection(connectionString);
|
||||
|
||||
cnx.Open();
|
||||
DataTable dt = cnx.GetSchema("Tables");
|
||||
@@ -20,21 +20,23 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
|
||||
|
||||
foreach (DataRow dr in dt.Rows)
|
||||
{
|
||||
tables.Add(new Table
|
||||
Table table = new Table
|
||||
{
|
||||
Schema = (string)dr["TABLE_SCHEMA"],
|
||||
Name = (string)dr["TABLE_NAME"],
|
||||
Type = (string)dr["TABLE_TYPE"]
|
||||
});
|
||||
};
|
||||
|
||||
tables.Add(table);
|
||||
}
|
||||
|
||||
return tables;
|
||||
}
|
||||
|
||||
public static DataTable GetData(string conexionString, string tableName)
|
||||
public static DataTable GetData(string connectionString, string tableFullName)
|
||||
{
|
||||
var cnx = new SqlConnection(conexionString);
|
||||
string strCmd = string.Format("SELECT * FROM {0}", tableName);
|
||||
var cnx = new SqlConnection(connectionString);
|
||||
string strCmd = string.Format("SELECT * FROM {0}", tableFullName);
|
||||
var da = new SqlDataAdapter(strCmd, cnx);
|
||||
var dt = new DataTable();
|
||||
cnx.Open();
|
||||
|
||||
@@ -2,29 +2,31 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace VAR.DatabaseExplorer.Code
|
||||
{
|
||||
public static class DataTableHelper
|
||||
{
|
||||
public static List<string> DataTable_GenerateInserts(DataTable dataTable, string destTable)
|
||||
public static void DataTable_GenerateInserts(TextWriter txtWriter, DataTable dataTable, string destTable)
|
||||
{
|
||||
var nfi = new NumberFormatInfo { NumberDecimalSeparator = "." };
|
||||
|
||||
var listCmds = new List<string>();
|
||||
var sbColumns = new StringBuilder();
|
||||
foreach (DataColumn dc in dataTable.Columns)
|
||||
{
|
||||
// El nombre de la columna
|
||||
if (sbColumns.Length > 0) { sbColumns.Append(", "); }
|
||||
sbColumns.AppendFormat("[{0}]", dc.ColumnName);
|
||||
}
|
||||
|
||||
// Recorrer la tabla de datos
|
||||
foreach (DataRow dr in dataTable.Rows)
|
||||
{
|
||||
var sbColumns = new StringBuilder();
|
||||
var sbValues = new StringBuilder();
|
||||
foreach (DataColumn dc in dataTable.Columns)
|
||||
{
|
||||
// El nombre de la columna
|
||||
if (sbColumns.Length > 0) { sbColumns.Append(", "); }
|
||||
sbColumns.AppendFormat("[{0}]", dc.ColumnName);
|
||||
|
||||
// El valor de la columna
|
||||
if (sbValues.Length > 0) { sbValues.Append(", "); }
|
||||
object valor = dr[dc];
|
||||
@@ -41,7 +43,12 @@ namespace VAR.DatabaseExplorer.Code
|
||||
// Cadenas
|
||||
sbValues.AppendFormat("'{0}'", ((string)valor).Replace("'", "''"));
|
||||
}
|
||||
else if (type == "int" || type == "int32" || type == "int64")
|
||||
else if (type == "short" || type == "int16")
|
||||
{
|
||||
// short
|
||||
sbValues.Append(((short)valor).ToString(nfi));
|
||||
}
|
||||
else if (type == "int" || type == "int32")
|
||||
{
|
||||
// int
|
||||
sbValues.Append(((int)valor).ToString(nfi));
|
||||
@@ -81,11 +88,9 @@ namespace VAR.DatabaseExplorer.Code
|
||||
}
|
||||
|
||||
// Insertar fila a la datatable destino
|
||||
listCmds.Add(string.Format("INSERT INTO {0} ({1}) VALUES ({2});",
|
||||
destTable, sbColumns.ToString(), sbValues.ToString()));
|
||||
txtWriter.WriteLine(string.Format("INSERT INTO {0} ({1}) VALUES ({2});",
|
||||
destTable, sbColumns.ToString(), sbValues.ToString()));
|
||||
}
|
||||
|
||||
return listCmds;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace VAR.DatabaseExplorer.Code
|
||||
{
|
||||
[Serializable]
|
||||
public class DatabaseDesc
|
||||
{
|
||||
private string _nombre = string.Empty;
|
||||
[XmlAttribute("Nombre")]
|
||||
public string Nombre
|
||||
{
|
||||
get { return _nombre; }
|
||||
set { _nombre = value; }
|
||||
}
|
||||
|
||||
private readonly List<TablaDesc> _tablas = new List<TablaDesc>();
|
||||
[XmlArray("Tablas")]
|
||||
public List<TablaDesc> Tablas { get { return _tablas; } }
|
||||
}
|
||||
}
|
||||
@@ -1,280 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace VAR.DatabaseExplorer.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 readonly 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 (String.Compare(col.Nombre, nombre, StringComparison.Ordinal) == 0)
|
||||
{
|
||||
return (col);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void FillDesc(String esquema, String nombre, SqlConnection cnx)
|
||||
{
|
||||
// establecer esquema y nombre
|
||||
Esquema = esquema;
|
||||
Nombre = nombre;
|
||||
|
||||
// Preparar comando y parametros
|
||||
var 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);
|
||||
var prm = new SqlParameter("@nombreTabla", SqlDbType.VarChar, 100) {Value = nombre};
|
||||
da.SelectCommand.Parameters.Add(prm);
|
||||
prm = new SqlParameter("@nombreEsquema", SqlDbType.VarChar, 100) {Value = esquema};
|
||||
da.SelectCommand.Parameters.Add(prm);
|
||||
|
||||
// Obtener datatable con las columnas
|
||||
var 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)
|
||||
{
|
||||
// Obtener columna
|
||||
ColumnaDesc 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;
|
||||
|
||||
[XmlAttribute("Nullable")]
|
||||
public bool Nullable
|
||||
{
|
||||
get { return _nullable; }
|
||||
set { _nullable = value; }
|
||||
}
|
||||
|
||||
private bool _primaria;
|
||||
|
||||
[XmlAttribute("Primaria")]
|
||||
public bool Primaria
|
||||
{
|
||||
get { return _primaria; }
|
||||
set { _primaria = value; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region GetTipo
|
||||
|
||||
private TipoCol _tipoCol = TipoCol.Unset;
|
||||
|
||||
public TipoCol GetTipo()
|
||||
{
|
||||
string strTipo = Tipo.ToLower();
|
||||
|
||||
if (_tipoCol != TipoCol.Unset)
|
||||
{
|
||||
return _tipoCol;
|
||||
}
|
||||
|
||||
// Numericos
|
||||
if (
|
||||
strTipo == "bigint" ||
|
||||
strTipo == "int" ||
|
||||
strTipo == "smallint" ||
|
||||
strTipo == "tinyint" ||
|
||||
strTipo == "bigint"
|
||||
)
|
||||
{
|
||||
_tipoCol = TipoCol.Numerico;
|
||||
}
|
||||
|
||||
// Aproximados numericos
|
||||
if (
|
||||
strTipo == "float" ||
|
||||
strTipo == "real"
|
||||
)
|
||||
{
|
||||
_tipoCol = TipoCol.AproxNumerico;
|
||||
}
|
||||
|
||||
// Tiempo
|
||||
if (
|
||||
strTipo == "date" ||
|
||||
strTipo == "datetimeoffset" ||
|
||||
strTipo == "datetime2" ||
|
||||
strTipo == "smalldatetime" ||
|
||||
strTipo == "datetime" ||
|
||||
strTipo == "time"
|
||||
)
|
||||
{
|
||||
_tipoCol = TipoCol.Tiempo;
|
||||
}
|
||||
|
||||
// Texto
|
||||
if (
|
||||
strTipo == "char" ||
|
||||
strTipo == "varchar" ||
|
||||
strTipo == "text" ||
|
||||
strTipo == "nchar" ||
|
||||
strTipo == "nvarchar" ||
|
||||
strTipo == "ntext"
|
||||
)
|
||||
{
|
||||
_tipoCol = TipoCol.Texto;
|
||||
}
|
||||
|
||||
// Binario
|
||||
if (
|
||||
strTipo == "binary" ||
|
||||
strTipo == "varbinary" ||
|
||||
strTipo == "image"
|
||||
)
|
||||
{
|
||||
_tipoCol = TipoCol.Binario;
|
||||
}
|
||||
|
||||
// Booleano
|
||||
if (
|
||||
strTipo == "bit"
|
||||
)
|
||||
{
|
||||
_tipoCol = TipoCol.Booleano;
|
||||
}
|
||||
|
||||
// Otro
|
||||
_tipoCol = TipoCol.Otro;
|
||||
|
||||
return _tipoCol;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
76
VAR.DatabaseExplorer/UI/FrmBaseDatos.Designer.cs
generated
76
VAR.DatabaseExplorer/UI/FrmBaseDatos.Designer.cs
generated
@@ -31,11 +31,6 @@
|
||||
this.lblConString = new System.Windows.Forms.Label();
|
||||
this.btnCopiarConString = new System.Windows.Forms.Button();
|
||||
this.menuBaseDatos = new System.Windows.Forms.MenuStrip();
|
||||
this.archivoToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.menuCargar = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.menuGuardar = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.menuConfiguracion = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.btnGenerar = new System.Windows.Forms.Button();
|
||||
this.btnVerDatos = new System.Windows.Forms.Button();
|
||||
this.lblTituloTabla = new System.Windows.Forms.Label();
|
||||
this.lblTituloDB = new System.Windows.Forms.Label();
|
||||
@@ -55,7 +50,7 @@
|
||||
this.colClave = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
|
||||
this.colNullable = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
|
||||
this.txtConString = new VAR.DatabaseExplorer.Controls.CustomTextBox();
|
||||
this.menuBaseDatos.SuspendLayout();
|
||||
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
|
||||
this.splitContainer1.Panel1.SuspendLayout();
|
||||
this.splitContainer1.Panel2.SuspendLayout();
|
||||
this.splitContainer1.SuspendLayout();
|
||||
@@ -83,63 +78,15 @@
|
||||
//
|
||||
// menuBaseDatos
|
||||
//
|
||||
this.menuBaseDatos.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
|
||||
this.archivoToolStripMenuItem,
|
||||
this.menuConfiguracion});
|
||||
this.menuBaseDatos.Location = new System.Drawing.Point(0, 0);
|
||||
this.menuBaseDatos.Name = "menuBaseDatos";
|
||||
this.menuBaseDatos.Size = new System.Drawing.Size(806, 24);
|
||||
this.menuBaseDatos.TabIndex = 9;
|
||||
this.menuBaseDatos.Text = "menuBaseDatos";
|
||||
this.menuBaseDatos.Visible = false;
|
||||
this.menuBaseDatos.ItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.menuBaseDatos_ItemClicked);
|
||||
//
|
||||
// archivoToolStripMenuItem
|
||||
//
|
||||
this.archivoToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
|
||||
this.menuCargar,
|
||||
this.menuGuardar});
|
||||
this.archivoToolStripMenuItem.MergeIndex = 0;
|
||||
this.archivoToolStripMenuItem.Name = "archivoToolStripMenuItem";
|
||||
this.archivoToolStripMenuItem.Size = new System.Drawing.Size(92, 20);
|
||||
this.archivoToolStripMenuItem.Text = "Base de Datos";
|
||||
//
|
||||
// menuCargar
|
||||
//
|
||||
this.menuCargar.Name = "menuCargar";
|
||||
this.menuCargar.Size = new System.Drawing.Size(116, 22);
|
||||
this.menuCargar.Text = "Cargar";
|
||||
this.menuCargar.Click += new System.EventHandler(this.menuCargar_Click);
|
||||
//
|
||||
// menuGuardar
|
||||
//
|
||||
this.menuGuardar.Name = "menuGuardar";
|
||||
this.menuGuardar.Size = new System.Drawing.Size(116, 22);
|
||||
this.menuGuardar.Text = "Guardar";
|
||||
this.menuGuardar.Click += new System.EventHandler(this.menuGuardar_Click);
|
||||
//
|
||||
// menuConfiguracion
|
||||
//
|
||||
this.menuConfiguracion.Name = "menuConfiguracion";
|
||||
this.menuConfiguracion.Size = new System.Drawing.Size(95, 20);
|
||||
this.menuConfiguracion.Text = "Configuracion";
|
||||
this.menuConfiguracion.Click += new System.EventHandler(this.menuConfiguracion_Click);
|
||||
//
|
||||
// btnGenerar
|
||||
//
|
||||
this.btnGenerar.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.btnGenerar.Location = new System.Drawing.Point(392, 485);
|
||||
this.btnGenerar.Name = "btnGenerar";
|
||||
this.btnGenerar.Size = new System.Drawing.Size(75, 23);
|
||||
this.btnGenerar.TabIndex = 10;
|
||||
this.btnGenerar.Text = "Generar";
|
||||
this.btnGenerar.UseVisualStyleBackColor = true;
|
||||
this.btnGenerar.Click += new System.EventHandler(this.btnGenerar_Click);
|
||||
this.menuBaseDatos.TabIndex = 18;
|
||||
//
|
||||
// btnVerDatos
|
||||
//
|
||||
this.btnVerDatos.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
|
||||
this.btnVerDatos.Location = new System.Drawing.Point(3, 485);
|
||||
this.btnVerDatos.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.btnVerDatos.Location = new System.Drawing.Point(392, 485);
|
||||
this.btnVerDatos.Name = "btnVerDatos";
|
||||
this.btnVerDatos.Size = new System.Drawing.Size(75, 23);
|
||||
this.btnVerDatos.TabIndex = 11;
|
||||
@@ -177,8 +124,8 @@
|
||||
//
|
||||
// btnDocGen
|
||||
//
|
||||
this.btnDocGen.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.btnDocGen.Location = new System.Drawing.Point(311, 485);
|
||||
this.btnDocGen.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
|
||||
this.btnDocGen.Location = new System.Drawing.Point(84, 485);
|
||||
this.btnDocGen.Name = "btnDocGen";
|
||||
this.btnDocGen.Size = new System.Drawing.Size(75, 23);
|
||||
this.btnDocGen.TabIndex = 14;
|
||||
@@ -231,7 +178,6 @@
|
||||
this.splitContainer1.Panel2.Controls.Add(this.lblTituloTabla);
|
||||
this.splitContainer1.Panel2.Controls.Add(this.btnDocGen);
|
||||
this.splitContainer1.Panel2.Controls.Add(this.lsvColumnas);
|
||||
this.splitContainer1.Panel2.Controls.Add(this.btnGenerar);
|
||||
this.splitContainer1.Panel2.Controls.Add(this.btnVerDatos);
|
||||
this.splitContainer1.Size = new System.Drawing.Size(806, 511);
|
||||
this.splitContainer1.SplitterDistance = 332;
|
||||
@@ -286,7 +232,7 @@
|
||||
// btnExportData
|
||||
//
|
||||
this.btnExportData.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
|
||||
this.btnExportData.Location = new System.Drawing.Point(84, 485);
|
||||
this.btnExportData.Location = new System.Drawing.Point(3, 485);
|
||||
this.btnExportData.Name = "btnExportData";
|
||||
this.btnExportData.Size = new System.Drawing.Size(75, 23);
|
||||
this.btnExportData.TabIndex = 15;
|
||||
@@ -357,10 +303,9 @@
|
||||
this.Name = "FrmBaseDatos";
|
||||
this.Text = "Base de Datos";
|
||||
this.Load += new System.EventHandler(this.frmBaseDatos_Load);
|
||||
this.menuBaseDatos.ResumeLayout(false);
|
||||
this.menuBaseDatos.PerformLayout();
|
||||
this.splitContainer1.Panel1.ResumeLayout(false);
|
||||
this.splitContainer1.Panel2.ResumeLayout(false);
|
||||
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
|
||||
this.splitContainer1.ResumeLayout(false);
|
||||
this.ResumeLayout(false);
|
||||
this.PerformLayout();
|
||||
@@ -380,12 +325,7 @@
|
||||
private System.Windows.Forms.ColumnHeader colNombreColumna;
|
||||
private System.Windows.Forms.ColumnHeader colTipoDatos;
|
||||
private System.Windows.Forms.MenuStrip menuBaseDatos;
|
||||
private System.Windows.Forms.ToolStripMenuItem archivoToolStripMenuItem;
|
||||
private System.Windows.Forms.ToolStripMenuItem menuCargar;
|
||||
private System.Windows.Forms.ToolStripMenuItem menuGuardar;
|
||||
private System.Windows.Forms.ToolStripMenuItem menuConfiguracion;
|
||||
private System.Windows.Forms.ColumnHeader colClave;
|
||||
private System.Windows.Forms.Button btnGenerar;
|
||||
private System.Windows.Forms.Button btnVerDatos;
|
||||
private System.Windows.Forms.Label lblTituloTabla;
|
||||
private System.Windows.Forms.Label lblTituloDB;
|
||||
|
||||
@@ -13,18 +13,14 @@ namespace VAR.DatabaseExplorer.UI
|
||||
{
|
||||
public partial class FrmBaseDatos : Form
|
||||
{
|
||||
private SqlConnection _cnx;
|
||||
private Config _config;
|
||||
private string _connectionString;
|
||||
|
||||
private string _tableSchema;
|
||||
private string _tableName;
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
// Establecer conexion
|
||||
txtConString.Text = _config.ConnectionString;
|
||||
_cnx = new SqlConnection(_config.ConnectionString);
|
||||
|
||||
List<Table> tables = TableDA.GetAllTables(_config.ConnectionString);
|
||||
List<Table> tables = TableBL.Table_GetAll(_connectionString);
|
||||
lsvTablas.Items.Clear();
|
||||
foreach (Table table in tables)
|
||||
{
|
||||
@@ -33,96 +29,21 @@ namespace VAR.DatabaseExplorer.UI
|
||||
item.SubItems.Add(table.Type);
|
||||
}
|
||||
|
||||
TablesToListView();
|
||||
lsvColumnas.Items.Clear();
|
||||
}
|
||||
|
||||
/// <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
|
||||
{
|
||||
Esquema = item.SubItems[0].Text,
|
||||
Nombre = item.SubItems[1].Text
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Metodo para seleccionar las tablas en le listview desde la configuracion
|
||||
/// </summary>
|
||||
private void TablesToListView()
|
||||
{
|
||||
int j;
|
||||
|
||||
// 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
|
||||
int n = _config.Tablas.Count;
|
||||
int i = j = 0;
|
||||
foreach (ListViewItem item in lsvTablas.Items)
|
||||
{
|
||||
item.Checked = false;
|
||||
do
|
||||
{
|
||||
if (string.Compare(_config.Tablas[i].Esquema, item.SubItems[0].Text, StringComparison.Ordinal) == 0 &&
|
||||
string.Compare(_config.Tablas[i].Nombre, item.SubItems[1].Text, StringComparison.Ordinal) == 0)
|
||||
{
|
||||
item.Checked = true;
|
||||
break;
|
||||
}
|
||||
i = (i + 1) % n;
|
||||
} while (i != j);
|
||||
j = i;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public DatabaseDesc CrearDatabaseDesc()
|
||||
{
|
||||
var db = new DatabaseDesc { Nombre = _cnx.Database };
|
||||
foreach (TablaInfo t in _config.Tablas)
|
||||
{
|
||||
var td = new TablaDesc();
|
||||
td.FillDesc(t.Esquema, t.Nombre, _cnx);
|
||||
db.Tablas.Add(td);
|
||||
}
|
||||
return (db);
|
||||
}
|
||||
|
||||
public FrmBaseDatos(String connectionString)
|
||||
public FrmBaseDatos(string connectionString)
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
_config = new Config {ConnectionString = connectionString};
|
||||
}
|
||||
|
||||
public FrmBaseDatos(Config config)
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
_config = config;
|
||||
_connectionString = connectionString;
|
||||
txtConString.Text = connectionString;
|
||||
}
|
||||
|
||||
private void frmBaseDatos_Load(object sender, EventArgs e)
|
||||
{
|
||||
Initialize();
|
||||
|
||||
lblTituloDB.Text = _cnx.Database;
|
||||
Database database = DatabaseBL.Database_Get(_connectionString);
|
||||
lblTituloDB.Text = database.Name;
|
||||
lblTituloTabla.Text = "";
|
||||
}
|
||||
|
||||
@@ -141,7 +62,7 @@ namespace VAR.DatabaseExplorer.UI
|
||||
|
||||
lblTituloTabla.Text = _tableSchema + @"." + _tableName;
|
||||
|
||||
List<Column> columns = ColumnDA.GetColumns(_config.ConnectionString, _tableSchema, _tableName);
|
||||
List<Column> columns = ColumnDA.GetColumns(_connectionString, _tableSchema, _tableName);
|
||||
lsvColumnas.Items.Clear();
|
||||
foreach (Column col in columns)
|
||||
{
|
||||
@@ -153,111 +74,39 @@ namespace VAR.DatabaseExplorer.UI
|
||||
}
|
||||
}
|
||||
|
||||
private void menuCargar_Click(object sender, EventArgs e)
|
||||
{
|
||||
var dialogo = new OpenFileDialog();
|
||||
if (dialogo.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
_config = Config.Cargar(dialogo.FileName);
|
||||
Initialize();
|
||||
}
|
||||
}
|
||||
|
||||
private void menuGuardar_Click(object sender, EventArgs e)
|
||||
{
|
||||
var dialogo = new SaveFileDialog();
|
||||
if (dialogo.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
TablesFromListView();
|
||||
_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)
|
||||
{
|
||||
if (string.IsNullOrEmpty(_tableSchema) || string.IsNullOrEmpty(_tableName)) { return; }
|
||||
|
||||
// Obtener descripcion de tabla
|
||||
var td = new TablaDesc();
|
||||
td.FillDesc(_tableSchema, _tableName, _cnx);
|
||||
|
||||
// Crear y mostrar el formulario de los datos.
|
||||
var form = new FrmDatos(td, _config.ConnectionString);
|
||||
var form = new FrmDatos(_connectionString, _tableSchema, _tableName);
|
||||
FrmPrincipal.AddForm(form);
|
||||
}
|
||||
|
||||
|
||||
private void btnProcs_Click(object sender, EventArgs e)
|
||||
{
|
||||
// Crear y mostrar el formulario de los procedimientos.
|
||||
var form = new FrmProcedimientos(_config.ConnectionString);
|
||||
var form = new FrmProcedimientos(_connectionString);
|
||||
FrmPrincipal.AddForm(form);
|
||||
}
|
||||
|
||||
private void btnExec_Click(object sender, EventArgs e)
|
||||
{
|
||||
// Crear y mostrar el formulario de exec.
|
||||
var form = new FrmExec(_config.ConnectionString);
|
||||
var form = new FrmExec(_connectionString);
|
||||
FrmPrincipal.AddForm(form);
|
||||
}
|
||||
|
||||
|
||||
private void menuBaseDatos_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void btnGenerar_Click(object sender, EventArgs e)
|
||||
{
|
||||
TablesFromListView();
|
||||
DatabaseDesc db = CrearDatabaseDesc();
|
||||
|
||||
var seriador = new XmlSerializer(typeof(DatabaseDesc));
|
||||
seriador.Serialize(Console.Out, db);
|
||||
}
|
||||
|
||||
private void btnDocGen_Click(object sender, EventArgs e)
|
||||
{
|
||||
// Hacer insensible la ventana
|
||||
Parent.Enabled = false;
|
||||
|
||||
// Obtener informacion.
|
||||
TablesFromListView();
|
||||
DatabaseDesc db = CrearDatabaseDesc();
|
||||
Database database = DatabaseBL.Database_GetSchema(connectionString: _connectionString, fillDefinitions: true);
|
||||
|
||||
// Escribir documentacion
|
||||
DocumentationGeneratorBL.GenerateDocumentation(db);
|
||||
string fixedDatabaseName = database.Name.Replace(' ', '_');
|
||||
var streamWriter = new StreamWriter(fixedDatabaseName + ".documentation.html");
|
||||
DatabaseBL.Database_GenerateDocumentation(streamWriter, database);
|
||||
streamWriter.Close();
|
||||
|
||||
// Hace sensible la ventana
|
||||
Parent.Enabled = true;
|
||||
|
||||
/*
|
||||
tablas_delistview();
|
||||
Thread hilo=new Thread(new ThreadStart(instancia.GenerarDoc));
|
||||
hilo.Start();
|
||||
*/
|
||||
}
|
||||
|
||||
public void GenerarDoc()
|
||||
{
|
||||
// Hacer insensible la ventana
|
||||
Enabled = false;
|
||||
|
||||
// Obtener informacion.
|
||||
DatabaseDesc db = CrearDatabaseDesc();
|
||||
|
||||
// Escribir documentacion
|
||||
DocumentationGeneratorBL.GenerateDocumentation(db);
|
||||
|
||||
// Hace sensible la ventana
|
||||
Enabled = true;
|
||||
}
|
||||
|
||||
private void btnRefresh_Click(object sender, EventArgs e)
|
||||
@@ -267,67 +116,14 @@ namespace VAR.DatabaseExplorer.UI
|
||||
|
||||
private void btnExportData_Click(object sender, EventArgs e)
|
||||
{
|
||||
// Hacer insensible la ventana
|
||||
Parent.Enabled = false;
|
||||
|
||||
// Obtener informacion.
|
||||
TablesFromListView();
|
||||
DatabaseDesc db = CrearDatabaseDesc();
|
||||
Database database = DatabaseBL.Database_GetSchema(connectionString: _connectionString, fillDefinitions: false);
|
||||
string fixedDatabaseName = database.Name.Replace(' ', '_');
|
||||
var streamWriter = new StreamWriter(fixedDatabaseName + ".data.sql");
|
||||
DatabaseBL.Database_ExportData(streamWriter, _connectionString, database);
|
||||
streamWriter.Close();
|
||||
|
||||
// Preparar cabecera del script
|
||||
var listCmds = new List<string>();
|
||||
listCmds.Add("SET NOCOUNT ON;");
|
||||
listCmds.Add(string.Empty);
|
||||
|
||||
//// Comandos de desactivacion de FKs
|
||||
//foreach (TablaDesc t in db.Tablas)
|
||||
//{
|
||||
// string tableName = string.Format("{0}.{1}", t.Esquema, t.Nombre);
|
||||
// listCmds.Add(string.Format("ALTER TABLE {0} NOCHECK CONSTRAINT all;", tableName));
|
||||
//}
|
||||
//listCmds.Add("GO");
|
||||
//listCmds.Add(string.Empty);
|
||||
|
||||
// Desactivar todas las FKs
|
||||
listCmds.Add("-- Disable all constraints");
|
||||
listCmds.Add("EXEC sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT all'");
|
||||
listCmds.Add("GO");
|
||||
listCmds.Add(string.Empty);
|
||||
|
||||
// Prepara informacion a exportar
|
||||
foreach (TablaDesc t in db.Tablas)
|
||||
{
|
||||
string tableName = string.Format("{0}.{1}", t.Esquema, t.Nombre);
|
||||
listCmds.Add(string.Format("PRINT '*** Importing data of {0}....';", tableName));
|
||||
List<string> listCmdsTableData = TableBL.ExportData(_config.ConnectionString, tableName);
|
||||
listCmds.AddRange(listCmdsTableData);
|
||||
}
|
||||
|
||||
//// Comandos de activacion de FKs
|
||||
//foreach (TablaDesc t in db.Tablas)
|
||||
//{
|
||||
// string tableName = string.Format("{0}.{1}", t.Esquema, t.Nombre);
|
||||
// listCmds.Add(string.Format("ALTER TABLE {0} WITH CHECK CHECK CONSTRAINT all;", tableName));
|
||||
//}
|
||||
//listCmds.Add("GO");
|
||||
//listCmds.Add(string.Empty);
|
||||
|
||||
// Activar todas las FKs
|
||||
listCmds.Add("-- Enable all constraints");
|
||||
listCmds.Add("EXEC sp_MSforeachtable @command1='print ''?''', @command2='ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all'");
|
||||
listCmds.Add("GO");
|
||||
listCmds.Add(string.Empty);
|
||||
|
||||
// Escribir script
|
||||
string fixedDatabaseName = db.Nombre.Replace(' ', '_');
|
||||
var escritor = new StreamWriter(fixedDatabaseName + ".data.sql");
|
||||
foreach (string cmd in listCmds)
|
||||
{
|
||||
escritor.WriteLine(cmd);
|
||||
}
|
||||
escritor.Close();
|
||||
|
||||
// Hace sensible la ventana
|
||||
Parent.Enabled = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -112,15 +112,15 @@
|
||||
<value>2.0</value>
|
||||
</resheader>
|
||||
<resheader name="reader">
|
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<metadata name="menuBaseDatos.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<metadata name="menuBaseDatos.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>17, 17</value>
|
||||
</metadata>
|
||||
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
|
||||
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
|
||||
<value>41</value>
|
||||
</metadata>
|
||||
</root>
|
||||
@@ -11,17 +11,20 @@ namespace VAR.DatabaseExplorer.UI
|
||||
{
|
||||
#region Declarations
|
||||
|
||||
private readonly TablaDesc _tablaDesc;
|
||||
private readonly string _conexionString = string.Empty;
|
||||
private readonly string _tableSchema = string.Empty;
|
||||
private readonly string _tableName = string.Empty;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Form life cycle
|
||||
|
||||
public FrmDatos(TablaDesc tablaDesc, string conexionString)
|
||||
public FrmDatos(string conexionString, string tableSchema, string tableName)
|
||||
{
|
||||
InitializeComponent();
|
||||
_tablaDesc = tablaDesc;
|
||||
_conexionString = conexionString;
|
||||
_tableSchema = tableSchema;
|
||||
_tableName = tableName;
|
||||
_conexionString = conexionString;
|
||||
LoadDataFromTable();
|
||||
}
|
||||
@@ -56,8 +59,8 @@ namespace VAR.DatabaseExplorer.UI
|
||||
|
||||
private void LoadDataFromTable()
|
||||
{
|
||||
Text = _tablaDesc.Esquema + @"." + _tablaDesc.Nombre;
|
||||
string tableFullName = "[" + _tablaDesc.Esquema + "].[" + _tablaDesc.Nombre + "]";
|
||||
Text = _tableSchema + @"." + _tableName;
|
||||
string tableFullName = "[" + _tableSchema + "].[" + _tableName + "]";
|
||||
var dt= new DataTable();
|
||||
var cnx = new SqlConnection(_conexionString);
|
||||
var da = new SqlDataAdapter( "select * from " + tableFullName, cnx);
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Windows.Forms;
|
||||
using VAR.DatabaseExplorer.Code;
|
||||
|
||||
@@ -48,12 +49,13 @@ namespace VAR.DatabaseExplorer.UI
|
||||
{
|
||||
DataTable dataTable = Exec();
|
||||
|
||||
List<string> listCmds = DataTableHelper.DataTable_GenerateInserts(dataTable, txtDestTable.Text);
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
DataTableHelper.DataTable_GenerateInserts(stringWriter, dataTable, txtDestTable.Text);
|
||||
|
||||
// Preparar la datatable destino
|
||||
var destDataTable = new DataTable();
|
||||
destDataTable.Columns.Add("Cmd", typeof(String));
|
||||
foreach (string cmd in listCmds)
|
||||
foreach (string cmd in stringWriter.GetStringBuilder().ToString().Split('\n'))
|
||||
{
|
||||
destDataTable.Rows.Add(new object[] { cmd });
|
||||
}
|
||||
|
||||
32
VAR.DatabaseExplorer/UI/FrmPrincipal.Designer.cs
generated
32
VAR.DatabaseExplorer/UI/FrmPrincipal.Designer.cs
generated
@@ -31,9 +31,6 @@
|
||||
this.menuPrincipal = new System.Windows.Forms.MenuStrip();
|
||||
this.menuServidor = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.menuBuscarServidor = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.menuBaseDatos = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.menuConectarA = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.menuConectarPRUEBAS = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.flowpnlWindows = new System.Windows.Forms.FlowLayoutPanel();
|
||||
this.menuPrincipal.SuspendLayout();
|
||||
this.SuspendLayout();
|
||||
@@ -41,8 +38,7 @@
|
||||
// menuPrincipal
|
||||
//
|
||||
this.menuPrincipal.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
|
||||
this.menuServidor,
|
||||
this.menuBaseDatos});
|
||||
this.menuServidor});
|
||||
this.menuPrincipal.Location = new System.Drawing.Point(0, 0);
|
||||
this.menuPrincipal.Name = "menuPrincipal";
|
||||
this.menuPrincipal.Size = new System.Drawing.Size(800, 24);
|
||||
@@ -64,29 +60,6 @@
|
||||
this.menuBuscarServidor.Text = "Buscar Servidor";
|
||||
this.menuBuscarServidor.Click += new System.EventHandler(this.menuBuscarServidor_Click);
|
||||
//
|
||||
// menuBaseDatos
|
||||
//
|
||||
this.menuBaseDatos.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
|
||||
this.menuConectarA,
|
||||
this.menuConectarPRUEBAS});
|
||||
this.menuBaseDatos.Name = "menuBaseDatos";
|
||||
this.menuBaseDatos.Size = new System.Drawing.Size(92, 20);
|
||||
this.menuBaseDatos.Text = "Base de Datos";
|
||||
//
|
||||
// menuConectarA
|
||||
//
|
||||
this.menuConectarA.Name = "menuConectarA";
|
||||
this.menuConectarA.Size = new System.Drawing.Size(183, 22);
|
||||
this.menuConectarA.Text = "Conectar a...";
|
||||
this.menuConectarA.Click += new System.EventHandler(this.menuConectarA_Click);
|
||||
//
|
||||
// menuConectarPRUEBAS
|
||||
//
|
||||
this.menuConectarPRUEBAS.Name = "menuConectarPRUEBAS";
|
||||
this.menuConectarPRUEBAS.Size = new System.Drawing.Size(183, 22);
|
||||
this.menuConectarPRUEBAS.Text = "Conectar a PRUEBAS";
|
||||
this.menuConectarPRUEBAS.Click += new System.EventHandler(this.menuConectarPRUEBAS_Click);
|
||||
//
|
||||
// flowpnlWindows
|
||||
//
|
||||
this.flowpnlWindows.AutoSize = true;
|
||||
@@ -122,9 +95,6 @@
|
||||
private System.Windows.Forms.MenuStrip menuPrincipal;
|
||||
private System.Windows.Forms.ToolStripMenuItem menuServidor;
|
||||
private System.Windows.Forms.ToolStripMenuItem menuBuscarServidor;
|
||||
private System.Windows.Forms.ToolStripMenuItem menuBaseDatos;
|
||||
private System.Windows.Forms.ToolStripMenuItem menuConectarA;
|
||||
private System.Windows.Forms.ToolStripMenuItem menuConectarPRUEBAS;
|
||||
private System.Windows.Forms.FlowLayoutPanel flowpnlWindows;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows.Forms;
|
||||
using VAR.DatabaseExplorer.Code;
|
||||
using VAR.DatabaseExplorer.Controls;
|
||||
|
||||
namespace VAR.DatabaseExplorer.UI
|
||||
@@ -82,35 +81,12 @@ namespace VAR.DatabaseExplorer.UI
|
||||
|
||||
#region Menus
|
||||
|
||||
private void menuConectarPRUEBAS_Click(object sender, EventArgs e)
|
||||
{
|
||||
// Crear ventana de la base de datos de pruebas
|
||||
var frm = new FrmBaseDatos("Data Source=localhost;Initial Catalog=Tests;Integrated Security=True");
|
||||
//frmBaseDatos frm = new frmBaseDatos("Data Source=DANTE;Initial Catalog=BD_AlfonsoRodriguez;Integrated Security=True");
|
||||
//frmBaseDatos frm = new frmBaseDatos("Data Source=OSKURITO;Initial Catalog=BD_AlfonsoRodriguez;Integrated Security=True");
|
||||
FrmPrincipal.AddForm(frm);
|
||||
}
|
||||
|
||||
private void menuBuscarServidor_Click(object sender, EventArgs e)
|
||||
{
|
||||
// Mostrar ventana de buscador de servidores
|
||||
var frm = new FrmServidores();
|
||||
FrmPrincipal.AddForm(frm);
|
||||
}
|
||||
|
||||
private void menuConectarA_Click(object sender, EventArgs e)
|
||||
{
|
||||
// Cargar configuracion
|
||||
var dialogo = new OpenFileDialog();
|
||||
if (dialogo.ShowDialog() != DialogResult.OK) return;
|
||||
|
||||
Config config = Config.Cargar(dialogo.FileName);
|
||||
|
||||
// Crear y mostrar ventana
|
||||
var frm = new FrmBaseDatos(config);
|
||||
FrmPrincipal.AddForm(frm);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,15 +33,16 @@ namespace VAR.DatabaseExplorer.UI
|
||||
if (lsvServidores.SelectedItems.Count > 0)
|
||||
{
|
||||
ListViewItem item = lsvServidores.SelectedItems[0];
|
||||
txtServidor.Text = String.IsNullOrEmpty(item.SubItems[1].Text)
|
||||
txtServidor.Text = string.IsNullOrEmpty(item.SubItems[1].Text)
|
||||
? item.SubItems[0].Text
|
||||
: String.Format("{0}/{1}", item.SubItems[0].Text, item.SubItems[1].Text);
|
||||
: string.Format("{0}/{1}", item.SubItems[0].Text, item.SubItems[1].Text);
|
||||
}
|
||||
}
|
||||
|
||||
private void btnListarBBDD_Click(object sender, EventArgs e)
|
||||
{
|
||||
List<Database> databases = DatabaseDA.GetAllDatabases(BuildConnectionString());
|
||||
string connectionString = BuildConnectionString(txtServidor.Text, null, txtUsuario.Text, TxtContrasenha.Text);
|
||||
List<Database> databases = DatabaseDA.GetAllDatabases(connectionString);
|
||||
|
||||
lsvBBDD.Items.Clear();
|
||||
foreach (Database database in databases)
|
||||
@@ -55,29 +56,29 @@ namespace VAR.DatabaseExplorer.UI
|
||||
private void lsvBBDD_DoubleClick(object sender, EventArgs e)
|
||||
{
|
||||
if (lsvBBDD.SelectedItems.Count != 1) return;
|
||||
|
||||
string databaseName = lsvBBDD.SelectedItems[0].SubItems[0].Text;
|
||||
// Llamar a la venta de la base de datos
|
||||
var frm = new FrmBaseDatos(BuildConnectionString());
|
||||
string connectionStirng = BuildConnectionString(txtServidor.Text, databaseName, txtUsuario.Text, TxtContrasenha.Text);
|
||||
var frm = new FrmBaseDatos(connectionStirng);
|
||||
FrmPrincipal.AddForm(frm);
|
||||
}
|
||||
|
||||
private string BuildConnectionString()
|
||||
private string BuildConnectionString(string server, string database, string user, string password)
|
||||
{
|
||||
// Construir cadena de conexion
|
||||
var constructor = new SqlConnectionStringBuilder();
|
||||
constructor.DataSource = (!string.IsNullOrEmpty(txtServidor.Text)) ? txtServidor.Text : "localhost";
|
||||
if (lsvBBDD.SelectedItems.Count > 0)
|
||||
constructor.DataSource = string.IsNullOrEmpty(server) ? "localhost" : server;
|
||||
if (string.IsNullOrEmpty(database) == false)
|
||||
{
|
||||
constructor.InitialCatalog = lsvBBDD.SelectedItems[0].SubItems[0].Text;
|
||||
constructor.InitialCatalog = database;
|
||||
}
|
||||
if (String.IsNullOrEmpty(txtUsuario.Text))
|
||||
if (string.IsNullOrEmpty(user))
|
||||
{
|
||||
constructor.IntegratedSecurity = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
constructor.UserID = txtUsuario.Text;
|
||||
constructor.Password = TxtContrasenha.Text;
|
||||
constructor.UserID = user;
|
||||
constructor.Password = password;
|
||||
}
|
||||
return constructor.ConnectionString;
|
||||
}
|
||||
|
||||
@@ -81,8 +81,8 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Code\BusinessLogic\DatabaseBL.cs" />
|
||||
<Compile Include="Code\BusinessLogic\ProcedureBL.cs" />
|
||||
<Compile Include="Code\BusinessLogic\TableBL.cs" />
|
||||
<Compile Include="Code\Config.cs" />
|
||||
<Compile Include="Code\DataAccess\ColumnDA.cs" />
|
||||
<Compile Include="Code\DataAccess\DatabaseDA.cs" />
|
||||
<Compile Include="Code\DataAccess\ProcedureDA.cs" />
|
||||
@@ -97,8 +97,6 @@
|
||||
<Compile Include="Controls\CustomListView.cs">
|
||||
<SubType>Component</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Code\DatabaseDesc.cs" />
|
||||
<Compile Include="Code\BusinessLogic\DocumentationGeneratorBL.cs" />
|
||||
<Compile Include="Controls\CustomTextBox.cs">
|
||||
<SubType>Component</SubType>
|
||||
</Compile>
|
||||
@@ -143,7 +141,6 @@
|
||||
</Compile>
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Code\TablaDesc.cs" />
|
||||
<Compile Include="Code\DataTableHelper.cs" />
|
||||
<EmbeddedResource Include="UI\FrmBaseDatos.resx">
|
||||
<DependentUpon>FrmBaseDatos.cs</DependentUpon>
|
||||
|
||||
Reference in New Issue
Block a user