first commit
This commit is contained in:
commit
1c5cf23dbf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,119 @@
|
|||
{
|
||||
"Version": 1,
|
||||
"WorkspaceRootPath": "C:\\Repos\\kmSqlReplicate\\",
|
||||
"Documents": [
|
||||
{
|
||||
"AbsoluteMoniker": "D:0:0:{364F9228-3601-4813-9E87-F042D9357580}|kmSqlReplicate.csproj|c:\\repos\\kmsqlreplicate\\classobj\\clsreplicatebase.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
|
||||
"RelativeMoniker": "D:0:0:{364F9228-3601-4813-9E87-F042D9357580}|kmSqlReplicate.csproj|solutionrelative:classobj\\clsreplicatebase.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
|
||||
},
|
||||
{
|
||||
"AbsoluteMoniker": "D:0:0:{364F9228-3601-4813-9E87-F042D9357580}|kmSqlReplicate.csproj|c:\\repos\\kmsqlreplicate\\program.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
|
||||
"RelativeMoniker": "D:0:0:{364F9228-3601-4813-9E87-F042D9357580}|kmSqlReplicate.csproj|solutionrelative:program.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
|
||||
},
|
||||
{
|
||||
"AbsoluteMoniker": "D:0:0:{364F9228-3601-4813-9E87-F042D9357580}|kmSqlReplicate.csproj|c:\\repos\\kmsqlreplicate\\classobj\\servicemain.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
|
||||
"RelativeMoniker": "D:0:0:{364F9228-3601-4813-9E87-F042D9357580}|kmSqlReplicate.csproj|solutionrelative:classobj\\servicemain.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
|
||||
}
|
||||
],
|
||||
"DocumentGroupContainers": [
|
||||
{
|
||||
"Orientation": 0,
|
||||
"VerticalTabListWidth": 256,
|
||||
"DocumentGroups": [
|
||||
{
|
||||
"DockedWidth": 200,
|
||||
"SelectedChildIndex": 12,
|
||||
"Children": [
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
"Name": "ST:128:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
|
||||
},
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
"Name": "ST:0:0:{1c4feeaa-4718-4aa9-859d-94ce25d182ba}"
|
||||
},
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
"Name": "ST:129:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
|
||||
},
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
"Name": "ST:130:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
|
||||
},
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
"Name": "ST:131:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
|
||||
},
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
"Name": "ST:132:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
|
||||
},
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
"Name": "ST:133:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
|
||||
},
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
"Name": "ST:134:0:{1fc202d4-d401-403c-9834-5b218574bb67}"
|
||||
},
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
"Name": "ST:135:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
|
||||
},
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
"Name": "ST:136:0:{1fc202d4-d401-403c-9834-5b218574bb67}"
|
||||
},
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
"Name": "ST:0:0:{aa2115a1-9712-457b-9047-dbb71ca2cdd2}"
|
||||
},
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
"Name": "ST:1:0:{e8b06f52-6d01-11d2-aa7d-00c04f990343}"
|
||||
},
|
||||
{
|
||||
"$type": "Document",
|
||||
"DocumentIndex": 0,
|
||||
"Title": "ClsReplicateBase.cs",
|
||||
"DocumentMoniker": "C:\\Repos\\kmSqlReplicate\\ClassObj\\ClsReplicateBase.cs",
|
||||
"RelativeDocumentMoniker": "ClassObj\\ClsReplicateBase.cs",
|
||||
"ToolTip": "C:\\Repos\\kmSqlReplicate\\ClassObj\\ClsReplicateBase.cs",
|
||||
"RelativeToolTip": "ClassObj\\ClsReplicateBase.cs",
|
||||
"ViewState": "AgIAAEIAAAAAAAAAAAAAAGEAAAAwAAAAAAAAAA==",
|
||||
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
|
||||
"WhenOpened": "2025-08-29T11:21:32.713Z",
|
||||
"EditorCaption": ""
|
||||
},
|
||||
{
|
||||
"$type": "Document",
|
||||
"DocumentIndex": 2,
|
||||
"Title": "ServiceMain.cs",
|
||||
"DocumentMoniker": "C:\\Repos\\kmSqlReplicate\\ClassObj\\ServiceMain.cs",
|
||||
"RelativeDocumentMoniker": "ClassObj\\ServiceMain.cs",
|
||||
"ToolTip": "C:\\Repos\\kmSqlReplicate\\ClassObj\\ServiceMain.cs",
|
||||
"RelativeToolTip": "ClassObj\\ServiceMain.cs",
|
||||
"ViewState": "AgIAAAAAAAAAAAAAAAAAABIAAAADAAAAAAAAAA==",
|
||||
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
|
||||
"WhenOpened": "2025-08-29T11:19:38.123Z",
|
||||
"EditorCaption": ""
|
||||
},
|
||||
{
|
||||
"$type": "Document",
|
||||
"DocumentIndex": 1,
|
||||
"Title": "Program.cs",
|
||||
"DocumentMoniker": "C:\\Repos\\kmSqlReplicate\\Program.cs",
|
||||
"RelativeDocumentMoniker": "Program.cs",
|
||||
"ToolTip": "C:\\Repos\\kmSqlReplicate\\Program.cs",
|
||||
"RelativeToolTip": "Program.cs",
|
||||
"ViewState": "AgIAABgAAAAAAAAAAAAAACEAAAALAAAAAAAAAA==",
|
||||
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
|
||||
"WhenOpened": "2025-08-29T11:13:28.917Z",
|
||||
"EditorCaption": ""
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
|
@ -0,0 +1,119 @@
|
|||
{
|
||||
"Version": 1,
|
||||
"WorkspaceRootPath": "C:\\Repos\\kmSqlReplicate\\",
|
||||
"Documents": [
|
||||
{
|
||||
"AbsoluteMoniker": "D:0:0:{364F9228-3601-4813-9E87-F042D9357580}|kmSqlReplicate.csproj|c:\\repos\\kmsqlreplicate\\classobj\\clsreplicatebase.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
|
||||
"RelativeMoniker": "D:0:0:{364F9228-3601-4813-9E87-F042D9357580}|kmSqlReplicate.csproj|solutionrelative:classobj\\clsreplicatebase.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
|
||||
},
|
||||
{
|
||||
"AbsoluteMoniker": "D:0:0:{364F9228-3601-4813-9E87-F042D9357580}|kmSqlReplicate.csproj|c:\\repos\\kmsqlreplicate\\program.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
|
||||
"RelativeMoniker": "D:0:0:{364F9228-3601-4813-9E87-F042D9357580}|kmSqlReplicate.csproj|solutionrelative:program.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
|
||||
},
|
||||
{
|
||||
"AbsoluteMoniker": "D:0:0:{364F9228-3601-4813-9E87-F042D9357580}|kmSqlReplicate.csproj|c:\\repos\\kmsqlreplicate\\classobj\\servicemain.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
|
||||
"RelativeMoniker": "D:0:0:{364F9228-3601-4813-9E87-F042D9357580}|kmSqlReplicate.csproj|solutionrelative:classobj\\servicemain.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
|
||||
}
|
||||
],
|
||||
"DocumentGroupContainers": [
|
||||
{
|
||||
"Orientation": 0,
|
||||
"VerticalTabListWidth": 256,
|
||||
"DocumentGroups": [
|
||||
{
|
||||
"DockedWidth": 200,
|
||||
"SelectedChildIndex": 12,
|
||||
"Children": [
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
"Name": "ST:128:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
|
||||
},
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
"Name": "ST:0:0:{1c4feeaa-4718-4aa9-859d-94ce25d182ba}"
|
||||
},
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
"Name": "ST:129:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
|
||||
},
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
"Name": "ST:130:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
|
||||
},
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
"Name": "ST:131:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
|
||||
},
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
"Name": "ST:132:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
|
||||
},
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
"Name": "ST:133:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
|
||||
},
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
"Name": "ST:134:0:{1fc202d4-d401-403c-9834-5b218574bb67}"
|
||||
},
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
"Name": "ST:135:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
|
||||
},
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
"Name": "ST:136:0:{1fc202d4-d401-403c-9834-5b218574bb67}"
|
||||
},
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
"Name": "ST:0:0:{aa2115a1-9712-457b-9047-dbb71ca2cdd2}"
|
||||
},
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
"Name": "ST:1:0:{e8b06f52-6d01-11d2-aa7d-00c04f990343}"
|
||||
},
|
||||
{
|
||||
"$type": "Document",
|
||||
"DocumentIndex": 0,
|
||||
"Title": "ClsReplicateBase.cs",
|
||||
"DocumentMoniker": "C:\\Repos\\kmSqlReplicate\\ClassObj\\ClsReplicateBase.cs",
|
||||
"RelativeDocumentMoniker": "ClassObj\\ClsReplicateBase.cs",
|
||||
"ToolTip": "C:\\Repos\\kmSqlReplicate\\ClassObj\\ClsReplicateBase.cs",
|
||||
"RelativeToolTip": "ClassObj\\ClsReplicateBase.cs",
|
||||
"ViewState": "AgIAAF0AAAAAAAAAAAAAAHkAAABiAAAAAAAAAA==",
|
||||
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
|
||||
"WhenOpened": "2025-08-29T11:21:32.713Z",
|
||||
"EditorCaption": ""
|
||||
},
|
||||
{
|
||||
"$type": "Document",
|
||||
"DocumentIndex": 2,
|
||||
"Title": "ServiceMain.cs",
|
||||
"DocumentMoniker": "C:\\Repos\\kmSqlReplicate\\ClassObj\\ServiceMain.cs",
|
||||
"RelativeDocumentMoniker": "ClassObj\\ServiceMain.cs",
|
||||
"ToolTip": "C:\\Repos\\kmSqlReplicate\\ClassObj\\ServiceMain.cs",
|
||||
"RelativeToolTip": "ClassObj\\ServiceMain.cs",
|
||||
"ViewState": "AgIAAAAAAAAAAAAAAAAAABIAAAADAAAAAAAAAA==",
|
||||
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
|
||||
"WhenOpened": "2025-08-29T11:19:38.123Z",
|
||||
"EditorCaption": ""
|
||||
},
|
||||
{
|
||||
"$type": "Document",
|
||||
"DocumentIndex": 1,
|
||||
"Title": "Program.cs",
|
||||
"DocumentMoniker": "C:\\Repos\\kmSqlReplicate\\Program.cs",
|
||||
"RelativeDocumentMoniker": "Program.cs",
|
||||
"ToolTip": "C:\\Repos\\kmSqlReplicate\\Program.cs",
|
||||
"RelativeToolTip": "Program.cs",
|
||||
"ViewState": "AgIAABgAAAAAAAAAAAAAACEAAAALAAAAAAAAAA==",
|
||||
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
|
||||
"WhenOpened": "2025-08-29T11:13:28.917Z",
|
||||
"EditorCaption": ""
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
|
@ -0,0 +1,307 @@
|
|||
using Dapper;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.SqlServer.Management.Smo;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using System.Data;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace kmSqlReplicate.ClassObj;
|
||||
|
||||
internal class ClsReplicateBase
|
||||
{
|
||||
|
||||
internal void Go()
|
||||
{
|
||||
var rn = new Random();
|
||||
var dtNext = DateTimeOffset.Now.AddSeconds(Debugger.IsAttached ? 5 : 45);
|
||||
|
||||
while (!Program.exitNow)
|
||||
{
|
||||
if (dtNext < DateTimeOffset.Now)
|
||||
{
|
||||
ReplicateNow();
|
||||
dtNext = DateTimeOffset.Now.AddSeconds(15);
|
||||
}
|
||||
Thread.Sleep(rn.Next(750, 1500));
|
||||
}
|
||||
}
|
||||
|
||||
private void ReplicateNow()
|
||||
{
|
||||
try
|
||||
{
|
||||
var tbMain2 = InitWorkTable();
|
||||
|
||||
using (var cnRep = new SqlConnection(kmCommonLibsCore.Constants.cnSysReplication))
|
||||
{
|
||||
var lst = cnRep.Query("dbo.[GetReplicationJobs]", commandType: CommandType.StoredProcedure).ToList();
|
||||
|
||||
cnRep.Open();
|
||||
foreach (var itm1 in lst)
|
||||
{
|
||||
string cnString = string.Format("Server={0};Database={1};Integrated Security=SSPI;TrustServerCertificate=True;",
|
||||
itm1.FromServer.ToString(), itm1.FromDatabase.ToString());
|
||||
string cmText = "Select *, Schema_Name(Schema_ID) + '.' + [name] [TableAndSchemaName], " +
|
||||
"@@SERVERNAME [ServerName], DB_Name() [DatabaseName], " +
|
||||
"Schema_Name(Schema_ID) [SchemaName], [name] [TableName] " +
|
||||
"From sys.Tables Where [type_desc]='USER_TABLE'";
|
||||
|
||||
using (var cn2 = new SqlConnection(cnString))
|
||||
{
|
||||
var lst2 = cn2.Query(cmText).ToList();
|
||||
|
||||
foreach (var itm2 in lst2)
|
||||
{
|
||||
if (Regex.IsMatch(itm2.TableAndSchemaName, itm1.FromTable, RegexOptions.IgnoreCase))
|
||||
{
|
||||
var drx = tbMain2.NewRow();
|
||||
drx["repMasterID"] = Convert.ToInt64(itm1.repMasterID);
|
||||
drx["ServerName"] = itm2.ServerName.ToString();
|
||||
drx["DatabaseName"] = itm2.DatabaseName.ToString();
|
||||
drx["SchemaName"] = itm2.SchemaName.ToString();
|
||||
drx["TableName"] = itm2.TableName.ToString();
|
||||
tbMain2.Rows.Add(drx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
ScriptingOptions scrOptions1, scrOptions2, scrOptions3;
|
||||
|
||||
scrOptions1 = new ScriptingOptions() // Drop and Create
|
||||
{
|
||||
Default = true,
|
||||
NoIdentities = true,
|
||||
DriAll = false,
|
||||
ScriptDrops = false,
|
||||
ScriptOwner = false,
|
||||
NoFileGroup = false,
|
||||
NoIndexPartitioningSchemes = true,
|
||||
NoTablePartitioningSchemes = true,
|
||||
ScriptForCreateDrop = true
|
||||
};
|
||||
scrOptions2 = new ScriptingOptions() // Get all keys and indexes
|
||||
{
|
||||
Default = false,
|
||||
DriPrimaryKey = true,
|
||||
Indexes = true,
|
||||
ScriptOwner = false,
|
||||
NoFileGroup = false,
|
||||
NoIndexPartitioningSchemes = true,
|
||||
NoTablePartitioningSchemes = true,
|
||||
ScriptForCreateDrop = true
|
||||
};
|
||||
scrOptions3 = new ScriptingOptions() // Get the permissions
|
||||
{
|
||||
Default = false,
|
||||
Permissions = true
|
||||
};
|
||||
|
||||
using (var cnSrc = new SqlConnection(string.Format("Server={0};Database={1};Integrated Security=SSPI;TrustServerCertificate=True;", itm1.FromServer.ToString(), itm1.FromDatabase.ToString())))
|
||||
{
|
||||
cnSrc.Open();
|
||||
|
||||
Server S1 = new Server(new Microsoft.SqlServer.Management.Common.ServerConnection(cnSrc));
|
||||
Database D1 = S1.Databases[itm1.FromDatabase.ToString()];
|
||||
|
||||
foreach (DataRow dr in tbMain2.Rows)
|
||||
{
|
||||
Table T1;
|
||||
var scrOutput = new StringCollection();
|
||||
var txtSql = new StringBuilder();
|
||||
|
||||
T1 = D1.Tables[dr["TableName"].ToString(), dr["SchemaName"].ToString()];
|
||||
txtSql.AppendFormat("DROP TABLE IF EXISTS [{0}].[{1}]\nGO\n\n", dr["SchemaName"].ToString(), dr["TableName"].ToString());
|
||||
txtSql.Append(string.Join("\n", T1.Script(scrOptions1).Cast<string>().ToArray()));
|
||||
txtSql.AppendLine("\nGO\n");
|
||||
txtSql.Append(string.Join("\n", T1.Script(scrOptions2).Cast<string>().ToArray()));
|
||||
txtSql.AppendLine("\nGO\n");
|
||||
dr["Sql"] = txtSql.ToString();
|
||||
|
||||
//
|
||||
|
||||
using (var cnDst = new SqlConnection(string.Format("Server={0};Database={1};Integrated Security=SSPI;TrustServerCertificate=True;", itm1.ToServer.ToString(), itm1.ToDatabase.ToString())))
|
||||
{
|
||||
string sqlTablePermissions = string.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
cnDst.Open();
|
||||
Server S2 = new Server(new Microsoft.SqlServer.Management.Common.ServerConnection(cnDst));
|
||||
Database D2 = S2.Databases[itm1.ToDatabase.ToString()];
|
||||
if (D2.Tables.Contains(dr["TableName"].ToString(), dr["SchemaName"].ToString()))
|
||||
{
|
||||
Table T2 = D2.Tables[dr["TableName"].ToString(), dr["SchemaName"].ToString()];
|
||||
sqlTablePermissions = string.Join("\n", T2.Script(scrOptions3).Cast<string>().ToArray());
|
||||
}
|
||||
|
||||
S2.ConnectionContext.BatchSeparator = "GO";
|
||||
S2.ConnectionContext.BeginTransaction();
|
||||
S2.ConnectionContext.ExecuteNonQuery(txtSql.ToString());
|
||||
S2.ConnectionContext.CommitTransaction();
|
||||
}
|
||||
catch (Exception ex2)
|
||||
{
|
||||
kmCommonLibsCore.ClsErrorReporting.ErrorEncountered(ex2, string.Format("Inside901 - {0}.{1}.{2}.{3}\n\n{4}",
|
||||
itm1.ToServer.ToString(), itm1.ToDatabase.ToString(), dr["SchemaName"].ToString(), dr["TableName"].ToString(),
|
||||
Regex.Replace(txtSql.ToString(), "(\r\n|\n\r|\r|\n)", "<br />")));
|
||||
sqlTablePermissions = string.Empty;
|
||||
}
|
||||
|
||||
using (var t1 = cnDst.BeginTransaction())
|
||||
using (var bc1 = new SqlBulkCopy(cnDst, SqlBulkCopyOptions.KeepIdentity, t1))
|
||||
{
|
||||
bc1.ColumnMappings.Clear();
|
||||
foreach (Column c1 in T1.Columns)
|
||||
{
|
||||
if (c1.Computed == false)
|
||||
bc1.ColumnMappings.Add(c1.Name, c1.Name);
|
||||
}
|
||||
|
||||
if (bc1.ColumnMappings.Count > 0)
|
||||
{
|
||||
using (var cm0 = new SqlCommand(string.Format("Select Count(*) Cnt From [{0}].[{1}]", dr["SchemaName"].ToString(), dr["TableName"].ToString()), cnDst) { CommandTimeout = 0 })
|
||||
using (var cm1 = new SqlCommand(string.Format("Select * From [{0}].[{1}]", dr["SchemaName"].ToString(), dr["TableName"].ToString()), cnSrc) { CommandTimeout = 0 })
|
||||
using (var rd1 = cm1.ExecuteReader())
|
||||
{
|
||||
bc1.DestinationTableName = string.Format("[{0}].[{1}]", dr["SchemaName"].ToString(), dr["TableName"].ToString());
|
||||
bc1.BulkCopyTimeout = 21600;
|
||||
try
|
||||
{
|
||||
var sw1 = Stopwatch.StartNew();
|
||||
bc1.WriteToServer(rd1);
|
||||
t1.Commit();
|
||||
sw1.Stop();
|
||||
dr["Replicated"] = DateTimeOffset.Now;
|
||||
dr["RowsCopied"] = Convert.ToInt64(cm0.ExecuteScalar());
|
||||
dr["Elapsed"] = string.Format(@"{0:h\:mm\:ss\.fff}", sw1.Elapsed);
|
||||
}
|
||||
catch (Exception ex2)
|
||||
{
|
||||
kmCommonLibsCore.ClsErrorReporting.ErrorEncountered(ex2, "Inside905");
|
||||
t1.Rollback();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
using (var t1 = cnRep.BeginTransaction())
|
||||
using (var bc1 = new SqlBulkCopy(cnRep, SqlBulkCopyOptions.KeepIdentity, t1))
|
||||
{
|
||||
bc1.ColumnMappings.Clear();
|
||||
foreach (DataColumn c1 in tbMain2.Columns)
|
||||
bc1.ColumnMappings.Add(c1.ColumnName, c1.ColumnName);
|
||||
|
||||
bc1.DestinationTableName = "[dbo].[tbMain2]";
|
||||
bc1.BulkCopyTimeout = 21600;
|
||||
bc1.WriteToServer(tbMain2);
|
||||
t1.Commit();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex1)
|
||||
{
|
||||
kmCommonLibsCore.ClsErrorReporting.ErrorEncountered(ex1, "Outside300");
|
||||
}
|
||||
|
||||
AdvanceToNextTime(itm1);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
kmCommonLibsCore.ClsErrorReporting.ErrorEncountered(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void AdvanceToNextTime(dynamic itm)
|
||||
{
|
||||
try
|
||||
{
|
||||
string IntervalTypePattern = "^(day|hour|minute|week|month)";
|
||||
string IntervalType = string.Empty;
|
||||
string IntervalNumberPattern = @"([0-9.]{1,})+";
|
||||
double IntervalNumber = 0;
|
||||
|
||||
if (Regex.IsMatch(itm.Frequency, IntervalTypePattern))
|
||||
{
|
||||
IntervalType = Regex.Match(itm.Frequency, IntervalTypePattern).Value.ToString().ToLower();
|
||||
}
|
||||
|
||||
var m = Regex.Match(itm.Frequency, IntervalNumberPattern);
|
||||
foreach (Capture mg in m.Groups[1].Captures)
|
||||
{
|
||||
if (double.TryParse(mg.Value, out IntervalNumber))
|
||||
break;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(IntervalType) && IntervalNumber > 0)
|
||||
{
|
||||
var dtNextExecution = (DateTimeOffset)itm.dtNextExecution;
|
||||
|
||||
while (dtNextExecution < DateTimeOffset.Now)
|
||||
{
|
||||
switch (IntervalType)
|
||||
{
|
||||
case "day":
|
||||
case "days":
|
||||
dtNextExecution=dtNextExecution.AddDays(IntervalNumber); break;
|
||||
case "hour":
|
||||
case "hours":
|
||||
dtNextExecution = dtNextExecution.AddHours(IntervalNumber); break;
|
||||
case "minute":
|
||||
case "minutes":
|
||||
dtNextExecution = dtNextExecution.AddMinutes(IntervalNumber); break;
|
||||
case "week":
|
||||
case "weeks":
|
||||
dtNextExecution = dtNextExecution.AddDays(IntervalNumber * 7f); break;
|
||||
case "month":
|
||||
case "months":
|
||||
dtNextExecution = dtNextExecution.AddMonths(Convert.ToInt32(Math.Truncate(IntervalNumber))); break;
|
||||
default:
|
||||
dtNextExecution = DateTimeOffset.Now.AddMinutes(30); break;
|
||||
}
|
||||
}
|
||||
using (var cn = new SqlConnection(kmCommonLibsCore.Constants.cnSysReplication))
|
||||
{
|
||||
cn.Execute("Update [dbo].[tbMain1] Set [dtNextExecution]=@dtNextExecution, [dtLastExecution]=SysDateTimeOffset() Where [repMasterID]=@repMasterID",
|
||||
new
|
||||
{
|
||||
repMasterID = Convert.ToInt64(itm.repMasterID),
|
||||
dtNextExecution = dtNextExecution
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
kmCommonLibsCore.ClsErrorReporting.ErrorEncountered(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private DataTable InitWorkTable()
|
||||
{
|
||||
var dt = new DataTable();
|
||||
|
||||
dt.Columns.Add("repMasterID", typeof(Int64));
|
||||
dt.Columns.Add("ServerName", typeof(String));
|
||||
dt.Columns.Add("DatabaseName", typeof(String));
|
||||
dt.Columns.Add("SchemaName", typeof(String));
|
||||
dt.Columns.Add("TableName", typeof(String));
|
||||
dt.Columns.Add("RowsCopied", typeof(Int64));
|
||||
dt.Columns.Add("Sql", typeof(String));
|
||||
dt.Columns.Add("Replicated", typeof(DateTimeOffset));
|
||||
dt.Columns.Add("Elapsed", typeof(String));
|
||||
|
||||
return dt;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
using log4net;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Topshelf;
|
||||
|
||||
namespace kmSqlReplicate.ClassObj;
|
||||
|
||||
internal class ServiceMain : ServiceControl
|
||||
{
|
||||
private ConcurrentBag<Task> _tasks = new();
|
||||
internal ILog appLog = LogManager.GetLogger(Program.AppName);
|
||||
|
||||
|
||||
public bool Start(HostControl hostControl)
|
||||
{
|
||||
_tasks.Add(Task.Run(() =>
|
||||
{
|
||||
var z = new ClassObj.ClsReplicateBase();
|
||||
z.Go();
|
||||
}));
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Stop(HostControl hostControl)
|
||||
{
|
||||
Program.exitNow = true;
|
||||
Task.WaitAll(_tasks.ToArray());
|
||||
Console.WriteLine("I've Successfully Killed All Tasks!");
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,189 @@
|
|||
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)
|
||||
{
|
||||
var obj = new ClsReplicateBase();
|
||||
obj.Go();
|
||||
//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()
|
||||
{
|
||||
ScriptingOptions scrOptions1, scrOptions2, scrOptions3, scrOptions4;
|
||||
|
||||
scrOptions1 = new ScriptingOptions() // Drops first
|
||||
{
|
||||
Default = true,
|
||||
//NoIdentities = true,
|
||||
ScriptDrops = true,
|
||||
//ScriptOwner = true,
|
||||
//NoFileGroup = false,
|
||||
//NoIndexPartitioningSchemes = true,
|
||||
//NoTablePartitioningSchemes = true,
|
||||
ScriptForCreateDrop = true,
|
||||
DriAllConstraints = true,
|
||||
//DriDefaults = true,
|
||||
DriForeignKeys = true,
|
||||
IncludeIfNotExists = true,
|
||||
//EnforceScriptingOptions = true,
|
||||
//AllowSystemObjects = false,
|
||||
//Indexes = true,
|
||||
DriAllKeys = true,
|
||||
DriChecks = true,
|
||||
//FullTextIndexes = true,
|
||||
//Statistics = true,
|
||||
//Triggers = true,
|
||||
//WithDependencies = true,
|
||||
DriAll = true,
|
||||
};
|
||||
scrOptions2 = new ScriptingOptions() // Then CREATE
|
||||
{
|
||||
Default = true,
|
||||
NoIdentities = false,
|
||||
ScriptDrops = false,
|
||||
ScriptOwner = false,
|
||||
NoFileGroup = false,
|
||||
NoIndexPartitioningSchemes = true,
|
||||
NoTablePartitioningSchemes = true,
|
||||
//ScriptForCreateDrop = true,
|
||||
//Statistics = true,
|
||||
//Triggers = true,
|
||||
};
|
||||
scrOptions3 = new ScriptingOptions() // Get all keys and indexes
|
||||
{
|
||||
Default = false,
|
||||
DriPrimaryKey = true,
|
||||
Indexes = true,
|
||||
ScriptOwner = false,
|
||||
NoFileGroup = false,
|
||||
NoIndexPartitioningSchemes = true,
|
||||
NoTablePartitioningSchemes = true,
|
||||
ScriptForCreateDrop = true,
|
||||
FullTextIndexes = true,
|
||||
DriAll = true
|
||||
};
|
||||
scrOptions4 = new ScriptingOptions() // Get the permissions
|
||||
{
|
||||
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"];
|
||||
|
||||
//foreach (var fk in T1.ForeignKeys)
|
||||
// Console.WriteLine("DROP " + fk.ToString());
|
||||
|
||||
var scr = new Scripter(S1) { Options = scrOptions1 };
|
||||
//scr.Options.IncludeIfNotExists = true;
|
||||
//scr.Options.ScriptSchema = true;
|
||||
//scr.Options.ScriptDrops = true;
|
||||
//Console.WriteLine (scr.EnumScript(new Urn[] { T1.Urn }).ToString());
|
||||
|
||||
var dw = new DependencyWalker(S1);
|
||||
var dwTree = dw.DiscoverDependencies(new Urn[] { T1.Urn }, true);
|
||||
var ddd = dw.WalkDependencies(dwTree);
|
||||
|
||||
foreach (var d in ddd)
|
||||
{
|
||||
foreach (var s in scr.EnumScript(new[] { d.Urn }))
|
||||
{
|
||||
Console.WriteLine(s);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//foreach (string s in scr.EnumScript(new Urn[] { T1.Urn }))
|
||||
// Console.WriteLine(s + "\n\n");
|
||||
|
||||
Console.ReadLine();
|
||||
|
||||
if (false)
|
||||
{
|
||||
txtSql.Append(string.Join("\n", T1.Script(scrOptions1).Cast<string>().ToArray()));
|
||||
txtSql.AppendLine("\nGO\n\n\n\n");
|
||||
txtSql.Append(string.Join("\n", T1.Script(scrOptions2).Cast<string>().ToArray()));
|
||||
txtSql.AppendLine("\nGO\n\n\n\n");
|
||||
txtSql.Append(string.Join("\n", T1.Script(scrOptions3).Cast<string>().ToArray()));
|
||||
txtSql.AppendLine("\nGO\n");
|
||||
|
||||
Console.WriteLine(txtSql.ToString());
|
||||
Console.ReadLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Binary file not shown.
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue