Code clean up

This commit is contained in:
2018-10-24 21:28:27 +02:00
parent 4b5ce5b834
commit 4da1292698
27 changed files with 110 additions and 123 deletions

1
.gitignore vendored
View File

@@ -5,3 +5,4 @@
*.user
*/bin/*
*/obj/*
/.vs/*

View File

@@ -1,5 +1,4 @@
using System;
using System.IO;
using System.IO;
using VAR.DatabaseExplorer.Code.DataAccess;
using VAR.DatabaseExplorer.Code.DataTransfer;
@@ -69,7 +68,7 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
// 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'");
@@ -83,7 +82,7 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
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'");
@@ -91,4 +90,4 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
txtWriter.WriteLine(string.Empty);
}
}
}
}

View File

@@ -18,6 +18,5 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
}
return procedures;
}
}
}
}

View File

@@ -13,7 +13,7 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
List<Table> tables = TableDA.GetAllTables(connectionString);
if (fillColumns)
{
foreach(Table table in tables)
foreach (Table table in tables)
{
table.Columns = ColumnDA.GetColumns(connectionString, table.Schema, table.Name);
}
@@ -34,6 +34,5 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
txtWriter.WriteLine("GO");
txtWriter.WriteLine(string.Empty);
}
}
}
}

View File

@@ -48,7 +48,7 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
{
string columnName = Convert.ToString(dr["ColumnName"]);
Column col = columns.FirstOrDefault(c => c.Name == columnName);
if(col == null)
if (col == null)
{
col = new Column();
columns.Add(col);
@@ -76,4 +76,4 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
return columns;
}
}
}
}

View File

@@ -12,7 +12,7 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
{
var databases = new List<Database>();
var cnx = new SqlConnection(connectionString);
cnx.Open();
DataTable dt = cnx.GetSchema("Databases");
cnx.Close();
@@ -23,22 +23,21 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
{
databases.Add(new Database
{
Name = (string) dr["database_name"],
CreateDate = (DateTime) dr["create_date"]
Name = (string)dr["database_name"],
CreateDate = (DateTime)dr["create_date"]
});
}
return databases;
}
public static Database GetInfo(string connectionString)
{
var cnx = new SqlConnection(connectionString);
string strCmd = string.Format(@"
SELECT
SELECT
[name] DatabaseName,
[create_date] CreateDate
FROM sys.databases
FROM sys.databases
WHERE database_id = DB_ID();
");
var da = new SqlDataAdapter(strCmd, cnx);
@@ -55,4 +54,4 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
return database;
}
}
}
}

View File

@@ -21,7 +21,7 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
cnx.Open();
da.Fill(dt);
cnx.Close();
foreach (DataRow dr in dt.Rows)
{
procedures.Add(new Procedure
@@ -79,4 +79,4 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
return sbProc.ToString();
}
}
}
}

View File

@@ -12,7 +12,7 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
{
var servers = new List<Server>();
SqlDataSourceEnumerator enumerador = SqlDataSourceEnumerator.Instance;
DataTable dtServers = enumerador.GetDataSources();
dtServers.DefaultView.Sort = "ServerName ASC, InstanceName ASC, Version ASC";
dtServers = dtServers.DefaultView.ToTable();
@@ -29,4 +29,4 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
return servers;
}
}
}
}

View File

@@ -17,7 +17,7 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
cnx.Close();
dt.DefaultView.Sort = "TABLE_SCHEMA ASC, TABLE_NAME ASC, TABLE_TYPE ASC";
dt = dt.DefaultView.ToTable();
foreach (DataRow dr in dt.Rows)
{
Table table = new Table
@@ -32,7 +32,7 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
return tables;
}
public static DataTable GetData(string connectionString, string tableFullName)
{
var cnx = new SqlConnection(connectionString);
@@ -45,6 +45,5 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
return dt;
}
}
}
}

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
@@ -12,7 +11,7 @@ namespace VAR.DatabaseExplorer.Code
public static void DataTable_GenerateInserts(TextWriter txtWriter, DataTable dataTable, string destTable)
{
var nfi = new NumberFormatInfo { NumberDecimalSeparator = "." };
var sbColumns = new StringBuilder();
foreach (DataColumn dc in dataTable.Columns)
{
@@ -20,7 +19,7 @@ namespace VAR.DatabaseExplorer.Code
if (sbColumns.Length > 0) { sbColumns.Append(", "); }
sbColumns.AppendFormat("[{0}]", dc.ColumnName);
}
// Recorrer la tabla de datos
foreach (DataRow dr in dataTable.Rows)
{
@@ -83,14 +82,13 @@ namespace VAR.DatabaseExplorer.Code
// Otros
sbValues.AppendFormat("'{0}'", valor.ToString());
}
}
}
// Insertar fila a la datatable destino
txtWriter.WriteLine(string.Format("INSERT INTO {0} ({1}) VALUES ({2});",
txtWriter.WriteLine(string.Format("INSERT INTO {0} ({1}) VALUES ({2});",
destTable, sbColumns.ToString(), sbValues.ToString()));
}
}
}
}
}

View File

@@ -1,5 +1,4 @@
using System;
using System;
using System.Xml.Serialization;
namespace VAR.DatabaseExplorer.Code.DataTransfer
@@ -22,4 +21,4 @@ namespace VAR.DatabaseExplorer.Code.DataTransfer
[XmlAttribute]
public bool PK { get; set; }
}
}
}

View File

@@ -1,5 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
@@ -20,4 +19,4 @@ namespace VAR.DatabaseExplorer.Code.DataTransfer
[XmlArray]
public List<Procedure> Procedures { get; set; }
}
}
}

View File

@@ -21,4 +21,4 @@ namespace VAR.DatabaseExplorer.Code.DataTransfer
[XmlElement]
public string Definition { get; set; }
}
}
}

View File

@@ -22,4 +22,4 @@ namespace VAR.DatabaseExplorer.Code.DataTransfer
[XmlArray]
public List<Database> Databases { get; set; }
}
}
}

View File

@@ -1,5 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
@@ -20,4 +19,4 @@ namespace VAR.DatabaseExplorer.Code.DataTransfer
[XmlArray]
public List<Column> Columns { get; set; }
}
}
}

View File

@@ -15,4 +15,4 @@ namespace VAR.DatabaseExplorer.Code.DataTransfer
[XmlAttribute]
public string Password { get; set; }
}
}
}

View File

@@ -1,11 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Collections;
namespace VAR.DatabaseExplorer.Controls
{
@@ -18,20 +15,21 @@ namespace VAR.DatabaseExplorer.Controls
{
#region Declarations
ListViewItemComparer itemComparer = new ListViewItemComparer();
private ListViewItemComparer itemComparer = new ListViewItemComparer();
#endregion
#endregion Declarations
#region Properties
private bool allowSorting = false;
public bool AllowSorting
{
get { return allowSorting; }
set { allowSorting = value; }
}
#endregion
#endregion Properties
#region Control life cicle
@@ -40,7 +38,7 @@ namespace VAR.DatabaseExplorer.Controls
ColumnClick += CustomListView_ColumnClick;
}
#endregion
#endregion Control life cicle
#region Events
@@ -52,7 +50,7 @@ namespace VAR.DatabaseExplorer.Controls
Sort();
}
#endregion
#endregion Events
}
#region ListViewItemComparer
@@ -70,8 +68,8 @@ namespace VAR.DatabaseExplorer.Controls
public int Compare(object x, object y)
{
var itemA = (ListViewItem) x;
var itemB = (ListViewItem) y;
var itemA = (ListViewItem)x;
var itemB = (ListViewItem)y;
if (itemA.SubItems.Count <= _col || itemB.SubItems.Count <= _col)
{
@@ -83,5 +81,5 @@ namespace VAR.DatabaseExplorer.Controls
}
}
#endregion
}
#endregion ListViewItemComparer
}

View File

@@ -20,9 +20,10 @@ namespace VAR.DatabaseExplorer.Controls
SendMessage(Handle, EM_SETTABSTOPS, 1, new int[] { tabWidth * 4 });
}
#endregion
#endregion SetTabWidth
private int _tabWidth = 8;
private int _tabWidth=8;
public int TabWidth
{
get { return _tabWidth; }
@@ -33,4 +34,4 @@ namespace VAR.DatabaseExplorer.Controls
}
}
}
}
}

View File

@@ -9,6 +9,7 @@ namespace VAR.DatabaseExplorer.Controls
#region Properties
private Form _window = null;
public Form Window
{
get { return _window; }
@@ -16,6 +17,7 @@ namespace VAR.DatabaseExplorer.Controls
}
private bool _active = false;
public bool Active
{
get { return _active; }
@@ -26,7 +28,7 @@ namespace VAR.DatabaseExplorer.Controls
}
}
#endregion
#endregion Properties
#region Creator
@@ -37,16 +39,16 @@ namespace VAR.DatabaseExplorer.Controls
Click += WindowButton_Click;
}
#endregion
#endregion Creator
#region Events
void WindowButton_Click(object sender, EventArgs e)
private void WindowButton_Click(object sender, EventArgs e)
{
if (_window == null) { return; }
_window.Activate();
}
#endregion
#endregion Events
}
}
}

View File

@@ -4,17 +4,17 @@ using VAR.DatabaseExplorer.UI;
namespace VAR.DatabaseExplorer
{
static class Program
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
private static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FrmPrincipal());
}
}
}
}

View File

@@ -11,4 +11,4 @@ using System.Runtime.InteropServices;
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("31f76805-336c-471c-8af2-a4c1364e97b9")]
[assembly: AssemblyVersion("1.0.0.*")]
[assembly: AssemblyVersion("1.0.0.*")]

View File

@@ -1,10 +1,7 @@
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;
using VAR.DatabaseExplorer.Code;
using VAR.DatabaseExplorer.Code.BusinessLogic;
using VAR.DatabaseExplorer.Code.DataAccess;
using VAR.DatabaseExplorer.Code.DataTransfer;
@@ -28,17 +25,17 @@ namespace VAR.DatabaseExplorer.UI
item.SubItems.Add(table.Name);
item.SubItems.Add(table.Type);
}
lsvColumnas.Items.Clear();
}
public FrmBaseDatos(string connectionString)
{
InitializeComponent();
_connectionString = connectionString;
txtConString.Text = connectionString;
}
private void frmBaseDatos_Load(object sender, EventArgs e)
{
Initialize();
@@ -55,13 +52,13 @@ namespace VAR.DatabaseExplorer.UI
private void lsvTablas_SelectedIndexChanged(object sender, EventArgs e)
{
if (lsvTablas.SelectedItems.Count != 1) { return; }
ListViewItem item = lsvTablas.SelectedItems[0];
_tableSchema = item.SubItems[0].Text;
_tableName = item.SubItems[1].Text;
lblTituloTabla.Text = _tableSchema + @"." + _tableName;
List<Column> columns = ColumnDA.GetColumns(_connectionString, _tableSchema, _tableName);
lsvColumnas.Items.Clear();
foreach (Column col in columns)
@@ -73,16 +70,15 @@ namespace VAR.DatabaseExplorer.UI
subitem.SubItems.Add(col.Nullable ? "Null" : "NotNull");
}
}
private void btnVerDatos_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(_tableSchema) || string.IsNullOrEmpty(_tableName)) { return; }
var form = new FrmDatos(_connectionString, _tableSchema, _tableName);
FrmPrincipal.AddForm(form);
}
private void btnProcs_Click(object sender, EventArgs e)
{
var form = new FrmProcedimientos(_connectionString);
@@ -94,21 +90,21 @@ namespace VAR.DatabaseExplorer.UI
var form = new FrmExec(_connectionString);
FrmPrincipal.AddForm(form);
}
private void btnDocGen_Click(object sender, EventArgs e)
{
Parent.Enabled = false;
Database database = DatabaseBL.Database_GetSchema(connectionString: _connectionString, fillDefinitions: true);
string fixedDatabaseName = database.Name.Replace(' ', '_');
var streamWriter = new StreamWriter(fixedDatabaseName + ".documentation.html");
DatabaseBL.Database_GenerateDocumentation(streamWriter, database);
streamWriter.Close();
Parent.Enabled = true;
}
private void btnRefresh_Click(object sender, EventArgs e)
{
Initialize();
@@ -117,15 +113,14 @@ namespace VAR.DatabaseExplorer.UI
private void btnExportData_Click(object sender, EventArgs e)
{
Parent.Enabled = false;
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();
Parent.Enabled = true;
}
}
}
}

View File

@@ -1,9 +1,8 @@
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
using VAR.DatabaseExplorer.Code;
using System.Data.SqlClient;
namespace VAR.DatabaseExplorer.UI
{
@@ -15,7 +14,7 @@ namespace VAR.DatabaseExplorer.UI
private readonly string _tableSchema = string.Empty;
private readonly string _tableName = string.Empty;
#endregion
#endregion Declarations
#region Form life cycle
@@ -28,14 +27,18 @@ namespace VAR.DatabaseExplorer.UI
_conexionString = conexionString;
LoadDataFromTable();
}
private void frmDatos_Load(object sender, EventArgs e) { }
#endregion
private void frmDatos_Load(object sender, EventArgs e)
{
}
#endregion Form life cycle
#region Events
private void dgvDatos_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { }
private void dgvDatos_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
}
private void dgvDatos_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
@@ -53,7 +56,7 @@ namespace VAR.DatabaseExplorer.UI
LoadDataFromTable();
}
#endregion
#endregion Events
#region Private methods
@@ -61,15 +64,15 @@ namespace VAR.DatabaseExplorer.UI
{
Text = _tableSchema + @"." + _tableName;
string tableFullName = "[" + _tableSchema + "].[" + _tableName + "]";
var dt= new DataTable();
var dt = new DataTable();
var cnx = new SqlConnection(_conexionString);
var da = new SqlDataAdapter( "select * from " + tableFullName, cnx);
var da = new SqlDataAdapter("select * from " + tableFullName, cnx);
cnx.Open();
da.Fill(dt);
cnx.Close();
dgvDatos.DataSource = dt;
}
#endregion
#endregion Private methods
}
}
}

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
@@ -15,7 +14,7 @@ namespace VAR.DatabaseExplorer.UI
private readonly string _cnxString;
#endregion
#endregion Declarations
#region Form life cycle
@@ -26,7 +25,7 @@ namespace VAR.DatabaseExplorer.UI
_cnxString = cnxString;
}
#endregion
#endregion Form life cycle
#region Events
@@ -79,7 +78,7 @@ namespace VAR.DatabaseExplorer.UI
}
}
#endregion
#endregion Events
#region Private methods
@@ -98,6 +97,6 @@ namespace VAR.DatabaseExplorer.UI
return dt;
}
#endregion
#endregion Private methods
}
}
}

View File

@@ -11,7 +11,7 @@ namespace VAR.DatabaseExplorer.UI
private static FrmPrincipal _currentInstance;
#endregion
#endregion Declarations
#region Creator
@@ -21,7 +21,7 @@ namespace VAR.DatabaseExplorer.UI
InitializeComponent();
}
#endregion
#endregion Creator
#region WindowList
@@ -77,16 +77,16 @@ namespace VAR.DatabaseExplorer.UI
}
}
#endregion
#endregion WindowList
#region Menus
private void menuBuscarServidor_Click(object sender, EventArgs e)
{
var frm = new FrmServidores();
FrmPrincipal.AddForm(frm);
}
#endregion
#endregion Menus
}
}
}

View File

@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using VAR.DatabaseExplorer.Code.DataAccess;
@@ -16,7 +15,7 @@ namespace VAR.DatabaseExplorer.UI
private readonly SqlConnection _cnx;
#endregion
#endregion Declarations
#region Form life cycle
@@ -33,7 +32,7 @@ namespace VAR.DatabaseExplorer.UI
lsvProcs_LoadData();
}
#endregion
#endregion Form life cycle
#region Events
@@ -53,7 +52,7 @@ namespace VAR.DatabaseExplorer.UI
lsvProcs_LoadData();
}
#endregion
#endregion Events
#region Private methods
@@ -71,6 +70,6 @@ namespace VAR.DatabaseExplorer.UI
}
}
#endregion
#endregion Private methods
}
}
}

View File

@@ -52,7 +52,6 @@ namespace VAR.DatabaseExplorer.UI
}
}
private void lsvBBDD_DoubleClick(object sender, EventArgs e)
{
if (lsvBBDD.SelectedItems.Count != 1) return;
@@ -83,4 +82,4 @@ namespace VAR.DatabaseExplorer.UI
return constructor.ConnectionString;
}
}
}
}