Create new BL and DA objects to abstract data acquisition and processing.
This commit is contained in:
12
VAR.DatabaseExplorer/Code/BusinessLogic/DatabaseBL.cs
Normal file
12
VAR.DatabaseExplorer/Code/BusinessLogic/DatabaseBL.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace VAR.DatabaseExplorer.Code.BusinessLogic
|
||||
{
|
||||
public class DatabaseBL
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
using System.IO;
|
||||
|
||||
namespace VAR.DatabaseExplorer.Code
|
||||
namespace VAR.DatabaseExplorer.Code.BusinessLogic
|
||||
{
|
||||
public class DocGen
|
||||
public class DocumentationGeneratorBL
|
||||
{
|
||||
public static void GenerarDocumentacion(DatabaseDesc database)
|
||||
public static void GenerateDocumentation(DatabaseDesc database)
|
||||
{
|
||||
// Abrir el documento que contendra la documentacion
|
||||
string fixedDatabaseName = database.Nombre.Replace(' ', '_');
|
||||
28
VAR.DatabaseExplorer/Code/BusinessLogic/TableBL.cs
Normal file
28
VAR.DatabaseExplorer/Code/BusinessLogic/TableBL.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using VAR.DatabaseExplorer.Code.DataAccess;
|
||||
|
||||
namespace VAR.DatabaseExplorer.Code.BusinessLogic
|
||||
{
|
||||
public class TableBL
|
||||
{
|
||||
public static List<string> ExportData(string conectionString, string tableName)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
79
VAR.DatabaseExplorer/Code/DataAccess/ColumnDA.cs
Normal file
79
VAR.DatabaseExplorer/Code/DataAccess/ColumnDA.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using VAR.DatabaseExplorer.Code.DataTransfer;
|
||||
|
||||
namespace VAR.DatabaseExplorer.Code.DataAccess
|
||||
{
|
||||
public class ColumnDA
|
||||
{
|
||||
public static List<Column> GetColumns(string conexionString, string tableSchema, string tableName)
|
||||
{
|
||||
var columns = new List<Column>();
|
||||
var cnx = new SqlConnection(conexionString);
|
||||
|
||||
// Preparar comando y parametros
|
||||
var da = new SqlDataAdapter(@"
|
||||
SELECT col.COLUMN_NAME AS ColumnName,
|
||||
col.DATA_TYPE AS Type,
|
||||
col.CHARACTER_MAXIMUM_LENGTH AS Size,
|
||||
c.CONSTRAINT_TYPE AS KeyType,
|
||||
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=@TableName AND
|
||||
col.TABLE_SCHEMA=@TableSchema
|
||||
ORDER BY col.ORDINAL_POSITION
|
||||
", cnx);
|
||||
var prm = new SqlParameter("@TableName", SqlDbType.VarChar, 100) { Value = tableName };
|
||||
da.SelectCommand.Parameters.Add(prm);
|
||||
prm = new SqlParameter("@TableSchema", SqlDbType.VarChar, 100) { Value = tableSchema };
|
||||
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
|
||||
foreach (DataRow dr in dt.Rows)
|
||||
{
|
||||
string columnName = Convert.ToString(dr["ColumnName"]);
|
||||
Column col = columns.FirstOrDefault(c => c.Name == columnName);
|
||||
if(col == null)
|
||||
{
|
||||
col = new Column();
|
||||
columns.Add(col);
|
||||
}
|
||||
|
||||
col.Name = columnName;
|
||||
|
||||
col.Type = Convert.ToString(dr["Type"]);
|
||||
|
||||
if ((dr["Size"] is DBNull) == false)
|
||||
{
|
||||
col.Size = Convert.ToInt32(dr["Size"]);
|
||||
}
|
||||
|
||||
string strNullable = (Convert.ToString(dr["Nullable"])).ToLower();
|
||||
col.Nullable = (strNullable == "yes") || (strNullable == "1") || (strNullable == "true");
|
||||
|
||||
string KeyType = Convert.ToString(dr["KeyType"]).ToLower();
|
||||
if (KeyType.Contains("primary"))
|
||||
{
|
||||
col.PK = true;
|
||||
}
|
||||
}
|
||||
|
||||
return columns;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
|
||||
{
|
||||
public class DatabaseDA
|
||||
{
|
||||
public static List<Database> Database_GetRegs(string conexionString)
|
||||
public static List<Database> GetAllDatabases(string conexionString)
|
||||
{
|
||||
var databases = new List<Database>();
|
||||
var cnx = new SqlConnection(conexionString);
|
||||
@@ -23,7 +23,7 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
|
||||
{
|
||||
databases.Add(new Database
|
||||
{
|
||||
Name = (String) dr["database_name"],
|
||||
Name = (string) dr["database_name"],
|
||||
CreateDate = (DateTime) dr["create_date"]
|
||||
});
|
||||
}
|
||||
|
||||
84
VAR.DatabaseExplorer/Code/DataAccess/ProcedureDA.cs
Normal file
84
VAR.DatabaseExplorer/Code/DataAccess/ProcedureDA.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using System;
|
||||
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)
|
||||
{
|
||||
var procedures = new List<Procedure>();
|
||||
var cnx = new SqlConnection(conexionString);
|
||||
|
||||
var da = new SqlDataAdapter(
|
||||
"SELECT ROUTINE_NAME Name, ROUTINE_SCHEMA [Schema], CREATED CreateDate, ROUTINE_TYPE [Type] FROM INFORMATION_SCHEMA.ROUTINES",
|
||||
cnx);
|
||||
var dt = new DataTable();
|
||||
cnx.Open();
|
||||
da.Fill(dt);
|
||||
cnx.Close();
|
||||
|
||||
foreach (DataRow dr in dt.Rows)
|
||||
{
|
||||
procedures.Add(new Procedure
|
||||
{
|
||||
Schema = Convert.ToString(dr["Schema"]),
|
||||
Name = Convert.ToString(dr["Name"]),
|
||||
Type = Convert.ToString(dr["Type"]),
|
||||
CreateDate = Convert.ToDateTime(dr["CreateDate"]),
|
||||
});
|
||||
}
|
||||
return procedures;
|
||||
}
|
||||
|
||||
public static string GetProcedureDefinition(string conexionString, string schema, string name)
|
||||
{
|
||||
SqlDataAdapter dataAdapter;
|
||||
var cnx = new SqlConnection(conexionString);
|
||||
cnx.Open();
|
||||
|
||||
if (cnx.ServerVersion.StartsWith("10.") || cnx.ServerVersion.StartsWith("11."))
|
||||
{
|
||||
dataAdapter = new SqlDataAdapter(@"
|
||||
SELECT
|
||||
ISNULL(smsp.definition, ssmsp.definition) AS [Definition]
|
||||
FROM sys.all_objects AS sp
|
||||
INNER JOIN sys.schemas sn ON sp.schema_id = sn.schema_id
|
||||
LEFT OUTER JOIN sys.sql_modules AS smsp ON smsp.object_id = sp.object_id
|
||||
LEFT OUTER JOIN sys.system_sql_modules AS ssmsp ON ssmsp.object_id = sp.object_id
|
||||
WHERE
|
||||
(sp.type = N'P' OR sp.type = N'RF' OR sp.type = N'PC' OR sp.type = N'IF' OR sp.type = N'FN' OR sp.type = N'TF')
|
||||
AND
|
||||
(sp.name = @name and sn.name = @schema)
|
||||
", cnx);
|
||||
dataAdapter.SelectCommand.Parameters.AddWithValue("@Name", name);
|
||||
dataAdapter.SelectCommand.Parameters.AddWithValue("@Schema", schema);
|
||||
}
|
||||
else
|
||||
{
|
||||
cnx.Close();
|
||||
return null;
|
||||
}
|
||||
|
||||
var dt = new DataTable();
|
||||
dataAdapter.Fill(dt);
|
||||
cnx.Close();
|
||||
|
||||
var sbProc = new StringBuilder();
|
||||
foreach (DataRow dr in dt.Rows)
|
||||
{
|
||||
string strProcBlock = Convert.ToString(dr["Definition"]);
|
||||
strProcBlock = strProcBlock.Replace("\r", "").Replace("\n", "\r\n");
|
||||
sbProc.Append(strProcBlock);
|
||||
}
|
||||
|
||||
return sbProc.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
|
||||
{
|
||||
public class ServerDA
|
||||
{
|
||||
public static List<Server> Server_GetRegs()
|
||||
public static List<Server> GetAllServers()
|
||||
{
|
||||
var servers = new List<Server>();
|
||||
SqlDataSourceEnumerator enumerador = SqlDataSourceEnumerator.Instance;
|
||||
@@ -21,9 +21,9 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
|
||||
{
|
||||
servers.Add(new Server
|
||||
{
|
||||
Name = (dr["ServerName"] == DBNull.Value) ? string.Empty : (String)dr["ServerName"],
|
||||
Instance = (dr["InstanceName"] == DBNull.Value) ? string.Empty : (String)dr["InstanceName"],
|
||||
Version = (dr["Version"] == DBNull.Value) ? "???" : (String)dr["Version"]
|
||||
Name = (dr["ServerName"] == DBNull.Value) ? string.Empty : (string)dr["ServerName"],
|
||||
Instance = (dr["InstanceName"] == DBNull.Value) ? string.Empty : (string)dr["InstanceName"],
|
||||
Version = (dr["Version"] == DBNull.Value) ? "???" : (string)dr["Version"]
|
||||
});
|
||||
}
|
||||
return servers;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using VAR.DatabaseExplorer.Code.DataTransfer;
|
||||
@@ -8,7 +7,7 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
|
||||
{
|
||||
public class TableDA
|
||||
{
|
||||
public static List<Table> Table_GetRegs(string conexionString)
|
||||
public static List<Table> GetAllTables(string conexionString)
|
||||
{
|
||||
var tables = new List<Table>();
|
||||
var cnx = new SqlConnection(conexionString);
|
||||
@@ -19,7 +18,6 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
|
||||
dt.DefaultView.Sort = "TABLE_SCHEMA ASC, TABLE_NAME ASC, TABLE_TYPE ASC";
|
||||
dt = dt.DefaultView.ToTable();
|
||||
|
||||
// Mostrar todas las tablas
|
||||
foreach (DataRow dr in dt.Rows)
|
||||
{
|
||||
tables.Add(new Table
|
||||
@@ -32,5 +30,19 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
|
||||
|
||||
return tables;
|
||||
}
|
||||
|
||||
public static DataTable GetData(string conexionString, string tableName)
|
||||
{
|
||||
var cnx = new SqlConnection(conexionString);
|
||||
string strCmd = string.Format("SELECT * FROM {0}", tableName);
|
||||
var da = new SqlDataAdapter(strCmd, cnx);
|
||||
var dt = new DataTable();
|
||||
cnx.Open();
|
||||
da.Fill(dt);
|
||||
cnx.Close();
|
||||
|
||||
return dt;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,39 +2,12 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace VAR.DatabaseExplorer.Code
|
||||
{
|
||||
public static class Utiles
|
||||
public static class DataTableHelper
|
||||
{
|
||||
#region File Utils
|
||||
|
||||
public static void CrearDirectorio(this String path)
|
||||
{
|
||||
DirectoryInfo info;
|
||||
try
|
||||
{
|
||||
info = new DirectoryInfo(path);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
info = new DirectoryInfo(".");
|
||||
}
|
||||
CrearDirectorio(info);
|
||||
}
|
||||
|
||||
public static void CrearDirectorio(this DirectoryInfo info)
|
||||
{
|
||||
if (info.Parent != null) CrearDirectorio(info.Parent);
|
||||
if (!info.Exists) info.Create();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region DataTable Utils
|
||||
|
||||
public static List<string> DataTable_GenerateInserts(DataTable dataTable, string destTable)
|
||||
{
|
||||
var nfi = new NumberFormatInfo { NumberDecimalSeparator = "." };
|
||||
@@ -108,13 +81,11 @@ namespace VAR.DatabaseExplorer.Code
|
||||
}
|
||||
|
||||
// Insertar fila a la datatable destino
|
||||
listCmds.Add(String.Format("INSERT INTO {0} ({1}) VALUES ({2});",
|
||||
listCmds.Add(string.Format("INSERT INTO {0} ({1}) VALUES ({2});",
|
||||
destTable, sbColumns.ToString(), sbValues.ToString()));
|
||||
}
|
||||
|
||||
return listCmds;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -9,10 +9,16 @@ namespace VAR.DatabaseExplorer.Code.DataTransfer
|
||||
{
|
||||
[XmlAttribute]
|
||||
public string Name { get; set; }
|
||||
|
||||
[XmlAttribute]
|
||||
public string Type { get; set; }
|
||||
|
||||
[XmlAttribute]
|
||||
public int Size { get; set; }
|
||||
|
||||
[XmlAttribute]
|
||||
public bool Nullable { get; set; }
|
||||
|
||||
[XmlAttribute]
|
||||
public bool PK { get; set; }
|
||||
}
|
||||
|
||||
@@ -10,16 +10,14 @@ namespace VAR.DatabaseExplorer.Code.DataTransfer
|
||||
{
|
||||
[XmlAttribute]
|
||||
public string Name { get; set; }
|
||||
|
||||
[XmlAttribute]
|
||||
public DateTime CreateDate { get; set; }
|
||||
|
||||
|
||||
private readonly List<Table> _tables = new List<Table>();
|
||||
[XmlArray]
|
||||
public List<Table> Tables { get; set; }
|
||||
|
||||
[XmlArray]
|
||||
public List<Table> Tables
|
||||
{
|
||||
get { return _tables; }
|
||||
}
|
||||
public List<Procedure> Procedures { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
24
VAR.DatabaseExplorer/Code/DataTransfer/Procedure.cs
Normal file
24
VAR.DatabaseExplorer/Code/DataTransfer/Procedure.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace VAR.DatabaseExplorer.Code.DataTransfer
|
||||
{
|
||||
[Serializable]
|
||||
public class Procedure
|
||||
{
|
||||
[XmlAttribute]
|
||||
public string Schema { get; set; }
|
||||
|
||||
[XmlAttribute]
|
||||
public string Name { get; set; }
|
||||
|
||||
[XmlAttribute]
|
||||
public string Type { get; set; }
|
||||
|
||||
[XmlAttribute]
|
||||
public DateTime CreateDate { get; set; }
|
||||
|
||||
[XmlElement]
|
||||
public string Definition { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -9,26 +9,17 @@ namespace VAR.DatabaseExplorer.Code.DataTransfer
|
||||
{
|
||||
[XmlAttribute]
|
||||
public string Name { get; set; }
|
||||
|
||||
[XmlAttribute]
|
||||
public string Instance { get; set; }
|
||||
|
||||
[XmlAttribute]
|
||||
public string Version { get; set; }
|
||||
|
||||
|
||||
private readonly List<User> _users = new List<User>();
|
||||
[XmlArray]
|
||||
public List<User> Users { get; set; }
|
||||
|
||||
[XmlArray]
|
||||
public List<User> Users
|
||||
{
|
||||
get { return _users; }
|
||||
}
|
||||
|
||||
private readonly List<Database> _databases = new List<Database>();
|
||||
|
||||
[XmlArray]
|
||||
public List<Database> Databases
|
||||
{
|
||||
get { return _databases; }
|
||||
}
|
||||
public List<Database> Databases { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,17 +10,14 @@ namespace VAR.DatabaseExplorer.Code.DataTransfer
|
||||
{
|
||||
[XmlAttribute]
|
||||
public string Schema { get; set; }
|
||||
|
||||
[XmlAttribute]
|
||||
public string Name { get; set; }
|
||||
|
||||
[XmlAttribute]
|
||||
public string Type { get; set; }
|
||||
|
||||
private readonly List<Column> _columns = new List<Column>();
|
||||
|
||||
[XmlArray]
|
||||
public List<Column> Columns
|
||||
{
|
||||
get { return _columns; }
|
||||
}
|
||||
public List<Column> Columns { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
7
VAR.DatabaseExplorer/UI/FrmBaseDatos.Designer.cs
generated
7
VAR.DatabaseExplorer/UI/FrmBaseDatos.Designer.cs
generated
@@ -52,7 +52,6 @@
|
||||
this.lsvColumnas = new VAR.DatabaseExplorer.Controls.CustomListView();
|
||||
this.colNombreColumna = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
|
||||
this.colTipoDatos = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
|
||||
this.colTamanho = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
|
||||
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();
|
||||
@@ -304,7 +303,6 @@
|
||||
this.lsvColumnas.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
|
||||
this.colNombreColumna,
|
||||
this.colTipoDatos,
|
||||
this.colTamanho,
|
||||
this.colClave,
|
||||
this.colNullable});
|
||||
this.lsvColumnas.FullRowSelect = true;
|
||||
@@ -325,10 +323,6 @@
|
||||
this.colTipoDatos.Text = "Tipo de Datos";
|
||||
this.colTipoDatos.Width = 81;
|
||||
//
|
||||
// colTamanho
|
||||
//
|
||||
this.colTamanho.Text = "Tamaño";
|
||||
//
|
||||
// colClave
|
||||
//
|
||||
this.colClave.Text = "Clave";
|
||||
@@ -385,7 +379,6 @@
|
||||
private VAR.DatabaseExplorer.Controls.CustomListView lsvColumnas;
|
||||
private System.Windows.Forms.ColumnHeader colNombreColumna;
|
||||
private System.Windows.Forms.ColumnHeader colTipoDatos;
|
||||
private System.Windows.Forms.ColumnHeader colTamanho;
|
||||
private System.Windows.Forms.MenuStrip menuBaseDatos;
|
||||
private System.Windows.Forms.ToolStripMenuItem archivoToolStripMenuItem;
|
||||
private System.Windows.Forms.ToolStripMenuItem menuCargar;
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Windows.Forms;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.IO;
|
||||
using System.Windows.Forms;
|
||||
using System.Xml.Serialization;
|
||||
using VAR.DatabaseExplorer.Code;
|
||||
using System.Collections.Generic;
|
||||
using VAR.DatabaseExplorer.Code.DataTransfer;
|
||||
using VAR.DatabaseExplorer.Code.BusinessLogic;
|
||||
using VAR.DatabaseExplorer.Code.DataAccess;
|
||||
using System.IO;
|
||||
using VAR.DatabaseExplorer.Code.DataTransfer;
|
||||
|
||||
namespace VAR.DatabaseExplorer.UI
|
||||
{
|
||||
@@ -15,8 +15,8 @@ namespace VAR.DatabaseExplorer.UI
|
||||
{
|
||||
private SqlConnection _cnx;
|
||||
private Config _config;
|
||||
private String _tableSchema;
|
||||
private String _tableName;
|
||||
private string _tableSchema;
|
||||
private string _tableName;
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
@@ -24,7 +24,7 @@ namespace VAR.DatabaseExplorer.UI
|
||||
txtConString.Text = _config.ConnectionString;
|
||||
_cnx = new SqlConnection(_config.ConnectionString);
|
||||
|
||||
List<Table> tables = TableDA.Table_GetRegs(_config.ConnectionString);
|
||||
List<Table> tables = TableDA.GetAllTables(_config.ConnectionString);
|
||||
lsvTablas.Items.Clear();
|
||||
foreach (Table table in tables)
|
||||
{
|
||||
@@ -79,8 +79,8 @@ namespace VAR.DatabaseExplorer.UI
|
||||
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)
|
||||
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;
|
||||
@@ -122,14 +122,12 @@ namespace VAR.DatabaseExplorer.UI
|
||||
{
|
||||
Initialize();
|
||||
|
||||
// Establecer titulos
|
||||
lblTituloDB.Text = _cnx.Database;
|
||||
lblTituloTabla.Text = "";
|
||||
}
|
||||
|
||||
private void btnCopiarConString_Click(object sender, EventArgs e)
|
||||
{
|
||||
// Copiar la cadena de conexion al portapapeles
|
||||
Clipboard.SetText(txtConString.Text);
|
||||
}
|
||||
|
||||
@@ -137,28 +135,20 @@ namespace VAR.DatabaseExplorer.UI
|
||||
{
|
||||
if (lsvTablas.SelectedItems.Count != 1) { return; }
|
||||
|
||||
// Determinar tabla seleccionada
|
||||
ListViewItem item = lsvTablas.SelectedItems[0];
|
||||
|
||||
// Recordar tabla seleccionada
|
||||
_tableSchema = item.SubItems[0].Text;
|
||||
_tableName = item.SubItems[1].Text;
|
||||
|
||||
// Establecer titulo de la lista de columnas
|
||||
lblTituloTabla.Text = _tableSchema + @"." + _tableName;
|
||||
|
||||
// Obtener descripcion de tabla
|
||||
var td = new TablaDesc();
|
||||
td.FillDesc(_tableSchema, _tableName, _cnx);
|
||||
|
||||
// Mostrar "columnas" de las tablas
|
||||
List<Column> columns = ColumnDA.GetColumns(_config.ConnectionString, _tableSchema, _tableName);
|
||||
lsvColumnas.Items.Clear();
|
||||
foreach (ColumnaDesc col in td.Columnas)
|
||||
foreach (Column col in columns)
|
||||
{
|
||||
ListViewItem subitem = lsvColumnas.Items.Add(col.Nombre);
|
||||
subitem.SubItems.Add(col.Tipo);
|
||||
subitem.SubItems.Add((col.Tamanho >= 0) ? String.Format("{0}", col.Tamanho) : string.Empty);
|
||||
subitem.SubItems.Add(col.Primaria ? "PK" : string.Empty);
|
||||
ListViewItem subitem = lsvColumnas.Items.Add(col.Name);
|
||||
string type = string.Format("{0}{1}", col.Type, col.Size > 0 ? string.Format("({0})", col.Size) : string.Empty);
|
||||
subitem.SubItems.Add(type);
|
||||
subitem.SubItems.Add(col.PK ? "PK" : string.Empty);
|
||||
subitem.SubItems.Add(col.Nullable ? "Null" : "NotNull");
|
||||
}
|
||||
}
|
||||
@@ -231,8 +221,6 @@ namespace VAR.DatabaseExplorer.UI
|
||||
|
||||
var seriador = new XmlSerializer(typeof(DatabaseDesc));
|
||||
seriador.Serialize(Console.Out, db);
|
||||
|
||||
//CodeGen.GenerarCodigo(config, db);
|
||||
}
|
||||
|
||||
private void btnDocGen_Click(object sender, EventArgs e)
|
||||
@@ -245,7 +233,7 @@ namespace VAR.DatabaseExplorer.UI
|
||||
DatabaseDesc db = CrearDatabaseDesc();
|
||||
|
||||
// Escribir documentacion
|
||||
DocGen.GenerarDocumentacion(db);
|
||||
DocumentationGeneratorBL.GenerateDocumentation(db);
|
||||
|
||||
// Hace sensible la ventana
|
||||
Parent.Enabled = true;
|
||||
@@ -266,7 +254,7 @@ namespace VAR.DatabaseExplorer.UI
|
||||
DatabaseDesc db = CrearDatabaseDesc();
|
||||
|
||||
// Escribir documentacion
|
||||
DocGen.GenerarDocumentacion(db);
|
||||
DocumentationGeneratorBL.GenerateDocumentation(db);
|
||||
|
||||
// Hace sensible la ventana
|
||||
Enabled = true;
|
||||
@@ -311,7 +299,7 @@ namespace VAR.DatabaseExplorer.UI
|
||||
{
|
||||
string tableName = string.Format("{0}.{1}", t.Esquema, t.Nombre);
|
||||
listCmds.Add(string.Format("PRINT '*** Importing data of {0}....';", tableName));
|
||||
List<string> listCmdsTableData = ExportData(tableName);
|
||||
List<string> listCmdsTableData = TableBL.ExportData(_config.ConnectionString, tableName);
|
||||
listCmds.AddRange(listCmdsTableData);
|
||||
}
|
||||
|
||||
@@ -343,37 +331,5 @@ namespace VAR.DatabaseExplorer.UI
|
||||
Parent.Enabled = true;
|
||||
}
|
||||
|
||||
private List<string> ExportData(string tableName)
|
||||
{
|
||||
DataTable dtData = Exec(string.Format("SELECT * FROM {0}", tableName));
|
||||
|
||||
var listCmds = new List<string>();
|
||||
List<string> listCmdsInsert = Utiles.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;
|
||||
}
|
||||
|
||||
private DataTable Exec(string cmd)
|
||||
{
|
||||
var cnx = new SqlConnection(_config.ConnectionString);
|
||||
|
||||
var da = new SqlDataAdapter(cmd, cnx);
|
||||
var dt = new DataTable();
|
||||
cnx.Open();
|
||||
da.Fill(dt);
|
||||
cnx.Close();
|
||||
|
||||
return dt;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ namespace VAR.DatabaseExplorer.UI
|
||||
{
|
||||
DataTable dataTable = Exec();
|
||||
|
||||
List<string> listCmds = Utiles.DataTable_GenerateInserts(dataTable, txtDestTable.Text);
|
||||
List<string> listCmds = DataTableHelper.DataTable_GenerateInserts(dataTable, txtDestTable.Text);
|
||||
|
||||
// Preparar la datatable destino
|
||||
var destDataTable = new DataTable();
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Windows.Forms;
|
||||
using VAR.DatabaseExplorer.Code.DataAccess;
|
||||
using VAR.DatabaseExplorer.Code.DataTransfer;
|
||||
|
||||
namespace VAR.DatabaseExplorer.UI
|
||||
{
|
||||
@@ -9,6 +12,8 @@ namespace VAR.DatabaseExplorer.UI
|
||||
{
|
||||
#region Declarations
|
||||
|
||||
private string _connectionString;
|
||||
|
||||
private readonly SqlConnection _cnx;
|
||||
|
||||
#endregion
|
||||
@@ -19,12 +24,13 @@ namespace VAR.DatabaseExplorer.UI
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
_connectionString = cnxString;
|
||||
_cnx = new SqlConnection(cnxString);
|
||||
}
|
||||
|
||||
private void frmProcedimientos_Load(object sender, EventArgs e)
|
||||
{
|
||||
LoadProcedures();
|
||||
lsvProcs_LoadData();
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -37,76 +43,31 @@ namespace VAR.DatabaseExplorer.UI
|
||||
|
||||
string name = lsvProcs.SelectedItems[0].SubItems[0].Text;
|
||||
string schema = lsvProcs.SelectedItems[0].SubItems[1].Text;
|
||||
LoadProcedure(schema, name);
|
||||
|
||||
string procDefinition = ProcedureDA.GetProcedureDefinition(_connectionString, schema, name);
|
||||
txtProc.Text = procDefinition;
|
||||
}
|
||||
|
||||
private void btnRefresh_Click(object sender, EventArgs e)
|
||||
{
|
||||
LoadProcedures();
|
||||
lsvProcs_LoadData();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private methods
|
||||
|
||||
private void LoadProcedures()
|
||||
private void lsvProcs_LoadData()
|
||||
{
|
||||
// Obtener un datatable con todos los procedimientos de la base de datos.
|
||||
var da = new SqlDataAdapter(
|
||||
"SELECT ROUTINE_NAME Name, ROUTINE_SCHEMA [Schema], CREATED CreateDate, ROUTINE_TYPE [Type] FROM INFORMATION_SCHEMA.ROUTINES",
|
||||
_cnx);
|
||||
var dt = new DataTable();
|
||||
_cnx.Open();
|
||||
da.Fill(dt);
|
||||
_cnx.Close();
|
||||
List<Procedure> procs = ProcedureDA.GetAllProcedures(_connectionString);
|
||||
|
||||
// Mostrar todos los procedimientos
|
||||
lsvProcs.Items.Clear();
|
||||
foreach (DataRow dr in dt.Rows)
|
||||
foreach (Procedure proc in procs)
|
||||
{
|
||||
ListViewItem item = lsvProcs.Items.Add((String)dr["Name"]);
|
||||
item.SubItems.Add((string)dr["Schema"]);
|
||||
item.SubItems.Add(((DateTime)dr["CreateDate"]).ToShortDateString());
|
||||
item.SubItems.Add((string)dr["Type"]);
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadProcedure(string schema, string name)
|
||||
{
|
||||
SqlDataAdapter dataAdapter;
|
||||
_cnx.Open();
|
||||
|
||||
if (_cnx.ServerVersion.StartsWith("10."))
|
||||
{
|
||||
dataAdapter = new SqlDataAdapter(@"
|
||||
SELECT
|
||||
ISNULL(smsp.definition, ssmsp.definition) AS [Definition]
|
||||
FROM sys.all_objects AS sp
|
||||
INNER JOIN sys.schemas sn ON sp.schema_id = sn.schema_id
|
||||
LEFT OUTER JOIN sys.sql_modules AS smsp ON smsp.object_id = sp.object_id
|
||||
LEFT OUTER JOIN sys.system_sql_modules AS ssmsp ON ssmsp.object_id = sp.object_id
|
||||
WHERE
|
||||
(sp.type = N'P' OR sp.type = N'RF' OR sp.type = N'PC' OR sp.type = N'IF' OR sp.type = N'FN' OR sp.type = N'TF')
|
||||
AND
|
||||
(sp.name = @name and sn.name = @schema)
|
||||
", _cnx);
|
||||
dataAdapter.SelectCommand.Parameters.AddWithValue("@Name", name);
|
||||
dataAdapter.SelectCommand.Parameters.AddWithValue("@Schema", schema);
|
||||
}
|
||||
else
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var dt = new DataTable();
|
||||
dataAdapter.Fill(dt);
|
||||
_cnx.Close();
|
||||
|
||||
// Mostrar el contenido del procedimiento
|
||||
txtProc.Text = String.Empty;
|
||||
foreach (DataRow dr in dt.Rows)
|
||||
{
|
||||
txtProc.Text += ((string)dr[0]).Replace("\r", "").Replace("\n", "\r\n");
|
||||
ListViewItem item = lsvProcs.Items.Add(proc.Name);
|
||||
item.SubItems.Add(proc.Schema);
|
||||
item.SubItems.Add(proc.CreateDate.ToShortDateString());
|
||||
item.SubItems.Add(proc.Type);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace VAR.DatabaseExplorer.UI
|
||||
|
||||
private void btnListarServidores_Click(object sender, EventArgs e)
|
||||
{
|
||||
List<Server> servers = ServerDA.Server_GetRegs();
|
||||
List<Server> servers = ServerDA.GetAllServers();
|
||||
|
||||
lsvServidores.Items.Clear();
|
||||
lsvBBDD.Items.Clear();
|
||||
@@ -41,7 +41,7 @@ namespace VAR.DatabaseExplorer.UI
|
||||
|
||||
private void btnListarBBDD_Click(object sender, EventArgs e)
|
||||
{
|
||||
List<Database> databases = DatabaseDA.Database_GetRegs(BuildConnectionString());
|
||||
List<Database> databases = DatabaseDA.GetAllDatabases(BuildConnectionString());
|
||||
|
||||
lsvBBDD.Items.Clear();
|
||||
foreach (Database database in databases)
|
||||
|
||||
@@ -80,12 +80,17 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Code\BusinessLogic\DatabaseBL.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" />
|
||||
<Compile Include="Code\DataAccess\ServerDA.cs" />
|
||||
<Compile Include="Code\DataAccess\TableDA.cs" />
|
||||
<Compile Include="Code\DataTransfer\Column.cs" />
|
||||
<Compile Include="Code\DataTransfer\Database.cs" />
|
||||
<Compile Include="Code\DataTransfer\Procedure.cs" />
|
||||
<Compile Include="Code\DataTransfer\Server.cs" />
|
||||
<Compile Include="Code\DataTransfer\Table.cs" />
|
||||
<Compile Include="Code\DataTransfer\User.cs" />
|
||||
@@ -93,7 +98,7 @@
|
||||
<SubType>Component</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Code\DatabaseDesc.cs" />
|
||||
<Compile Include="Code\DocGen.cs" />
|
||||
<Compile Include="Code\BusinessLogic\DocumentationGeneratorBL.cs" />
|
||||
<Compile Include="Controls\CustomTextBox.cs">
|
||||
<SubType>Component</SubType>
|
||||
</Compile>
|
||||
@@ -139,7 +144,7 @@
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Code\TablaDesc.cs" />
|
||||
<Compile Include="Code\Utiles.cs" />
|
||||
<Compile Include="Code\DataTableHelper.cs" />
|
||||
<EmbeddedResource Include="UI\FrmBaseDatos.resx">
|
||||
<DependentUpon>FrmBaseDatos.cs</DependentUpon>
|
||||
</EmbeddedResource>
|
||||
@@ -176,9 +181,7 @@
|
||||
<Install>true</Install>
|
||||
</BootstrapperPackage>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="Code\BussinesLogic\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<ItemGroup>
|
||||
<None Include="app.manifest" />
|
||||
</ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user