FrmBaseDatos: btnExportData Exports the data of the selected tables.
This commit is contained in:
@@ -7,6 +7,7 @@ using ServerExplorer.Code;
|
||||
using System.Collections.Generic;
|
||||
using ServerExplorer.Code.DataTransfer;
|
||||
using ServerExplorer.Code.DataAccess;
|
||||
using System.IO;
|
||||
|
||||
namespace ServerExplorer.UI
|
||||
{
|
||||
@@ -271,14 +272,92 @@ namespace ServerExplorer.UI
|
||||
Enabled = true;
|
||||
}
|
||||
|
||||
private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void btnRefresh_Click(object sender, EventArgs e)
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
private void btnExportData_Click(object sender, EventArgs e)
|
||||
{
|
||||
// Hacer insensible la ventana
|
||||
Parent.Enabled = false;
|
||||
|
||||
// Obtener informacion.
|
||||
TablesFromListView();
|
||||
DatabaseDesc db = CrearDatabaseDesc();
|
||||
|
||||
// Preparar cabecera del script
|
||||
var listCmds = new List<string>();
|
||||
listCmds.Add("SET NOCOUNT ON;");
|
||||
listCmds.Add(string.Empty);
|
||||
|
||||
// Comandos de desactivacion de FKs
|
||||
foreach (TablaDesc t in db.Tablas)
|
||||
{
|
||||
string tableName = string.Format("{0}.{1}", t.Esquema, t.Nombre);
|
||||
listCmds.Add(string.Format("ALTER TABLE {0} NOCHECK CONSTRAINT all;", tableName));
|
||||
}
|
||||
listCmds.Add(string.Empty);
|
||||
|
||||
// Prepara informacion a exportar
|
||||
foreach (TablaDesc t in db.Tablas)
|
||||
{
|
||||
string tableName = string.Format("{0}.{1}", t.Esquema, t.Nombre);
|
||||
List<string> listCmdsTableData = ExportData(tableName);
|
||||
listCmds.AddRange(listCmdsTableData);
|
||||
}
|
||||
|
||||
// Comandos de activacion de FKs
|
||||
foreach (TablaDesc t in db.Tablas)
|
||||
{
|
||||
string tableName = string.Format("{0}.{1}", t.Esquema, t.Nombre);
|
||||
listCmds.Add(string.Format("ALTER TABLE {0} WITH CHECK CHECK CONSTRAINT all;", tableName));
|
||||
}
|
||||
listCmds.Add(string.Empty);
|
||||
|
||||
// Escribir script
|
||||
string fixedDatabaseName = db.Nombre.Replace(' ', '_');
|
||||
var escritor = new StreamWriter(fixedDatabaseName + ".data.sql");
|
||||
foreach (string cmd in listCmds)
|
||||
{
|
||||
escritor.WriteLine(cmd);
|
||||
}
|
||||
escritor.Close();
|
||||
|
||||
// Hace sensible la ventana
|
||||
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(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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user