diff --git a/VAR.DatabaseExplorer/Code/BusinessLogic/DatabaseBL.cs b/VAR.DatabaseExplorer/Code/BusinessLogic/DatabaseBL.cs
index 8baed03..651e18d 100644
--- a/VAR.DatabaseExplorer/Code/BusinessLogic/DatabaseBL.cs
+++ b/VAR.DatabaseExplorer/Code/BusinessLogic/DatabaseBL.cs
@@ -14,10 +14,10 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
return database;
}
- public static Database Database_GetSchema(string connectionString, bool fillTableDefinitions = false, bool fillProcDefinitions = false)
+ public static Database Database_GetSchema(string connectionString, bool fillTableDefinitions = false, bool fillProcDefinitions = false, bool fillViewDefinitions = false)
{
Database database = DatabaseDA.GetInfo(connectionString);
- database.Tables = TableBL.Table_GetAll(connectionString, fillTableDefinitions);
+ database.Tables = TableBL.Table_GetAll(connectionString, fillTableDefinitions, fillViewDefinitions);
database.Procedures = ProcedureBL.Procedure_GetAll(connectionString, fillProcDefinitions);
return database;
}
@@ -151,5 +151,19 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
txtWriter.WriteLine(string.Empty);
}
}
+
+ public static void Database_ExportViews(StreamWriter txtWriter, Database database)
+ {
+ // Preparar cabecera del script
+ txtWriter.WriteLine("SET NOCOUNT ON;");
+ txtWriter.WriteLine("GO");
+ txtWriter.WriteLine(string.Empty);
+
+ foreach (Table table in database.Tables)
+ {
+ if (table.Type != "VIEW") { continue; }
+ TableBL.View_GenerateCreate(table, txtWriter);
+ }
+ }
}
}
\ No newline at end of file
diff --git a/VAR.DatabaseExplorer/Code/BusinessLogic/TableBL.cs b/VAR.DatabaseExplorer/Code/BusinessLogic/TableBL.cs
index be30dbd..3e5cca2 100644
--- a/VAR.DatabaseExplorer/Code/BusinessLogic/TableBL.cs
+++ b/VAR.DatabaseExplorer/Code/BusinessLogic/TableBL.cs
@@ -2,6 +2,7 @@
using System.Data;
using System.IO;
using System.Linq;
+using System.Text;
using VAR.DatabaseExplorer.Code.DataAccess;
using VAR.DatabaseExplorer.Code.DataTransfer;
@@ -9,7 +10,7 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
{
public class TableBL
{
- public static List
Table_GetAll(string connectionString, bool fillColumns = false)
+ public static List Table_GetAll(string connectionString, bool fillColumns = false, bool fillViewDefinitions = false)
{
List tables = TableDA.GetAllTables(connectionString);
if (fillColumns)
@@ -20,6 +21,23 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
table.Columns = allColumns.Where(c => c.TableName == table.Name && c.TableSchema == table.Schema).ToList();
}
}
+ if (fillViewDefinitions)
+ {
+ // Get all view definitions
+ List definitions = TableDA.GetViewDefinitions(connectionString, null, null);
+ foreach (Table table in tables)
+ {
+ var sbDefinition = new StringBuilder();
+ foreach (ViewDefinition definition in definitions)
+ {
+ if (table.Schema == definition.ViewSchema && table.Name == definition.ViewName)
+ {
+ sbDefinition.Append(definition.Definition);
+ }
+ }
+ table.ViewDefinition = sbDefinition.ToString();
+ }
+ }
return tables;
}
@@ -123,5 +141,22 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
txtWriter.WriteLine("GO");
txtWriter.WriteLine();
}
+
+ public static void View_GenerateCreate(Table table, StreamWriter txtWriter)
+ {
+ if (table.Type != "VIEW") { return; }
+
+ string viewName = string.Format("{0}.{1}", table.Schema, table.Name);
+ txtWriter.WriteLine(string.Format("PRINT '*** Creating view {0}....';", viewName));
+ txtWriter.WriteLine(string.Format("IF EXISTS (SELECT TOP 1 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{0}' AND TABLE_NAME = '{1}')", table.Schema, table.Name));
+ txtWriter.WriteLine("BEGIN");
+ txtWriter.WriteLine(string.Format(" DROP VIEW {0};", viewName));
+ txtWriter.WriteLine("END");
+
+ txtWriter.WriteLine("GO");
+ txtWriter.WriteLine(table.ViewDefinition);
+ txtWriter.WriteLine("GO");
+ txtWriter.WriteLine();
+ }
}
}
\ No newline at end of file
diff --git a/VAR.DatabaseExplorer/Code/DataAccess/TableDA.cs b/VAR.DatabaseExplorer/Code/DataAccess/TableDA.cs
index 5266a40..d262f01 100644
--- a/VAR.DatabaseExplorer/Code/DataAccess/TableDA.cs
+++ b/VAR.DatabaseExplorer/Code/DataAccess/TableDA.cs
@@ -1,4 +1,5 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using VAR.DatabaseExplorer.Code.DataTransfer;
@@ -33,6 +34,53 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
return tables;
}
+ public static List GetViewDefinitions(string connectionString, string schema = null, string name = null)
+ {
+ SqlDataAdapter dataAdapter;
+ var cnx = new SqlConnection(connectionString);
+ cnx.Open();
+
+ if (cnx.ServerVersion.StartsWith("10.") || cnx.ServerVersion.StartsWith("11.") || cnx.ServerVersion.StartsWith("13.") || cnx.ServerVersion.StartsWith("14."))
+ {
+ dataAdapter = new SqlDataAdapter(@"
+ SELECT
+ sn.name ViewSchema,
+ SO.name ViewName,
+ ISNULL(SM.definition, SM.definition) AS [Definition]
+ FROM sys.sql_modules SM
+ INNER JOIN sys.Objects SO ON SM.Object_id = SO.Object_id
+ INNER JOIN sys.schemas sn ON SO.schema_id = sn.schema_id
+ WHERE SO.type = 'v'
+ AND
+ (@name IS NULL OR SO.name = @name) AND
+ (@schema IS NULL OR sn.name = @schema)
+ ", cnx);
+ dataAdapter.SelectCommand.Parameters.AddWithValue("@Name", (object)name ?? DBNull.Value);
+ dataAdapter.SelectCommand.Parameters.AddWithValue("@Schema", (object)schema ?? DBNull.Value);
+ }
+ else
+ {
+ cnx.Close();
+ return null;
+ }
+
+ var dt = new DataTable();
+ dataAdapter.Fill(dt);
+ cnx.Close();
+
+ var definitions = new List();
+ foreach (DataRow dr in dt.Rows)
+ {
+ string strViewSchema = Convert.ToString(dr["ViewSchema"]);
+ string strViewName = Convert.ToString(dr["ViewName"]);
+ string strDefinition = Convert.ToString(dr["Definition"]);
+ strDefinition = strDefinition.Replace("\r", "").Replace("\n", "\r\n");
+ definitions.Add(new ViewDefinition { ViewSchema = strViewSchema, ViewName = strViewName, Definition = strDefinition });
+ }
+
+ return definitions;
+ }
+
public static DataTable GetData(string connectionString, string tableFullName)
{
var cnx = new SqlConnection(connectionString);
diff --git a/VAR.DatabaseExplorer/Code/DataTransfer/Table.cs b/VAR.DatabaseExplorer/Code/DataTransfer/Table.cs
index b682796..22ce175 100644
--- a/VAR.DatabaseExplorer/Code/DataTransfer/Table.cs
+++ b/VAR.DatabaseExplorer/Code/DataTransfer/Table.cs
@@ -18,5 +18,8 @@ namespace VAR.DatabaseExplorer.Code.DataTransfer
[XmlArray]
public List Columns { get; set; }
+
+ [XmlElement]
+ public string ViewDefinition { get; set; }
}
}
\ No newline at end of file
diff --git a/VAR.DatabaseExplorer/Code/DataTransfer/ViewDefinition.cs b/VAR.DatabaseExplorer/Code/DataTransfer/ViewDefinition.cs
new file mode 100644
index 0000000..4d50485
--- /dev/null
+++ b/VAR.DatabaseExplorer/Code/DataTransfer/ViewDefinition.cs
@@ -0,0 +1,9 @@
+namespace VAR.DatabaseExplorer.Code.DataTransfer
+{
+ public class ViewDefinition
+ {
+ public string ViewSchema { get; set; }
+ public string ViewName { get; set; }
+ public string Definition { get; set; }
+ }
+}
diff --git a/VAR.DatabaseExplorer/UI/FrmBaseDatos.cs b/VAR.DatabaseExplorer/UI/FrmBaseDatos.cs
index 0e8da6e..4ea4ad5 100644
--- a/VAR.DatabaseExplorer/UI/FrmBaseDatos.cs
+++ b/VAR.DatabaseExplorer/UI/FrmBaseDatos.cs
@@ -116,7 +116,7 @@ namespace VAR.DatabaseExplorer.UI
Database database = DatabaseBL.Database_GetSchema(connectionString: _connectionString, fillTableDefinitions: true);
string fixedDatabaseName = database.Name.Replace(' ', '_');
- var streamWriter = new StreamWriter(fixedDatabaseName + ".Data.sql");
+ var streamWriter = new StreamWriter("05." + fixedDatabaseName + ".Data.sql");
DatabaseBL.Database_ExportData(streamWriter, _connectionString, database);
streamWriter.Close();
@@ -127,7 +127,7 @@ namespace VAR.DatabaseExplorer.UI
{
Parent.Enabled = false;
- Database database = DatabaseBL.Database_GetSchema(connectionString: _connectionString, fillTableDefinitions: true, fillProcDefinitions: true);
+ Database database = DatabaseBL.Database_GetSchema(connectionString: _connectionString, fillTableDefinitions: true, fillProcDefinitions: true, fillViewDefinitions: true);
string fixedDatabaseName = database.Name.Replace(' ', '_');
// Schemas
@@ -136,16 +136,19 @@ namespace VAR.DatabaseExplorer.UI
streamWriterSchemas.Close();
// Tables
- var streamWriterTables = new StreamWriter(fixedDatabaseName + ".Tables.sql");
+ var streamWriterTables = new StreamWriter("02." + fixedDatabaseName + ".Tables.sql");
DatabaseBL.Database_ExportTables(streamWriterTables, _connectionString, database);
streamWriterTables.Close();
// Routines
- var streamWriterRoutines = new StreamWriter(fixedDatabaseName + ".Routines.sql");
+ var streamWriterRoutines = new StreamWriter("03." + fixedDatabaseName + ".Routines.sql");
DatabaseBL.Database_ExportFunctions(streamWriterRoutines, database);
streamWriterRoutines.Close();
- // TODO Views
+ // Views
+ var streamWriterViews = new StreamWriter("04." + fixedDatabaseName + ".Views.sql");
+ DatabaseBL.Database_ExportViews(streamWriterViews, database);
+ streamWriterViews.Close();
Parent.Enabled = true;
}
diff --git a/VAR.DatabaseExplorer/VAR.DatabaseExplorer.csproj b/VAR.DatabaseExplorer/VAR.DatabaseExplorer.csproj
index 224686e..6ecda9a 100644
--- a/VAR.DatabaseExplorer/VAR.DatabaseExplorer.csproj
+++ b/VAR.DatabaseExplorer/VAR.DatabaseExplorer.csproj
@@ -95,6 +95,7 @@
+
Component