190 lines
5.4 KiB
C#
190 lines
5.4 KiB
C#
using kmSqlReplicate.ClassObj;
|
|
using log4net;
|
|
using log4net.Config;
|
|
using Microsoft.Data.SqlClient;
|
|
using Microsoft.SqlServer.Management.Sdk.Sfc;
|
|
using Microsoft.SqlServer.Management.Smo;
|
|
using Newtonsoft.Json;
|
|
using System.Collections.Specialized;
|
|
using System.Diagnostics;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using Topshelf;
|
|
|
|
namespace kmSqlReplicate;
|
|
|
|
internal class Program
|
|
{
|
|
internal static string AppPath = AppDomain.CurrentDomain.BaseDirectory.Trim();
|
|
internal static string AppName = AppDomain.CurrentDomain.FriendlyName.Trim();
|
|
internal static string MachineName = Environment.MachineName.Trim();
|
|
internal static string ProgramVersion = string.Empty;
|
|
internal static bool exitNow = false;
|
|
|
|
static void Main(string[] args)
|
|
{
|
|
var logRepo = LogManager.GetRepository(Assembly.GetEntryAssembly());
|
|
XmlConfigurator.Configure(logRepo, new FileInfo(Path.Combine(AppPath, "log4net.config")));
|
|
GlobalContext.Properties["Application"] = AppName;
|
|
|
|
SetProgramVersion();
|
|
if (Debugger.IsAttached)
|
|
{
|
|
if (true)
|
|
{
|
|
var obj = new ClsReplicateBase();
|
|
obj.Go();
|
|
}
|
|
else
|
|
Test1();
|
|
}
|
|
else
|
|
{
|
|
var rc = HostFactory.Run(x =>
|
|
{
|
|
x.Service<ServiceMain>();
|
|
x.EnableServiceRecovery(r => r.RestartService(TimeSpan.FromSeconds(15)));
|
|
x.SetServiceName("kmSqlReplicate");
|
|
x.StartAutomaticallyDelayed();
|
|
x.SetDescription("Will handle replicating of tables in SQL between two servers.");
|
|
x.UseLog4Net();
|
|
});
|
|
}
|
|
}
|
|
private static void SetProgramVersion()
|
|
{
|
|
string FullExecutableName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
|
|
var fif = new FileInfo(FullExecutableName);
|
|
|
|
ProgramVersion = string.Format("{0} ({1:d-MMM-yyyy})", FileVersionOf(FullExecutableName), fif.LastWriteTime);
|
|
} //SetProgramVersion
|
|
internal static Version FileVersionOf(string Filename)
|
|
{
|
|
Version rv;
|
|
try
|
|
{
|
|
var myBuildInfo = FileVersionInfo.GetVersionInfo(Filename);
|
|
string kmBuildVersion = "";
|
|
|
|
kmBuildVersion = string.Format("{0}.{1}.{2}.{3}", myBuildInfo.ProductMajorPart, myBuildInfo.ProductMinorPart, myBuildInfo.ProductBuildPart, myBuildInfo.ProductPrivatePart);
|
|
rv = new Version(kmBuildVersion);
|
|
}
|
|
catch
|
|
{
|
|
rv = new Version("1.0");
|
|
}
|
|
|
|
return rv;
|
|
} //FileVersionOf
|
|
|
|
internal static void Test1()
|
|
{
|
|
var dctOptions = new SortedDictionary<byte, ScriptingOptions>();
|
|
|
|
// Drops first
|
|
dctOptions.Add(0, new ScriptingOptions()
|
|
{
|
|
Default = true,
|
|
ScriptDrops = true,
|
|
//ScriptForCreateDrop = true,
|
|
//DriAllConstraints = true,
|
|
//DriForeignKeys = true,
|
|
IncludeIfNotExists = true,
|
|
//DriAllKeys = true,
|
|
//DriChecks = true,
|
|
//DriAll = true,
|
|
//WithDependencies = false,
|
|
});
|
|
// Then CREATE
|
|
dctOptions.Add(1, new ScriptingOptions()
|
|
{
|
|
Default = true,
|
|
NoIdentities = false,
|
|
ScriptDrops = false,
|
|
ScriptOwner = false,
|
|
NoFileGroup = false,
|
|
NoIndexPartitioningSchemes = true,
|
|
NoTablePartitioningSchemes = true,
|
|
DriDefaults = true,
|
|
//ScriptForCreateDrop = true,
|
|
//Statistics = true,
|
|
//Triggers = true,
|
|
});
|
|
// Get all keys and indexes
|
|
dctOptions.Add(2, new ScriptingOptions()
|
|
{
|
|
Default = false,
|
|
DriPrimaryKey = true,
|
|
Indexes = true,
|
|
ScriptOwner = false,
|
|
NoFileGroup = false,
|
|
NoIndexPartitioningSchemes = true,
|
|
NoTablePartitioningSchemes = true,
|
|
ScriptForCreateDrop = true
|
|
});
|
|
// Get the permissions
|
|
dctOptions.Add(3, new ScriptingOptions()
|
|
{
|
|
Default = false,
|
|
Permissions = true
|
|
});
|
|
|
|
using (var cnSrc = new SqlConnection(string.Format("Server={0};Database={1};Integrated Security=SSPI;TrustServerCertificate=True;", "BigMac", "MyCarHQ")))
|
|
{
|
|
var scrOutput = new StringCollection();
|
|
var txtSql = new StringBuilder();
|
|
|
|
cnSrc.Open();
|
|
|
|
Server S1 = new Server(new Microsoft.SqlServer.Management.Common.ServerConnection(cnSrc));
|
|
Database D1 = S1.Databases["MyCarHQ"];
|
|
//Table T1 = D1.Tables["SourceFiles", "appt"];
|
|
Table T1 = D1.Tables["ConversationMaster", "conv"];
|
|
|
|
//foreach (var fk in T1.ForeignKeys)
|
|
// Console.WriteLine("DROP " + fk.ToString());
|
|
|
|
foreach (var kvp in dctOptions)
|
|
{
|
|
if (true)
|
|
{
|
|
txtSql.Append(string.Join("\n", T1.Script(kvp.Value).Cast<string>().ToArray()));
|
|
txtSql.AppendLine("\nGO\n");
|
|
}
|
|
else
|
|
{
|
|
var scr = new Scripter(S1) { Options = kvp.Value };
|
|
|
|
var dw = new DependencyWalker(S1);
|
|
var dwTree = dw.DiscoverDependencies(new Urn[] { T1.Urn }, true);
|
|
var ddd = dw.WalkDependencies(dwTree);
|
|
|
|
foreach (var d in ddd)
|
|
{
|
|
bool needGo = false;
|
|
|
|
foreach (var s in scr.EnumScript(new[] { d.Urn }))
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(s))
|
|
{
|
|
txtSql.AppendLine(s.ToString() + "\n");
|
|
needGo = true;
|
|
}
|
|
}
|
|
if (needGo)
|
|
txtSql.AppendLine("\nGO\n\n");
|
|
}
|
|
}
|
|
}
|
|
|
|
//foreach (string s in scr.EnumScript(new Urn[] { T1.Urn }))
|
|
// Console.WriteLine(s + "\n\n");
|
|
|
|
//Console.ReadLine();
|
|
|
|
Console.WriteLine(txtSql.ToString());
|
|
Console.ReadLine();
|
|
}
|
|
}
|
|
}
|