using System; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Globalization; using System.Windows.Forms; 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 { // Obtener el nombre de la tabla destino string destTable = Microsoft.VisualBasic.Interaction.InputBox("Nombre de la tabla destino", "tabla destino", String.Empty, Top + (Height / 2), Left + (Width / 2)); DataTable dt = GenerarInserts(destTable); dgvDatos.DataSource = dt; } 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; } private DataTable GenerarInserts(string destTable) { var nfi = new NumberFormatInfo { NumberDecimalSeparator = "." }; DataTable dataTable = Exec(); // Preparar la datatable destino var destDataTable = new DataTable(); destDataTable.Columns.Add("Comando", typeof(String)); // Recorrer la tabla de datos foreach (DataRow dr in dataTable.Rows) { string strColumns = String.Empty; string strValues = String.Empty; foreach (DataColumn dc in dataTable.Columns) { // El nombre de la columna if (strColumns != String.Empty) strColumns += ", "; strColumns += "[" + dc.ColumnName + "]"; // El valor de la columna if (strValues != String.Empty) strValues += ", "; object valor = dr[dc]; if (valor == DBNull.Value || valor == null) { // NULOS strValues += "NULL"; } else { string type = dc.DataType.Name.ToLower(); if (type == "string") { // Cadenas strValues += "'" + ((string)valor).Replace("'", "''") + "'"; } else if (type == "decimal") { // Decimales strValues += ((decimal)valor).ToString(nfi); } else if (type == "bit" || type == "bool" || type == "boolean") { // Booleanos strValues += ((bool)valor) ? "1" : "0"; } else if (type == "byte[]" ) { // Arrays de bytes (imagenes, archivos etc) strValues += "0x" + BitConverter.ToString(((byte[])valor)).Replace("-", ""); } else { // Otros strValues += valor.ToString(); } } } // Insertar fila a la datatable destino destDataTable.Rows.Add( new object[] { String.Format("INSERT INTO {0} ({1}) VALUES ({2})", destTable, strColumns, strValues) }); } return destDataTable; } #endregion } }