|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- using System;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.ComponentModel;
- using System.Data;
- using System.Data.SqlClient;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using Microsoft.SqlServer.Management.Common;
- using Microsoft.SqlServer.Management.Smo;
-
- namespace Quanjiang.DigitalScholl.DataSync
- {
- public partial class SyncWindow : Form
- {
- public SyncWindow()
- {
- InitializeComponent();
- }
- string _synctype = "";
- Thread thread = null;
- int max = 0;
- public SyncWindow(string synctype)
- {
- _synctype = synctype;
- InitializeComponent();
- }
-
- private void SyncWindow_Load(object sender, EventArgs e)
- {
- this.ControlBox = false;
- progressBar1.Minimum = 0;
- progressBar1.Maximum = max;
- progressBar1.Value = 0;
- if (_synctype == "core")
- {
- ThreadStart start = new ThreadStart(SyncCoreDB);
- thread = new Thread(start);
- thread.Start();
- }
- if (_synctype == "mis")
- {
- ThreadStart start = new ThreadStart(SyncMisDB);
- thread = new Thread(start);
- thread.Start();
- }
- }
-
- public void SyncCoreDB()
- {
- try
- {
- //获取所有表 core
- MainService mainservice = new MainService();
- SetValueProgressBar(SetValue, 0);
- SetWindowTitle("正在连接数据库...");
- AppendTextValue("正在连接框架源数据库...\r\n");
- //框架源
- Server servercoresource = new Server(new ServerConnection(new SqlConnection(mainservice.GetDataRepository("source").getDbConnection().ConnectionString)));
- AppendTextValue("正在连接框架目标数据库...\r\n");
- Server servercoretarget = new Server(new ServerConnection(new SqlConnection(mainservice.GetDataRepository("target").getDbConnection().ConnectionString)));
- if (servercoresource.Status == ServerStatus.Online)
- {
- AppendTextValue("框架源数据库连接成功...\r\n");
- }
- if (servercoretarget.Status == ServerStatus.Online)
- {
- AppendTextValue("框架目标数据库连接成功...\r\n");
- }
- if (servercoresource.Status != ServerStatus.Online || servercoretarget.Status != ServerStatus.Online)
- {
- AppendTextValue("数据库连接失败,请检查连接字符串或网络环境后重试!\r\n");
- return;
- }
- Database databasesource = servercoresource.Databases[mainservice.GetDataRepository("source").getDbConnection().Database];
- Database databasetarget = servercoretarget.Databases[mainservice.GetDataRepository("target").getDbConnection().Database];
- TableCollection sourcetables = databasesource.Tables;
- TableCollection targettables = databasetarget.Tables;
- SetWindowTitle("正在同步框架表结构...");
- AppendTextValue("正在同步框架表结构...\r\n");
- AppendTextValue("本次同步框架表结构共" + sourcetables.Count + "个表...\r\n");
- SetProgressMax(sourcetables.Count);
- foreach (Table sourcetable in sourcetables)
- {
- AppendTextValue("正在检测" + sourcetable.Name + "表结构...\r\n");
- SetValueProgressBar(SetValue, GetProgressValue()+1);
- if (Program.beginTable != "")
- {
- if(sourcetable.Name!=Program.beginTable)
- continue;
- }
- if (targettables.Contains(sourcetable.Name))
- {
- ColumnCollection sourcecolumns = sourcetable.Columns;
- foreach (Column sourcecolumn in sourcecolumns)
- {
- //检查字段是否包含
- if (targettables[sourcetable.Name].Columns.Contains(sourcecolumn.Name))
- {
- //检查数据类型
- if (targettables[sourcetable.Name].Columns[sourcecolumn.Name].DataType.Equals(sourcecolumn.DataType))
- {
- continue;
- }
- else
- {
- //修改数据类型
- databasetarget.Tables[sourcetable.Name].Columns[sourcecolumn.Name].DataType = sourcecolumn.DataType;
- databasetarget.Tables[sourcetable.Name].Alter();
- }
- }
- else
- {
- Column colmiss = new Column(databasetarget.Tables[sourcetable.Name], sourcecolumn.Name, sourcecolumn.DataType);
- colmiss.Collation = sourcecolumn.Collation;
- colmiss.Nullable = sourcecolumn.Nullable;
- colmiss.Identity = sourcecolumn.Identity;
- colmiss.Default = sourcecolumn.Default;
- foreach (ExtendedProperty property in sourcecolumn.ExtendedProperties)
- {
- colmiss.ExtendedProperties.Add(new ExtendedProperty(colmiss,property.Name,property.Value));
- }
- databasetarget.Tables[sourcetable.Name].Columns.Add(colmiss);
- databasetarget.Tables[sourcetable.Name].Alter();
- }
- }
-
- }
- else
- {
- //初始化Scripter
- ScriptingOptions scriptingOptions = new ScriptingOptions();
- scriptingOptions.Add(ScriptOption.DriAllConstraints);
- scriptingOptions.Add(ScriptOption.DriAllKeys);
- scriptingOptions.Add(ScriptOption.Default);
- scriptingOptions.Add(ScriptOption.ContinueScriptingOnError);
- scriptingOptions.Add(ScriptOption.ConvertUserDefinedDataTypesToBaseType);
- scriptingOptions.Add(ScriptOption.IncludeIfNotExists);
- scriptingOptions.Add(ScriptOption.DriPrimaryKey);
- scriptingOptions.Add(ScriptOption.ExtendedProperties);
- StringCollection sc= sourcetable.Script(scriptingOptions);
- StringBuilder sb=new StringBuilder();
- foreach (var s in sc)
- {
- sb.Append(s+"\r\n");
- }
- //新建表
- mainservice.GetDataRepository("target").ExecuteBySql(sb.ToString());
- }
- }
- AppendTextValue("正在保存表结构,请耐心等待...\r\n");
- CloseWindow();
- }
- catch (Exception e)
- {
- AppendTextValue("error:" + e.Message + "\r\n");
- }
-
- }
- public void SyncMisDB()
- {
- try
- {
- //获取所有表 core
- MainService mainservice = new MainService();
- SetValueProgressBar(SetValue, 0);
- SetWindowTitle("正在连接数据库...");
- AppendTextValue("正在连接mis源数据库...\r\n");
- //框架源
- Server servercoresource = new Server(new ServerConnection(new SqlConnection(mainservice.GetDataRepository("missource").getDbConnection().ConnectionString)));
- AppendTextValue("正在连接mis目标数据库...\r\n");
- Server servercoretarget = new Server(new ServerConnection(new SqlConnection(mainservice.GetDataRepository("mistarget").getDbConnection().ConnectionString)));
- if (servercoresource.Status == ServerStatus.Online)
- {
- AppendTextValue("mis源数据库连接成功...\r\n");
- }
- if (servercoretarget.Status == ServerStatus.Online)
- {
- AppendTextValue("mis目标数据库连接成功...\r\n");
- }
- if (servercoresource.Status != ServerStatus.Online || servercoretarget.Status != ServerStatus.Online)
- {
- AppendTextValue("数据库连接失败,请检查连接字符串或网络环境后重试!\r\n");
- return;
- }
- Database databasesource = servercoresource.Databases[mainservice.GetDataRepository("missource").getDbConnection().Database];
- Database databasetarget = servercoretarget.Databases[mainservice.GetDataRepository("mistarget").getDbConnection().Database];
- TableCollection sourcetables = databasesource.Tables;
- TableCollection targettables = databasetarget.Tables;
- SetWindowTitle("正在同步mis表结构...");
- AppendTextValue("正在同步mis表结构...\r\n");
- AppendTextValue("本次同步mis表结构共" + sourcetables.Count + "个表...\r\n");
- SetProgressMax(sourcetables.Count);
- foreach (Table sourcetable in sourcetables)
- {
- AppendTextValue("正在检测" + sourcetable.Name + "表结构...\r\n");
- SetValueProgressBar(SetValue, GetProgressValue() + 1);
- if (Program.beginTable != "")
- {
- if (sourcetable.Name != Program.beginTable)
- continue;
- }
- if (targettables.Contains(sourcetable.Name))
- {
- ColumnCollection sourcecolumns = sourcetable.Columns;
- foreach (Column sourcecolumn in sourcecolumns)
- {
- //检查字段是否包含
- if (targettables[sourcetable.Name].Columns.Contains(sourcecolumn.Name))
- {
- //检查数据类型
- if (targettables[sourcetable.Name].Columns[sourcecolumn.Name].DataType.Equals(sourcecolumn.DataType))
- {
- continue;
- }
- else
- {
- //修改数据类型
- databasetarget.Tables[sourcetable.Name].Columns[sourcecolumn.Name].DataType = sourcecolumn.DataType;
- databasetarget.Tables[sourcetable.Name].Alter();
- }
- }
- else
- {
- Column colmiss = new Column(databasetarget.Tables[sourcetable.Name], sourcecolumn.Name, sourcecolumn.DataType);
- colmiss.Collation = sourcecolumn.Collation;
- colmiss.Nullable = sourcecolumn.Nullable;
- colmiss.Identity = sourcecolumn.Identity;
- colmiss.Default = sourcecolumn.Default;
- foreach (ExtendedProperty property in sourcecolumn.ExtendedProperties)
- {
- colmiss.ExtendedProperties.Add(new ExtendedProperty(colmiss, property.Name, property.Value));
- }
- databasetarget.Tables[sourcetable.Name].Columns.Add(colmiss);
- databasetarget.Tables[sourcetable.Name].Alter();
- }
- }
-
- }
- else
- {
- //初始化Scripter
- ScriptingOptions scriptingOptions = new ScriptingOptions();
- scriptingOptions.Add(ScriptOption.DriAllConstraints);
- scriptingOptions.Add(ScriptOption.DriAllKeys);
- scriptingOptions.Add(ScriptOption.Default);
- scriptingOptions.Add(ScriptOption.ContinueScriptingOnError);
- scriptingOptions.Add(ScriptOption.ConvertUserDefinedDataTypesToBaseType);
- scriptingOptions.Add(ScriptOption.IncludeIfNotExists);
- scriptingOptions.Add(ScriptOption.DriPrimaryKey);
- scriptingOptions.Add(ScriptOption.ExtendedProperties);
- StringCollection sc = sourcetable.Script(scriptingOptions);
- StringBuilder sb = new StringBuilder();
- foreach (var s in sc)
- {
- sb.Append(s + "\r\n");
- }
- //新建表
- mainservice.GetDataRepository("mistarget").ExecuteBySql(sb.ToString());
- }
- }
- AppendTextValue("正在保存表结构,请耐心等待...\r\n");
- CloseWindow();
- }
- catch (Exception e)
- {
- AppendTextValue("error:" + e.Message + "\r\n");
- }
- }
-
- #region 委托
-
- public delegate void AppendTextValueDelegate(string text);
- public delegate void SetValueDelegate(int val);
- public delegate void CloseWindowDelegate();
- public delegate void SetWindowTitleDelegate(string text);
- public delegate int GetProgressValueDelegate();
- public delegate void SetProgressMaxDelegate(int max);
- public void AppendTextValue(string text)
- {
- if (this.rtblog.InvokeRequired)
- {
- AppendTextValueDelegate callback = new AppendTextValueDelegate(AppendTextValue);
- this.Invoke(callback, text);
- }
- else
- {
- rtblog.AppendText(text);
-
- }
- }
- public void SetProgressMax(int max)
- {
- if (this.progressBar1.InvokeRequired)
- {
- SetProgressMaxDelegate callback = new SetProgressMaxDelegate(SetProgressMax);
- this.Invoke(callback, max);
- }
- else
- {
- progressBar1.Maximum = max;
- }
- }
- public int GetProgressValue()
- {
- if (this.progressBar1.InvokeRequired)
- {
- GetProgressValueDelegate callback = new GetProgressValueDelegate(GetProgressValue);
- return Convert.ToInt32(this.Invoke(callback));
- }
- else
- {
- return progressBar1.Value;
- }
- }
- public void SetWindowTitle(string text)
- {
- if (this.InvokeRequired)
- {
- SetWindowTitleDelegate callback = new SetWindowTitleDelegate(SetWindowTitle);
- this.Invoke(callback, text);
- }
- else
- {
- this.Text = text;
- }
- }
- public void CloseWindow()
- {
- if (this.InvokeRequired)
- {
- CloseWindowDelegate callback = new CloseWindowDelegate(CloseWindow);
- this.Invoke(callback);
- }
- else
- {
- this.Close();
- }
-
- }
- public void SetValue(int val)
- {
- progressBar1.Value = val;
- }
- public void SetValueProgressBar(SetValueDelegate myDelegate, int val)
- {
- if (this.progressBar1.InvokeRequired)
- {
- this.Invoke(myDelegate, val);
- }
- else
- {
- myDelegate(val);
- }
- }
- #endregion
-
- private void btcancelsync_Click(object sender, EventArgs e)
- {
- CloseWindow();
- }
- }
- }
|