using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Windows.Forms; using ServerExplorer.Code; namespace ServerExplorer.UI { public partial class FrmExec : Form { #region Declarations private readonly string _cnxString; #endregion #region Form life cycle public FrmExec(string cnxString) { InitializeComponent(); _cnxString = cnxString; } #endregion #region Events private void btnExec_Click(object sender, EventArgs e) { try { DataTable dt = Exec(); dgvDatos.DataSource = dt; } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void btnGenInserts_Click(object sender, EventArgs e) { try { DataTable dataTable = Exec(); List listCmds = Utiles.DataTable_GenerateInserts(dataTable, txtDestTable.Text); // Preparar la datatable destino var destDataTable = new DataTable(); destDataTable.Columns.Add("Cmd", typeof(String)); foreach (string cmd in listCmds) { destDataTable.Rows.Add(new object[] { cmd }); } dgvDatos.DataSource = destDataTable; } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void dgvDatos_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (e.DesiredType == typeof(Image)) { if (e.Value is DBNull || (e.Value is byte[] && ((byte[])e.Value).Length <= 0)) { e.Value = new Bitmap(1, 1); } } } #endregion #region Private methods private DataTable Exec() { // Establecer conexion var cnx = new SqlConnection(_cnxString); // Obtener resultado en un datatable. var da = new SqlDataAdapter(txtCommand.Text, cnx); var dt = new DataTable(); cnx.Open(); da.Fill(dt); cnx.Close(); return dt; } #endregion } }