diff --git a/ServerExplorer/Code/Config.cs b/ServerExplorer/Code/Config.cs index 8fc0764..4edeff3 100644 --- a/ServerExplorer/Code/Config.cs +++ b/ServerExplorer/Code/Config.cs @@ -10,17 +10,17 @@ namespace ServerExplorer.Code { #region Data parameters - private string conString = ""; + private string _conString = ""; [XmlAttribute("ConnectionString")] public string ConnectionString { - get { return conString; } - set { conString = value; } + get { return _conString; } + set { _conString = value; } } - private List tablas = new List(); + private readonly List _tablas = new List(); [XmlArray("Tablas")] - public List Tablas { get { return tablas; } } + public List Tablas { get { return _tablas; } } #endregion @@ -28,17 +28,17 @@ namespace ServerExplorer.Code public void Guardar(String fichero) { - XmlSerializer seriador = new XmlSerializer(typeof(Config)); - StreamWriter escritor = new StreamWriter(fichero); + var seriador = new XmlSerializer(typeof(Config)); + var escritor = new StreamWriter(fichero); seriador.Serialize(escritor, this); escritor.Close(); } public static Config Cargar(String fichero) { - XmlSerializer seriador = new XmlSerializer(typeof(Config)); - StreamReader lector = new StreamReader(fichero); - Config config = (Config)seriador.Deserialize(lector); + var seriador = new XmlSerializer(typeof(Config)); + var lector = new StreamReader(fichero); + var config = (Config)seriador.Deserialize(lector); lector.Close(); return config; } @@ -49,20 +49,20 @@ namespace ServerExplorer.Code [Serializable] public class TablaInfo { - private string esquema = string.Empty; + private string _esquema = string.Empty; [XmlAttribute("Esquema")] public string Esquema { - get { return esquema; } - set { esquema = value; } + get { return _esquema; } + set { _esquema = value; } } - private string nombre = string.Empty; + private string _nombre = string.Empty; [XmlAttribute("Nombre")] public string Nombre { - get { return nombre; } - set { nombre = value; } + get { return _nombre; } + set { _nombre = value; } } } } \ No newline at end of file diff --git a/ServerExplorer/Code/DatabaseDesc.cs b/ServerExplorer/Code/DatabaseDesc.cs index 0fd9438..33fc654 100644 --- a/ServerExplorer/Code/DatabaseDesc.cs +++ b/ServerExplorer/Code/DatabaseDesc.cs @@ -1,7 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Xml.Serialization; namespace ServerExplorer.Code @@ -9,16 +7,16 @@ namespace ServerExplorer.Code [Serializable] public class DatabaseDesc { - private string nombre = string.Empty; + private string _nombre = string.Empty; [XmlAttribute("Nombre")] public string Nombre { - get { return nombre; } - set { nombre = value; } + get { return _nombre; } + set { _nombre = value; } } - private List tablas = new List(); + private readonly List _tablas = new List(); [XmlArray("Tablas")] - public List Tablas { get { return tablas; } } + public List Tablas { get { return _tablas; } } } } diff --git a/ServerExplorer/Code/DocGen.cs b/ServerExplorer/Code/DocGen.cs index f743b7f..ca68ac9 100644 --- a/ServerExplorer/Code/DocGen.cs +++ b/ServerExplorer/Code/DocGen.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.IO; -using ServerExplorer.Code; +using System.IO; namespace ServerExplorer.Code { @@ -13,7 +8,7 @@ namespace ServerExplorer.Code { // Abrir el documento que contendra la documentacion string fixedDatabaseName = database.Nombre.Replace(' ', '_'); - StreamWriter escritor = new StreamWriter(fixedDatabaseName + ".documentacion.html"); + var escritor = new StreamWriter(fixedDatabaseName + ".documentacion.html"); // Poner cabecera de la documentacion escritor.WriteLine(""); diff --git a/ServerExplorer/Code/TablaDesc.cs b/ServerExplorer/Code/TablaDesc.cs index 32a31c8..9dd44f9 100644 --- a/ServerExplorer/Code/TablaDesc.cs +++ b/ServerExplorer/Code/TablaDesc.cs @@ -1,9 +1,6 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Data; -using System.Data.Sql; using System.Data.SqlClient; using System.Xml.Serialization; @@ -14,25 +11,31 @@ namespace ServerExplorer.Code { #region Data parameters - private String esquema = String.Empty; + private String _esquema = String.Empty; + [XmlAttribute("Esquema")] public string Esquema { - get { return esquema; } - set { esquema = value; } + get { return _esquema; } + set { _esquema = value; } } - private String nombre = String.Empty; + private String _nombre = String.Empty; + [XmlAttribute("Nombre")] public string Nombre { - get { return nombre; } - set { nombre = value; } + get { return _nombre; } + set { _nombre = value; } } - private List columnas = new List(); + private readonly List _columnas = new List(); + [XmlArray("Columnas")] - public List Columnas { get { return columnas; } } + public List Columnas + { + get { return _columnas; } + } #endregion @@ -43,7 +46,7 @@ namespace ServerExplorer.Code { foreach (ColumnaDesc col in Columnas) { - if (col.Nombre.CompareTo(nombre) == 0) + if (String.Compare(col.Nombre, nombre, StringComparison.Ordinal) == 0) { return (col); } @@ -53,41 +56,35 @@ namespace ServerExplorer.Code public void FillDesc(String esquema, String nombre, SqlConnection cnx) { - SqlParameter prm; - DataTable dt; - SqlDataAdapter da; - // establecer esquema y nombre Esquema = esquema; Nombre = nombre; // Preparar comando y parametros - da = new SqlDataAdapter( - "SELECT col.COLUMN_NAME AS Columna, " + - " col.DATA_TYPE AS Tipo, " + - " col.CHARACTER_MAXIMUM_LENGTH AS Tamanho, " + - " c.CONSTRAINT_TYPE AS TipoClave, " + - " col.IS_NULLABLE AS Nullable " + - " FROM INFORMATION_SCHEMA.COLUMNS AS col " + - " LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS k " + - " ON col.COLUMN_NAME=k.COLUMN_NAME AND " + - " col.TABLE_NAME=k.TABLE_NAME AND " + - " col.TABLE_SCHEMA=k.TABLE_SCHEMA " + - " LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS c " + - " ON k.CONSTRAINT_NAME=c.CONSTRAINT_NAME " + - " WHERE col.TABLE_NAME=@nombreTabla AND " + - " col.TABLE_SCHEMA=@nombreEsquema " + - " ORDER BY col.ORDINAL_POSITION", - cnx); - prm = new SqlParameter("@nombreTabla", SqlDbType.VarChar, 100); - prm.Value = nombre; + 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); - prm.Value = esquema; + prm = new SqlParameter("@nombreEsquema", SqlDbType.VarChar, 100) {Value = esquema}; da.SelectCommand.Parameters.Add(prm); // Obtener datatable con las columnas - dt = new DataTable(); + var dt = new DataTable(); cnx.Open(); da.Fill(dt); cnx.Close(); @@ -96,10 +93,8 @@ namespace ServerExplorer.Code Columnas.Clear(); foreach (DataRow dr in dt.Rows) { - ColumnaDesc col; - // Obtener columna - col = GetCol((String)dr["Columna"]); + ColumnaDesc col = GetCol((String) dr["Columna"]); if (col == null) { col = new ColumnaDesc(); @@ -107,20 +102,20 @@ namespace ServerExplorer.Code } // Establecer datos de la columna - col.Nombre = (String)dr["Columna"]; - col.Tipo = ((String)dr["Tipo"]).ToLower(); + col.Nombre = (String) dr["Columna"]; + col.Tipo = ((String) dr["Tipo"]).ToLower(); if (dr["Tamanho"] != DBNull.Value) { - col.Tamanho = (int)dr["Tamanho"]; + col.Tamanho = (int) dr["Tamanho"]; } if (dr["TipoClave"] != DBNull.Value) { - if (((String)dr["TipoClave"]).Contains("PRIMARY")) + if (((String) dr["TipoClave"]).Contains("PRIMARY")) { col.Primaria = true; } } - string strNullable=((String)dr["Nullable"]).ToLower(); + string strNullable = ((String) dr["Nullable"]).ToLower(); col.Nullable = (strNullable == "yes"); } } @@ -149,58 +144,64 @@ namespace ServerExplorer.Code { #region Data properties - private string nombre = String.Empty; + private string _nombre = String.Empty; + [XmlAttribute("Nombre")] public string Nombre { - get { return nombre; } - set { nombre = value; } + get { return _nombre; } + set { _nombre = value; } } - private string tipo = String.Empty; + private string _tipo = String.Empty; + [XmlAttribute("Tipo")] public string Tipo { - get { return tipo; } - set { tipo = value; } + get { return _tipo; } + set { _tipo = value; } } - private int tamanho = -1; + private int _tamanho = -1; + [XmlAttribute("Tamanho")] public int Tamanho { - get { return tamanho; } - set { tamanho = value; } + get { return _tamanho; } + set { _tamanho = value; } } - private bool nullable = false; + private bool _nullable; + [XmlAttribute("Nullable")] public bool Nullable { - get { return nullable; } - set { nullable = value; } + get { return _nullable; } + set { _nullable = value; } } - private bool primaria = false; + private bool _primaria; + [XmlAttribute("Primaria")] public bool Primaria { - get { return primaria; } - set { primaria = value; } + get { return _primaria; } + set { _primaria = value; } } #endregion #region GetTipo - private TipoCol _tipo = TipoCol.Unset; + private TipoCol _tipoCol = TipoCol.Unset; + public TipoCol GetTipo() { string strTipo = Tipo.ToLower(); - if (_tipo != TipoCol.Unset) + if (_tipoCol != TipoCol.Unset) { - return _tipo; + return _tipoCol; } // Numericos @@ -212,7 +213,7 @@ namespace ServerExplorer.Code strTipo == "bigint" ) { - _tipo = TipoCol.Numerico; + _tipoCol = TipoCol.Numerico; } // Aproximados numericos @@ -221,7 +222,7 @@ namespace ServerExplorer.Code strTipo == "real" ) { - _tipo = TipoCol.AproxNumerico; + _tipoCol = TipoCol.AproxNumerico; } // Tiempo @@ -234,7 +235,7 @@ namespace ServerExplorer.Code strTipo == "time" ) { - _tipo = TipoCol.Tiempo; + _tipoCol = TipoCol.Tiempo; } // Texto @@ -247,7 +248,7 @@ namespace ServerExplorer.Code strTipo == "ntext" ) { - _tipo = TipoCol.Texto; + _tipoCol = TipoCol.Texto; } // Binario @@ -257,7 +258,7 @@ namespace ServerExplorer.Code strTipo == "image" ) { - _tipo = TipoCol.Binario; + _tipoCol = TipoCol.Binario; } // Booleano @@ -265,13 +266,13 @@ namespace ServerExplorer.Code strTipo == "bit" ) { - _tipo = TipoCol.Booleano; + _tipoCol = TipoCol.Booleano; } // Otro - _tipo = TipoCol.Otro; + _tipoCol = TipoCol.Otro; - return _tipo; + return _tipoCol; } #endregion diff --git a/ServerExplorer/Code/Utiles.cs b/ServerExplorer/Code/Utiles.cs index f70ff5b..9bd976a 100644 --- a/ServerExplorer/Code/Utiles.cs +++ b/ServerExplorer/Code/Utiles.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.IO; namespace ServerExplorer.Code @@ -10,7 +7,7 @@ namespace ServerExplorer.Code { public static void CrearDirectorio(this String path) { - DirectoryInfo info = null; + DirectoryInfo info; try { info = new DirectoryInfo(path); diff --git a/ServerExplorer/Program.cs b/ServerExplorer/Program.cs index f77d268..fc61a17 100644 --- a/ServerExplorer/Program.cs +++ b/ServerExplorer/Program.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Windows.Forms; using ServerExplorer.UI; diff --git a/ServerExplorer/UI/FrmBaseDatos.cs b/ServerExplorer/UI/FrmBaseDatos.cs index dc02cc9..6792afb 100644 --- a/ServerExplorer/UI/FrmBaseDatos.cs +++ b/ServerExplorer/UI/FrmBaseDatos.cs @@ -1,37 +1,29 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; using System.Windows.Forms; -using System.Data.Sql; using System.Data.SqlClient; using System.Xml.Serialization; -using System.Threading; using ServerExplorer.Code; namespace ServerExplorer.UI { public partial class FrmBaseDatos : Form { - private FrmBaseDatos instancia; - private SqlConnection cnx; - private Config config; - private String tableSchema = null; - private String tableName = null; + private SqlConnection _cnx; + private Config _config; + private String _tableSchema; + private String _tableName; private void Initialize() { // Establecer conexion - txtConString.Text = config.ConnectionString; - cnx = new SqlConnection(config.ConnectionString); + txtConString.Text = _config.ConnectionString; + _cnx = new SqlConnection(_config.ConnectionString); // Obtener lista de tablas - cnx.Open(); - DataTable dt = cnx.GetSchema("Tables"); - cnx.Close(); + _cnx.Open(); + DataTable dt = _cnx.GetSchema("Tables"); + _cnx.Close(); // Mostrar todas las tablas lsvTablas.Items.Clear(); @@ -52,12 +44,12 @@ namespace ServerExplorer.UI /// private void TablesFromListView() { - config.Tablas.Clear(); + _config.Tablas.Clear(); foreach (ListViewItem item in lsvTablas.Items) { if (item.Checked) { - config.Tablas.Add(new TablaInfo + _config.Tablas.Add(new TablaInfo { Esquema = item.SubItems[1].Text, Nombre = item.SubItems[0].Text @@ -71,10 +63,10 @@ namespace ServerExplorer.UI /// private void TablesToListView() { - int i, j, n; + int j; // Desmarcar todos en caso de no haber ninguna tabla - if (config.Tablas.Count == 0) + if (_config.Tablas.Count == 0) { foreach (ListViewItem item in lsvTablas.Items) item.Checked = false; @@ -82,20 +74,15 @@ namespace ServerExplorer.UI } // Recorrer items marcando los que estan en la configuracion - n = config.Tablas.Count; - i = j = 0; + int n = _config.Tablas.Count; + int i = j = 0; foreach (ListViewItem item in lsvTablas.Items) { item.Checked = false; - String tablename = - item.SubItems[1].Text + // Esquema - item.SubItems[0].Text; // Nombre tabla do { - if (config.Tablas[i].Esquema. - CompareTo(item.SubItems[1].Text) == 0 && - config.Tablas[i].Nombre. - CompareTo(item.SubItems[0].Text) == 0) + if (String.Compare(_config.Tablas[i].Esquema, item.SubItems[1].Text, StringComparison.Ordinal) == 0 && + String.Compare(_config.Tablas[i].Nombre, item.SubItems[0].Text, StringComparison.Ordinal) == 0) { item.Checked = true; break; @@ -109,31 +96,28 @@ namespace ServerExplorer.UI public DatabaseDesc CrearDatabaseDesc() { - DatabaseDesc db = new DatabaseDesc { Nombre = cnx.Database }; - foreach (TablaInfo t in config.Tablas) + var db = new DatabaseDesc { Nombre = _cnx.Database }; + foreach (TablaInfo t in _config.Tablas) { - TablaDesc td = new TablaDesc(); - td.FillDesc(t.Esquema, t.Nombre, cnx); + 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(); - instancia = this; - config = new Config(); - config.ConnectionString = ConnectionString; + _config = new Config {ConnectionString = connectionString}; } public FrmBaseDatos(Config config) { InitializeComponent(); - instancia = this; - this.config = config; + _config = config; } @@ -143,14 +127,14 @@ namespace ServerExplorer.UI Initialize(); // Establecer titulos - lblTituloDB.Text = cnx.Database; + lblTituloDB.Text = _cnx.Database; lblTituloTabla.Text = ""; } private void btnCopiarConString_Click(object sender, EventArgs e) { // Copiar la cadena de conexion al portapapeles - System.Windows.Forms.Clipboard.SetText(txtConString.Text); + Clipboard.SetText(txtConString.Text); } private void lsvTablas_SelectedIndexChanged(object sender, EventArgs e) @@ -161,15 +145,15 @@ namespace ServerExplorer.UI ListViewItem item = lsvTablas.SelectedItems[0]; // Recordar tabla seleccionada - tableSchema = item.SubItems[1].Text.ToString(); - tableName = item.SubItems[0].Text.ToString(); + _tableSchema = item.SubItems[1].Text; + _tableName = item.SubItems[0].Text; // Establecer titulo de la lista de columnas - lblTituloTabla.Text = tableSchema + "." + tableName; + lblTituloTabla.Text = _tableSchema + @"." + _tableName; // Obtener descripcion de tabla - TablaDesc td = new TablaDesc(); - td.FillDesc(tableSchema, tableName, cnx); + var td = new TablaDesc(); + td.FillDesc(_tableSchema, _tableName, _cnx); // Mostrar "columnas" de las tablas lsvColumnas.Items.Clear(); @@ -185,21 +169,21 @@ namespace ServerExplorer.UI private void menuCargar_Click(object sender, EventArgs e) { - OpenFileDialog dialogo = new OpenFileDialog(); + var dialogo = new OpenFileDialog(); if (dialogo.ShowDialog() == DialogResult.OK) { - config = Config.Cargar(dialogo.FileName); + _config = Config.Cargar(dialogo.FileName); Initialize(); } } private void menuGuardar_Click(object sender, EventArgs e) { - SaveFileDialog dialogo = new SaveFileDialog(); + var dialogo = new SaveFileDialog(); if (dialogo.ShowDialog() == DialogResult.OK) { TablesFromListView(); - config.Guardar(dialogo.FileName); + _config.Guardar(dialogo.FileName); } } @@ -212,14 +196,14 @@ namespace ServerExplorer.UI private void btnVerDatos_Click(object sender, EventArgs e) { - if (string.IsNullOrEmpty(tableSchema) || string.IsNullOrEmpty(tableName)) { return; } + if (string.IsNullOrEmpty(_tableSchema) || string.IsNullOrEmpty(_tableName)) { return; } // Obtener descripcion de tabla - TablaDesc td = new TablaDesc(); - td.FillDesc(tableSchema, tableName, cnx); + var td = new TablaDesc(); + td.FillDesc(_tableSchema, _tableName, _cnx); // Crear y mostrar el formulario de los datos. - FrmDatos form = new FrmDatos(td, config.ConnectionString); + var form = new FrmDatos(td, _config.ConnectionString); FrmPrincipal.AddForm(form); } @@ -227,14 +211,14 @@ namespace ServerExplorer.UI private void btnProcs_Click(object sender, EventArgs e) { // Crear y mostrar el formulario de los procedimientos. - FrmProcedimientos form = new FrmProcedimientos(config.ConnectionString); + var form = new FrmProcedimientos(_config.ConnectionString); FrmPrincipal.AddForm(form); } private void btnExec_Click(object sender, EventArgs e) { // Crear y mostrar el formulario de exec. - FrmExec form = new FrmExec(config.ConnectionString); + var form = new FrmExec(_config.ConnectionString); FrmPrincipal.AddForm(form); } @@ -249,8 +233,8 @@ namespace ServerExplorer.UI TablesFromListView(); DatabaseDesc db = CrearDatabaseDesc(); - XmlSerializer seriador = new XmlSerializer(typeof(DatabaseDesc)); - seriador.Serialize(System.Console.Out, db); + var seriador = new XmlSerializer(typeof(DatabaseDesc)); + seriador.Serialize(Console.Out, db); //CodeGen.GenerarCodigo(config, db); } @@ -258,7 +242,7 @@ namespace ServerExplorer.UI private void btnDocGen_Click(object sender, EventArgs e) { // Hacer insensible la ventana - this.Parent.Enabled = false; + Parent.Enabled = false; // Obtener informacion. TablesFromListView(); @@ -268,7 +252,7 @@ namespace ServerExplorer.UI DocGen.GenerarDocumentacion(db); // Hace sensible la ventana - this.Parent.Enabled = true; + Parent.Enabled = true; /* tablas_delistview(); @@ -280,7 +264,7 @@ namespace ServerExplorer.UI public void GenerarDoc() { // Hacer insensible la ventana - this.Enabled = false; + Enabled = false; // Obtener informacion. DatabaseDesc db = CrearDatabaseDesc(); @@ -289,7 +273,7 @@ namespace ServerExplorer.UI DocGen.GenerarDocumentacion(db); // Hace sensible la ventana - this.Enabled = true; + Enabled = true; } private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e) diff --git a/ServerExplorer/UI/FrmDatos.cs b/ServerExplorer/UI/FrmDatos.cs index 26a007b..1b11666 100644 --- a/ServerExplorer/UI/FrmDatos.cs +++ b/ServerExplorer/UI/FrmDatos.cs @@ -1,10 +1,6 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; using System.Data; using System.Drawing; -using System.Linq; -using System.Text; using System.Windows.Forms; using ServerExplorer.Code; using System.Data.SqlClient; @@ -15,8 +11,8 @@ namespace ServerExplorer.UI { #region Declarations - private TablaDesc tablaDesc = null; - private string conexionString = string.Empty; + private readonly TablaDesc _tablaDesc; + private readonly string _conexionString = string.Empty; #endregion @@ -25,8 +21,8 @@ namespace ServerExplorer.UI public FrmDatos(TablaDesc tablaDesc, string conexionString) { InitializeComponent(); - this.tablaDesc = tablaDesc; - this.conexionString = conexionString; + _tablaDesc = tablaDesc; + _conexionString = conexionString; LoadDataFromTable(); } @@ -60,11 +56,11 @@ namespace ServerExplorer.UI private void LoadDataFromTable() { - Text = tablaDesc.Esquema + "." + tablaDesc.Nombre; - string tableFullName = "[" + tablaDesc.Esquema + "].[" + tablaDesc.Nombre + "]"; - DataTable dt= new DataTable(); - SqlConnection cnx = new SqlConnection(conexionString); - SqlDataAdapter da = new SqlDataAdapter( "select * from " + tableFullName, cnx); + Text = _tablaDesc.Esquema + @"." + _tablaDesc.Nombre; + string tableFullName = "[" + _tablaDesc.Esquema + "].[" + _tablaDesc.Nombre + "]"; + var dt= new DataTable(); + var cnx = new SqlConnection(_conexionString); + var da = new SqlDataAdapter( "select * from " + tableFullName, cnx); cnx.Open(); da.Fill(dt); cnx.Close(); diff --git a/ServerExplorer/UI/FrmExec.cs b/ServerExplorer/UI/FrmExec.cs index 62bb8b4..1835c93 100644 --- a/ServerExplorer/UI/FrmExec.cs +++ b/ServerExplorer/UI/FrmExec.cs @@ -1,13 +1,9 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Windows.Forms; using System.Data.SqlClient; +using System.Drawing; using System.Globalization; +using System.Windows.Forms; namespace ServerExplorer.UI { @@ -15,7 +11,7 @@ namespace ServerExplorer.UI { #region Declarations - private string cnxString; + private readonly string _cnxString; #endregion @@ -25,7 +21,7 @@ namespace ServerExplorer.UI { InitializeComponent(); - this.cnxString = cnxString; + _cnxString = cnxString; } #endregion @@ -51,7 +47,7 @@ namespace ServerExplorer.UI { // Obtener el nombre de la tabla destino string destTable = Microsoft.VisualBasic.Interaction.InputBox("Nombre de la tabla destino", "tabla destino", String.Empty, - this.Top + (this.Height / 2), this.Left + (this.Width / 2)); + Top + (Height / 2), Left + (Width / 2)); DataTable dt = GenerarInserts(destTable); dgvDatos.DataSource = dt; @@ -79,16 +75,12 @@ namespace ServerExplorer.UI private DataTable Exec() { - SqlConnection cnx; - DataTable dt; - SqlDataAdapter da; - // Establecer conexion - cnx = new SqlConnection(cnxString); + var cnx = new SqlConnection(_cnxString); // Obtener resultado en un datatable. - da = new SqlDataAdapter(txtCommand.Text, cnx); - dt = new DataTable(); + var da = new SqlDataAdapter(txtCommand.Text, cnx); + var dt = new DataTable(); cnx.Open(); da.Fill(dt); cnx.Close(); @@ -98,14 +90,12 @@ namespace ServerExplorer.UI private DataTable GenerarInserts(string destTable) { - DataTable dataTable = new DataTable(); - DataTable destDataTable; - NumberFormatInfo nfi = new NumberFormatInfo { NumberDecimalSeparator = "." }; + var nfi = new NumberFormatInfo { NumberDecimalSeparator = "." }; - dataTable = Exec(); + DataTable dataTable = Exec(); // Preparar la datatable destino - destDataTable = new DataTable(); + var destDataTable = new DataTable(); destDataTable.Columns.Add("Comando", typeof(String)); // Recorrer la tabla de datos diff --git a/ServerExplorer/UI/FrmPrincipal.cs b/ServerExplorer/UI/FrmPrincipal.cs index 04be7f8..f6a91c3 100644 --- a/ServerExplorer/UI/FrmPrincipal.cs +++ b/ServerExplorer/UI/FrmPrincipal.cs @@ -1,10 +1,5 @@ using System; using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; using System.Windows.Forms; using ServerExplorer.Code; using ServerExplorer.Controls; @@ -15,7 +10,7 @@ namespace ServerExplorer.UI { #region Declarations - private static FrmPrincipal currentInstance; + private static FrmPrincipal _currentInstance; #endregion @@ -23,7 +18,7 @@ namespace ServerExplorer.UI public FrmPrincipal() { - currentInstance = this; + _currentInstance = this; InitializeComponent(); } @@ -35,12 +30,12 @@ namespace ServerExplorer.UI public static void AddForm(Form frm) { - frm.MdiParent = currentInstance; + frm.MdiParent = _currentInstance; frm.WindowState = FormWindowState.Maximized; frm.Show(); - currentInstance.listChilds.Add(frm); - frm.FormClosed += currentInstance.FrmPrincipal_OnChildClose; - currentInstance.RefreshWindowButtons(); + _currentInstance.listChilds.Add(frm); + frm.FormClosed += _currentInstance.FrmPrincipal_OnChildClose; + _currentInstance.RefreshWindowButtons(); } protected void FrmPrincipal_OnChildClose(object sender, FormClosedEventArgs e) @@ -90,7 +85,7 @@ namespace ServerExplorer.UI private void menuConectarPRUEBAS_Click(object sender, EventArgs e) { // Crear ventana de la base de datos de pruebas - FrmBaseDatos frm = new FrmBaseDatos("Data Source=SSSRV3;Initial Catalog=PRUEBAS;User ID=sa;Password=SLsssrv3"); + var frm = new FrmBaseDatos("Data Source=SSSRV3;Initial Catalog=PRUEBAS;User ID=sa;Password=SLsssrv3"); //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); @@ -99,22 +94,21 @@ namespace ServerExplorer.UI private void menuBuscarServidor_Click(object sender, EventArgs e) { // Mostrar ventana de buscador de servidores - FrmServidores frm = new FrmServidores(); + var frm = new FrmServidores(); FrmPrincipal.AddForm(frm); } private void menuConectarA_Click(object sender, EventArgs e) { // Cargar configuracion - OpenFileDialog dialogo = new OpenFileDialog(); - if (dialogo.ShowDialog() == DialogResult.OK) - { - Config config = Config.Cargar(dialogo.FileName); + var dialogo = new OpenFileDialog(); + if (dialogo.ShowDialog() != DialogResult.OK) return; - // Crear y mostrar ventana - FrmBaseDatos frm = new FrmBaseDatos(config); - FrmPrincipal.AddForm(frm); - } + Config config = Config.Cargar(dialogo.FileName); + + // Crear y mostrar ventana + var frm = new FrmBaseDatos(config); + FrmPrincipal.AddForm(frm); } #endregion diff --git a/ServerExplorer/UI/FrmProcedimientos.cs b/ServerExplorer/UI/FrmProcedimientos.cs index 57aac03..1111a6f 100644 --- a/ServerExplorer/UI/FrmProcedimientos.cs +++ b/ServerExplorer/UI/FrmProcedimientos.cs @@ -9,7 +9,7 @@ namespace ServerExplorer.UI { #region Declarations - private SqlConnection cnx; + private readonly SqlConnection _cnx; #endregion @@ -19,7 +19,7 @@ namespace ServerExplorer.UI { InitializeComponent(); - cnx = new SqlConnection(cnxString); + _cnx = new SqlConnection(cnxString); } private void frmProcedimientos_Load(object sender, EventArgs e) @@ -35,9 +35,8 @@ namespace ServerExplorer.UI { if (lsvProcs.SelectedItems.Count <= 0) { return; } - string schema, name; - name = lsvProcs.SelectedItems[0].SubItems[0].Text; - schema = lsvProcs.SelectedItems[0].SubItems[1].Text; + string name = lsvProcs.SelectedItems[0].SubItems[0].Text; + string schema = lsvProcs.SelectedItems[0].SubItems[1].Text; LoadProcedure(schema, name); } @@ -52,17 +51,14 @@ namespace ServerExplorer.UI private void LoadProcedures() { - DataTable dt; - SqlDataAdapter da; - // Obtener un datatable con todos los procedimientos de la base de datos. - da = new SqlDataAdapter( + var da = new SqlDataAdapter( "SELECT ROUTINE_NAME Name, ROUTINE_SCHEMA [Schema], CREATED CreateDate, ROUTINE_TYPE [Type] FROM INFORMATION_SCHEMA.ROUTINES", - cnx); - dt = new DataTable(); - cnx.Open(); + _cnx); + var dt = new DataTable(); + _cnx.Open(); da.Fill(dt); - cnx.Close(); + _cnx.Close(); // Mostrar todos los procedimientos lsvProcs.Items.Clear(); @@ -87,14 +83,14 @@ namespace ServerExplorer.UI (sp.type = N'P' OR sp.type = N'RF' OR sp.type='PC') and (sp.name=@name and SCHEMA_NAME(sp.schema_id)=@schema) - ", cnx); + ", _cnx); da.SelectCommand.Parameters.AddWithValue("@Name", name); da.SelectCommand.Parameters.AddWithValue("@Schema", schema); var dt = new DataTable(); - cnx.Open(); + _cnx.Open(); da.Fill(dt); - cnx.Close(); + _cnx.Close(); // Mostrar el contenido del procedimiento txtProc.Text = String.Empty; diff --git a/ServerExplorer/UI/FrmServidores.cs b/ServerExplorer/UI/FrmServidores.cs index 9d295e3..deb10e3 100644 --- a/ServerExplorer/UI/FrmServidores.cs +++ b/ServerExplorer/UI/FrmServidores.cs @@ -1,14 +1,8 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Windows.Forms; using System.Data.Sql; using System.Data.SqlClient; -using System.Data.SqlTypes; +using System.Windows.Forms; namespace ServerExplorer.UI { @@ -32,28 +26,9 @@ namespace ServerExplorer.UI lsvBBDD.Items.Clear(); foreach (DataRow dr in servidores.Rows) { - // Nombre del servidor ListViewItem item = lsvServidores.Items.Add((String)dr["ServerName"]); - - // Nombre de la instancia - if (dr["InstanceName"] == System.DBNull.Value) - { - item.SubItems.Add(""); - } - else - { - item.SubItems.Add((String)dr["InstanceName"]); - } - - // Numero de version - if (dr["Version"] == System.DBNull.Value) - { - item.SubItems.Add("???"); - } - else - { - item.SubItems.Add((String)dr["Version"]); - } + item.SubItems.Add((dr["InstanceName"] == DBNull.Value) ? string.Empty : (String)dr["InstanceName"]); + item.SubItems.Add((dr["Version"] == DBNull.Value) ? "???" : (String) dr["Version"]); } } @@ -62,41 +37,20 @@ namespace ServerExplorer.UI if (lsvServidores.SelectedItems.Count > 0) { ListViewItem item = lsvServidores.SelectedItems[0]; - if (item.SubItems[1].Text.CompareTo("") == 0) - { - // Servidor sin subinstancias - txtServidor.Text = item.SubItems[0].Text; - } - else - { - // Servidor con subinstancias - txtServidor.Text = item.SubItems[0].Text + "/" + 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); } } private void btnListarBBDD_Click(object sender, EventArgs e) { - // Construir cadena de conexion - SqlConnectionStringBuilder constructor = new SqlConnectionStringBuilder(); - constructor.DataSource = txtServidor.Text; - if (String.IsNullOrEmpty(txtUsuario.Text)) - { - constructor.IntegratedSecurity = true; - } - else - { - constructor.UserID = txtUsuario.Text; - constructor.Password = TxtContrasenha.Text; - } - // Obtener todas las bases de datos - SqlConnection cnx = new SqlConnection(constructor.ConnectionString); + var cnx = new SqlConnection(BuildConnectionString()); cnx.Open(); DataTable dt = cnx.GetSchema("Databases"); cnx.Close(); - // Mostrar bases de datos lsvBBDD.Items.Clear(); foreach (DataRow dr in dt.Rows) @@ -109,26 +63,32 @@ namespace ServerExplorer.UI private void lsvBBDD_DoubleClick(object sender, EventArgs e) { - if (lsvBBDD.SelectedItems.Count == 1) - { - // Construir cadena de conexion final - SqlConnectionStringBuilder constructor = new SqlConnectionStringBuilder(); - constructor.DataSource = (!string.IsNullOrEmpty(txtServidor.Text)) ? txtServidor.Text : "localhost"; - constructor.InitialCatalog = (String)lsvBBDD.SelectedItems[0].SubItems[0].Text; - if (txtUsuario.Text.CompareTo("") == 0) - { - constructor.IntegratedSecurity = true; - } - else - { - constructor.UserID = txtUsuario.Text; - constructor.Password = TxtContrasenha.Text; - } + if (lsvBBDD.SelectedItems.Count != 1) return; - // Llamar a la venta de la base de datos - FrmBaseDatos frm = new FrmBaseDatos(constructor.ConnectionString); - FrmPrincipal.AddForm(frm); + // Llamar a la venta de la base de datos + var frm = new FrmBaseDatos(BuildConnectionString()); + FrmPrincipal.AddForm(frm); + } + + private string BuildConnectionString() + { + // Construir cadena de conexion + var constructor = new SqlConnectionStringBuilder(); + constructor.DataSource = (!string.IsNullOrEmpty(txtServidor.Text)) ? txtServidor.Text : "localhost"; + if (lsvBBDD.SelectedItems.Count > 0) + { + constructor.InitialCatalog = lsvBBDD.SelectedItems[0].SubItems[0].Text; } + if (String.IsNullOrEmpty(txtUsuario.Text)) + { + constructor.IntegratedSecurity = true; + } + else + { + constructor.UserID = txtUsuario.Text; + constructor.Password = TxtContrasenha.Text; + } + return constructor.ConnectionString; } } }