using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.IO;
using System.Data.SqlClient;
using System.Data;


namespace AstronomyDB
{
    public class DataCreator
    {
        public static void CreateDatabase(string server)
        {
            string s = "";
            DbConnectionStringBuilder builder = new DbConnectionStringBuilder();
            builder["Data Source"] = server;
            builder["Initial Catalog"] = "master";
            builder["Integrated Security"] = "SSPI";
            builder["Timeout"] = 10000;
            string cs = builder.ConnectionString;
            SqlConnection conn = new SqlConnection(cs);
            SqlCommand comm = new SqlCommand();
            comm.Connection = conn;
            comm.CommandType = CommandType.Text;
            execStr(s, comm, conn);
            s += "CREATE DATABASE [AstronomyDB] ";
            execStr(s, comm, conn);
            s = "";
            builder["Initial Catalog"] = "AstronomyDB";
            conn = new SqlConnection(builder.ConnectionString);
            comm.Connection = conn;
            s += "SET ANSI_NULLS ON ";
            execStr(s, comm, conn);
            s = "";
            s += "SET QUOTED_IDENTIFIER ON ";
            execStr(s, comm, conn);
            s = "";
            s += "IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[BinaryTree]') AND type in (N'U')) ";
            s += "BEGIN ";
            s += "CREATE TABLE [dbo].[BinaryTree]( ";
            s += "	[Id] [uniqueidentifier] NOT NULL, ";
            s += "	[ParentId] [uniqueidentifier] NOT NULL, ";
            s += "	[Name] [nvarchar](50) NOT NULL, ";
            s += "	[Description] [nvarchar](300) NOT NULL, ";
            s += "	[ext] [char](10) NOT NULL, ";
            s += " CONSTRAINT [PK_BinaryTree] PRIMARY KEY CLUSTERED  ";
            s += "( ";
            s += "	[Id] ASC ";
            s += ")WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] ";
            s += ") ON [PRIMARY] ";
            s += "END ";
            execStr(s, comm, conn);
            s = "";
            s += "SET ANSI_NULLS ON ";
            execStr(s, comm, conn);
            s = "";
            s += "SET QUOTED_IDENTIFIER ON ";
            execStr(s, comm, conn);
            s = "";
            s += "IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[BinaryTable]') AND type in (N'U')) ";
            s += "BEGIN ";
            s += "CREATE TABLE [dbo].[BinaryTable]( ";
            s += "	[Id] [uniqueidentifier] NOT NULL, ";
            s += "	[ParentId] [uniqueidentifier] NOT NULL, ";
            s += "	[Name] [nvarchar](50) NOT NULL, ";
            s += "	[Description] [nvarchar](300) NOT NULL, ";
            s += "	[Data] [image] NOT NULL, ";
            s += "	[Length] [nvarchar](30) NOT NULL, ";
            s += "	[Ext] [varchar](10) NOT NULL, ";
            s += " CONSTRAINT [PK_BinaryTable] PRIMARY KEY CLUSTERED  ";
            s += "( ";
            s += "	[Id] ASC ";
            s += ")WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] ";
            s += ") ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] ";
            s += "END ";
            execStr(s, comm, conn);
            s = "";
            s += "SET ANSI_NULLS ON ";
            execStr(s, comm, conn);
            s = "";
            s += "SET QUOTED_IDENTIFIER ON ";
            execStr(s, comm, conn);
            s = "";
            s += "IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[ViewBinaryTableId]')) ";
            s += "EXEC dbo.sp_executesql @statement = N'CREATE VIEW [dbo].[ViewBinaryTableId] ";
            s += "AS ";
            s += "SELECT     Id ";
            s += "FROM         dbo.BinaryTable ";
            s += "'  ";
            execStr(s, comm, conn);
            s = "";
            s += "EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00] ";
            s += "Begin DesignProperties =  ";
            s += "   Begin PaneConfigurations =  ";
            s += "      Begin PaneConfiguration = 0 ";
            s += "         NumPanes = 4 ";
            s += "         Configuration = \"(H (1[40] 4[20] 2[20] 3) )\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 1 ";
            s += "         NumPanes = 3 ";
            s += "         Configuration = \"(H (1 [50] 4 [25] 3))\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 2 ";
            s += "         NumPanes = 3 ";
            s += "         Configuration = \"(H (1 [50] 2 [25] 3))\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 3 ";
            s += "         NumPanes = 3 ";
            s += "         Configuration = \"(H (4 [30] 2 [40] 3))\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 4 ";
            s += "         NumPanes = 2 ";
            s += "         Configuration = \"(H (1 [56] 3))\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 5 ";
            s += "         NumPanes = 2 ";
            s += "         Configuration = \"(H (2 [66] 3))\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 6 ";
            s += "         NumPanes = 2 ";
            s += "         Configuration = \"(H (4 [50] 3))\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 7 ";
            s += "         NumPanes = 1 ";
            s += "         Configuration = \"(V (3))\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 8 ";
            s += "         NumPanes = 3 ";
            s += "         Configuration = \"(H (1[56] 4[18] 2) )\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 9 ";
            s += "         NumPanes = 2 ";
            s += "         Configuration = \"(H (1 [75] 4))\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 10 ";
            s += "         NumPanes = 2 ";
            s += "         Configuration = \"(H (1[66] 2) )\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 11 ";
            s += "         NumPanes = 2 ";
            s += "         Configuration = \"(H (4 [60] 2))\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 12 ";
            s += "         NumPanes = 1 ";
            s += "         Configuration = \"(H (1) )\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 13 ";
            s += "         NumPanes = 1 ";
            s += "         Configuration = \"(V (4))\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 14 ";
            s += "         NumPanes = 1 ";
            s += "         Configuration = \"(V (2))\" ";
            s += "      End ";
            s += "      ActivePaneConfig = 0 ";
            s += "   End ";
            s += "   Begin DiagramPane =  ";
            s += "      Begin Origin =  ";
            s += "         Top = 0 ";
            s += "         Left = 0 ";
            s += "      End ";
            s += "      Begin Tables =  ";
            s += "         Begin Table = \"BinaryTable\" ";
            s += "            Begin Extent =  ";
            s += "               Top = 6 ";
            s += "               Left = 38 ";
            s += "               Bottom = 114 ";
            s += "               Right = 189 ";
            s += "            End ";
            s += "            DisplayFlags = 280 ";
            s += "            TopColumn = 0 ";
            s += "         End ";
            s += "      End ";
            s += "   End ";
            s += "   Begin SQLPane =  ";
            s += "   End ";
            s += "   Begin DataPane =  ";
            s += "      Begin ParameterDefaults = \"\" ";
            s += "      End ";
            s += "   End ";
            s += "   Begin CriteriaPane =  ";
            s += "      Begin ColumnWidths = 11 ";
            s += "         Column = 1440 ";
            s += "         Alias = 900 ";
            s += "         Table = 1170 ";
            s += "         Output = 720 ";
            s += "         Append = 1400 ";
            s += "         NewValue = 1170 ";
            s += "         SortType = 1350 ";
            s += "         SortOrder = 1410 ";
            s += "         GroupBy = 1350 ";
            s += "         Filter = 1350 ";
            s += "         Or = 1350 ";
            s += "         Or = 1350 ";
            s += "         Or = 1350 ";
            s += "      End ";
            s += "   End ";
            s += "End ";
            s += "' ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'VIEW', @level1name=N'ViewBinaryTableId' ";
            s += " ";
            execStr(s, comm, conn);
            s = "";
            s += "EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'VIEW', @level1name=N'ViewBinaryTableId' ";
            s += " ";
            execStr(s, comm, conn);
            s = "";
            s += "SET ANSI_NULLS ON ";
            execStr(s, comm, conn);
            s = "";
            s += "SET QUOTED_IDENTIFIER ON ";
            execStr(s, comm, conn);
            s = "";
            s += "IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DeleteBinary]') AND type in (N'P', N'PC')) ";
            s += "BEGIN ";
            s += "EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[DeleteBinary] ";
            s += "( ";
            s += "	@Id UNIQUEIDENTIFIER ";
            s += ") ";
            s += "AS ";
            s += "BEGIN ";
            s += "DELETE FROM BinaryTable WHERE Id = @Id ";
            s += "END'  ";
            s += "END ";
            execStr(s, comm, conn);
            s = "";
            s += "SET ANSI_NULLS ON ";
            execStr(s, comm, conn);
            s = "";
            s += "SET QUOTED_IDENTIFIER ON ";
            execStr(s, comm, conn);
            s = "";
            s += "IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[InsertBinary]') AND type in (N'P', N'PC')) ";
            s += "BEGIN ";
            s += "EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[InsertBinary] ";
            s += "	( ";
            s += "	@ParentId UNIQUEIDENTIFIER, ";
            s += "	@Name NVARCHAR(50), ";
            s += "	@Description NVARCHAR(300), ";
            s += "	@Data IMAGE, ";
            s += "	@Length INTEGER, ";
            s += "	@Ext CHAR(10) ";
            s += "	) ";
            s += "AS ";
            s += "BEGIN ";
            s += "DECLARE @TID AS UNIQUEIDENTIFIER ";
            s += "SET @TID = NEWID() ";
            s += "IF (SELECT COUNT(*) FROM BinaryTable WHERE @ParentID = ParentID AND @Name = Name) > 0 ";
            s += "RETURN(1) ";
            s += "INSERT INTO BinaryTable (Id, ParentId, Name, Description, Data, Length, ext) VALUES(@TID, @ParentId, @Name, @Description, @Data, @Length, @Ext)  ";
            s += "SELECT Id FROM ViewBinaryTableId WHERE @TID = Id ";
            s += " ";
            s += "END'  ";
            s += "END ";
            execStr(s, comm, conn);
            s = "";
            s += "SET ANSI_NULLS ON ";
            execStr(s, comm, conn);
            s = "";
            s += "SET QUOTED_IDENTIFIER ON ";
            execStr(s, comm, conn);
            s = "";
            s += "IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SelectBinary]') AND type in (N'P', N'PC')) ";
            s += "BEGIN ";
            s += "EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[SelectBinary] ";
            s += "( ";
            s += "@Id UNIQUEIDENTIFIER ";
            s += ") ";
            s += " ";
            s += "AS ";
            s += "BEGIN ";
            s += "SELECT Data, Ext FROM BinaryTable where Id = @Id ";
            s += "END'  ";
            s += "END ";
            execStr(s, comm, conn);
            s = "";
            s += "SET ANSI_NULLS ON ";
            execStr(s, comm, conn);
            s = "";
            s += "SET QUOTED_IDENTIFIER ON ";
            execStr(s, comm, conn);
            s = "";
            s += "IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SelectBinaryTable]') AND type in (N'P', N'PC')) ";
            s += "BEGIN ";
            s += "EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[SelectBinaryTable] ";
            s += "AS ";
            s += "BEGIN ";
            s += "SELECT Id, ParentId, Name, Description, Ext FROM BinaryTable ";
            s += "END'  ";
            s += "END ";
            execStr(s, comm, conn);
            s = "";
            s += "SET ANSI_NULLS ON ";
            execStr(s, comm, conn);
            s = "";
            s += "SET QUOTED_IDENTIFIER ON ";
            execStr(s, comm, conn);
            s = "";
            s += "IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[UpdateBinaryData]') AND type in (N'P', N'PC')) ";
            s += "BEGIN ";
            s += "EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[UpdateBinaryData] ";
            s += "( ";
            s += "@Id UNIQUEIDENTIFIER, ";
            s += "@Data IMAGE ";
            s += ") ";
            s += "AS ";
            s += "BEGIN ";
            s += "UPDATE BinaryTable SET Data = @Data WHERE Id = @Id ";
            s += "END'  ";
            s += "END ";
            execStr(s, comm, conn);
            s = "";
            s += "SET ANSI_NULLS ON ";
            execStr(s, comm, conn);
            s = "";
            s += "SET QUOTED_IDENTIFIER ON ";
            execStr(s, comm, conn);
            s = "";
            s += "IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[UpdateBinaryDataName]') AND type in (N'P', N'PC')) ";
            s += "BEGIN ";
            s += "EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[UpdateBinaryDataName] ";
            s += "( ";
            s += "@Id UNIQUEIDENTIFIER, ";
            s += "@Name NVARCHAR(50), ";
            s += "@Description NVARCHAR(300) ";
            s += ") ";
            s += "AS ";
            s += "BEGIN ";
            s += "UPDATE BinaryTable SET Name = @Name, Description = @Description WHERE Id = @Id ";
            s += "END'  ";
            s += "END ";
            execStr(s, comm, conn);
            s = "";
            s += "SET ANSI_NULLS ON ";
            execStr(s, comm, conn);
            s = "";
            s += "SET QUOTED_IDENTIFIER ON ";
            execStr(s, comm, conn);
            s = "";
            s += "IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[UpdateBinaryTreeName]') AND type in (N'P', N'PC')) ";
            s += "BEGIN ";
            s += "EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[UpdateBinaryTreeName]  ";
            s += "@Id UNIQUEIDENTIFIER, ";
            s += "@Name NVARCHAR(50), ";
            s += "@Description NVARCHAR(300) ";
            s += "AS ";
            s += "BEGIN ";
            s += "UPDATE BinaryTree SET Name = @Name, Description = @Description WHERE Id = @Id ";
            s += "END'  ";
            s += "END ";
            execStr(s, comm, conn);
            s = "";
            s += "SET ANSI_NULLS ON ";
            execStr(s, comm, conn);
            s = "";
            s += "SET QUOTED_IDENTIFIER ON ";
            execStr(s, comm, conn);
            s = "";
            s += "IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SelectBinaryTree]') AND type in (N'P', N'PC')) ";
            s += "BEGIN ";
            s += "EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[SelectBinaryTree] ";
            s += "AS ";
            s += "BEGIN ";
            s += "SELECT * FROM BinaryTree ";
            s += "END'  ";
            s += "END ";
            execStr(s, comm, conn);
            s = "";
            s += "SET ANSI_NULLS ON ";
            execStr(s, comm, conn);
            s = "";
            s += "SET QUOTED_IDENTIFIER ON ";
            execStr(s, comm, conn);
            s = "";
            s += "IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[InsertBinaryNode]') AND type in (N'P', N'PC')) ";
            s += "BEGIN ";
            s += "EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[InsertBinaryNode] ";
            s += "( ";
            s += "@ParentId UNIQUEIDENTIFIER, ";
            s += "@NAME NVARCHAR(50), ";
            s += "@Description NVARCHAR(300), ";
            s += "@ext CHAR(10) ";
            s += ") ";
            s += "AS ";
            s += "BEGIN ";
            s += "DECLARE @TID AS UNIQUEIDENTIFIER ";
            s += " ";
            s += "SET @TID = NEWID() ";
            s += " ";
            s += "IF (SELECT COUNT(*) FROM BinaryTree WHERE @ParentID = ParentID AND @Name = Name) > 0 ";
            s += "RETURN(1) ";
            s += "INSERT INTO BinaryTree (Id, ParentId, Name, Description, ext) VALUES(@TID, @ParentId, @Name, @Description, @Ext)  ";
            s += "SELECT Id FROM ViewBinaryTreeId WHERE @TID = Id ";
            s += " ";
            s += " ";
            s += "END ";
            s += "'  ";
            s += "END ";
            execStr(s, comm, conn);
            s = "";
            s += "SET ANSI_NULLS ON ";
            execStr(s, comm, conn);
            s = "";
            s += "SET QUOTED_IDENTIFIER ON ";
            execStr(s, comm, conn);
            s = "";
            s += "IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DeleteBinaryTree]') AND type in (N'P', N'PC')) ";
            s += "BEGIN ";
            s += "EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[DeleteBinaryTree] ";
            s += "( ";
            s += "	@Id UNIQUEIDENTIFIER ";
            s += ") ";
            s += "AS ";
            s += "BEGIN ";
            s += "DELETE FROM BinaryTree WHERE Id = @Id ";
            s += "END'  ";
            s += "END ";
            execStr(s, comm, conn);
            s = "";
            s += "SET ANSI_NULLS ON ";
            execStr(s, comm, conn);
            s = "";
            s += "SET QUOTED_IDENTIFIER ON ";
            execStr(s, comm, conn);
            s = "";
            s += "IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[ViewBinaryTreeId]')) ";
            s += "EXEC dbo.sp_executesql @statement = N'CREATE VIEW [dbo].[ViewBinaryTreeId] ";
            s += "AS ";
            s += "SELECT     Id ";
            s += "FROM         dbo.BinaryTree ";
            s += "'  ";
            execStr(s, comm, conn);
            s = "";
            s += "EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00] ";
            s += "Begin DesignProperties =  ";
            s += "   Begin PaneConfigurations =  ";
            s += "      Begin PaneConfiguration = 0 ";
            s += "         NumPanes = 4 ";
            s += "         Configuration = \"(H (1[40] 4[20] 2[20] 3) )\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 1 ";
            s += "         NumPanes = 3 ";
            s += "         Configuration = \"(H (1 [50] 4 [25] 3))\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 2 ";
            s += "         NumPanes = 3 ";
            s += "         Configuration = \"(H (1 [50] 2 [25] 3))\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 3 ";
            s += "         NumPanes = 3 ";
            s += "         Configuration = \"(H (4 [30] 2 [40] 3))\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 4 ";
            s += "         NumPanes = 2 ";
            s += "         Configuration = \"(H (1 [56] 3))\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 5 ";
            s += "         NumPanes = 2 ";
            s += "         Configuration = \"(H (2 [66] 3))\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 6 ";
            s += "         NumPanes = 2 ";
            s += "         Configuration = \"(H (4 [50] 3))\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 7 ";
            s += "         NumPanes = 1 ";
            s += "         Configuration = \"(V (3))\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 8 ";
            s += "         NumPanes = 3 ";
            s += "         Configuration = \"(H (1[56] 4[18] 2) )\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 9 ";
            s += "         NumPanes = 2 ";
            s += "         Configuration = \"(H (1 [75] 4))\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 10 ";
            s += "         NumPanes = 2 ";
            s += "         Configuration = \"(H (1[66] 2) )\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 11 ";
            s += "         NumPanes = 2 ";
            s += "         Configuration = \"(H (4 [60] 2))\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 12 ";
            s += "         NumPanes = 1 ";
            s += "         Configuration = \"(H (1) )\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 13 ";
            s += "         NumPanes = 1 ";
            s += "         Configuration = \"(V (4))\" ";
            s += "      End ";
            s += "      Begin PaneConfiguration = 14 ";
            s += "         NumPanes = 1 ";
            s += "         Configuration = \"(V (2))\" ";
            s += "      End ";
            s += "      ActivePaneConfig = 0 ";
            s += "   End ";
            s += "   Begin DiagramPane =  ";
            s += "      Begin Origin =  ";
            s += "         Top = 0 ";
            s += "         Left = 0 ";
            s += "      End ";
            s += "      Begin Tables =  ";
            s += "         Begin Table = \"BinaryTree\" ";
            s += "            Begin Extent =  ";
            s += "               Top = 6 ";
            s += "               Left = 38 ";
            s += "               Bottom = 114 ";
            s += "               Right = 189 ";
            s += "            End ";
            s += "            DisplayFlags = 280 ";
            s += "            TopColumn = 0 ";
            s += "         End ";
            s += "      End ";
            s += "   End ";
            s += "   Begin SQLPane =  ";
            s += "   End ";
            s += "   Begin DataPane =  ";
            s += "      Begin ParameterDefaults = \"\" ";
            s += "      End ";
            s += "   End ";
            s += "   Begin CriteriaPane =  ";
            s += "      Begin ColumnWidths = 11 ";
            s += "         Column = 1440 ";
            s += "         Alias = 900 ";
            s += "         Table = 1170 ";
            s += "         Output = 720 ";
            s += "         Append = 1400 ";
            s += "         NewValue = 1170 ";
            s += "         SortType = 1350 ";
            s += "         SortOrder = 1410 ";
            s += "         GroupBy = 1350 ";
            s += "         Filter = 1350 ";
            s += "         Or = 1350 ";
            s += "         Or = 1350 ";
            s += "         Or = 1350 ";
            s += "      End ";
            s += "   End ";
            s += "End ";
            s += "' ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'VIEW', @level1name=N'ViewBinaryTreeId' ";
            s += " ";
            execStr(s, comm, conn);
            s = "";
            s += "EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'VIEW', @level1name=N'ViewBinaryTreeId' ";
            s += " ";
            execStr(s, comm, conn);
            s = "";
            s += "IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_BinaryTree_BinaryTree]') AND parent_object_id = OBJECT_ID(N'[dbo].[BinaryTree]')) ";
            s += "ALTER TABLE [dbo].[BinaryTree]  WITH CHECK ADD  CONSTRAINT [FK_BinaryTree_BinaryTree] FOREIGN KEY([ParentId]) ";
            s += "REFERENCES [dbo].[BinaryTree] ([Id]) ";
            execStr(s, comm, conn);
            s = "";
            s += "IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_BinaryTable_BinaryTree]') AND parent_object_id = OBJECT_ID(N'[dbo].[BinaryTable]')) ";
            s += "ALTER TABLE [dbo].[BinaryTable]  WITH CHECK ADD  CONSTRAINT [FK_BinaryTable_BinaryTree] FOREIGN KEY([ParentId]) ";
            s += "REFERENCES [dbo].[BinaryTree] ([Id]) ";
            execStr(s, comm, conn);
            s = "INSERT INTO BinaryTree ";
            s += "(Id, ParentId, Name, Description, ext) ";
            s += "VALUES     ('EB82DFAD-56C4-44a3-B8ED-42C25E9FE554', 'EB82DFAD-56C4-44a3-B8ED-42C25E9FE554', '/', 'Root', ' ')";
            execStr(s, comm, conn);
        }

        static public void CreateString(string fin, string fout)
        {
            StreamReader r = new StreamReader(fin);
            StreamWriter w = new StreamWriter(fout);
            while (true)
            {
                string s = r.ReadLine();
                if (s == null)
                {
                    break;
                }
                s = s.Replace("\"", "\\\"");
                s = "s += \"" + s + " \";";
                w.WriteLine(s);
            }
            r.Close();
            w.Close();
        }


        static public void CreateStringN(string fin, string fout)
        {
            StreamReader r = new StreamReader(fin);
            StreamWriter w = new StreamWriter(fout);

            while (true)
            {
                string s = r.ReadLine();
                if (s == null)
                {
                    break;
                }
                if (s.Contains("GO"))
                {
                    writeExec(w);
                    continue;
                }
                s = s.Replace("\"", "\\\"");
                s = "s += \"" + s + " \";";
                w.WriteLine(s);
            }
            writeExec(w);
            w.Close();
            r.Close();

        }

        static void writeExec(StreamWriter w)
        {
            w.WriteLine("execStr(s, comm, conn);");
            w.WriteLine("s = \"\";");
        }


        static private string getSring(StreamReader r)
        {
            string s = "";
            while (true)
            {
                string ss = r.ReadLine();
                if (ss == null)
                {
                    break;
                }
     
            }
            return s;
        }


        static void execStr(string s, SqlCommand comm, SqlConnection conn)
        {
            if (s.Length == 0)
            {
                return;
            }
            comm.CommandText = s;
            conn.Open();
            comm.ExecuteNonQuery();
            conn.Close();
        }

    }
}
