You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

368 line
16 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. using Microsoft.SqlServer.Management.Common;
  14. using Microsoft.SqlServer.Management.Smo;
  15. namespace Quanjiang.DigitalScholl.DataSync
  16. {
  17. public partial class SyncWindow : Form
  18. {
  19. public SyncWindow()
  20. {
  21. InitializeComponent();
  22. }
  23. string _synctype = "";
  24. Thread thread = null;
  25. int max = 0;
  26. public SyncWindow(string synctype)
  27. {
  28. _synctype = synctype;
  29. InitializeComponent();
  30. }
  31. private void SyncWindow_Load(object sender, EventArgs e)
  32. {
  33. this.ControlBox = false;
  34. progressBar1.Minimum = 0;
  35. progressBar1.Maximum = max;
  36. progressBar1.Value = 0;
  37. if (_synctype == "core")
  38. {
  39. ThreadStart start = new ThreadStart(SyncCoreDB);
  40. thread = new Thread(start);
  41. thread.Start();
  42. }
  43. if (_synctype == "mis")
  44. {
  45. ThreadStart start = new ThreadStart(SyncMisDB);
  46. thread = new Thread(start);
  47. thread.Start();
  48. }
  49. }
  50. public void SyncCoreDB()
  51. {
  52. try
  53. {
  54. //获取所有表 core
  55. MainService mainservice = new MainService();
  56. SetValueProgressBar(SetValue, 0);
  57. SetWindowTitle("正在连接数据库...");
  58. AppendTextValue("正在连接框架源数据库...\r\n");
  59. //框架源
  60. Server servercoresource = new Server(new ServerConnection(new SqlConnection(mainservice.GetDataRepository("source").getDbConnection().ConnectionString)));
  61. AppendTextValue("正在连接框架目标数据库...\r\n");
  62. Server servercoretarget = new Server(new ServerConnection(new SqlConnection(mainservice.GetDataRepository("target").getDbConnection().ConnectionString)));
  63. if (servercoresource.Status == ServerStatus.Online)
  64. {
  65. AppendTextValue("框架源数据库连接成功...\r\n");
  66. }
  67. if (servercoretarget.Status == ServerStatus.Online)
  68. {
  69. AppendTextValue("框架目标数据库连接成功...\r\n");
  70. }
  71. if (servercoresource.Status != ServerStatus.Online || servercoretarget.Status != ServerStatus.Online)
  72. {
  73. AppendTextValue("数据库连接失败,请检查连接字符串或网络环境后重试!\r\n");
  74. return;
  75. }
  76. Database databasesource = servercoresource.Databases[mainservice.GetDataRepository("source").getDbConnection().Database];
  77. Database databasetarget = servercoretarget.Databases[mainservice.GetDataRepository("target").getDbConnection().Database];
  78. TableCollection sourcetables = databasesource.Tables;
  79. TableCollection targettables = databasetarget.Tables;
  80. SetWindowTitle("正在同步框架表结构...");
  81. AppendTextValue("正在同步框架表结构...\r\n");
  82. AppendTextValue("本次同步框架表结构共" + sourcetables.Count + "个表...\r\n");
  83. SetProgressMax(sourcetables.Count);
  84. foreach (Table sourcetable in sourcetables)
  85. {
  86. AppendTextValue("正在检测" + sourcetable.Name + "表结构...\r\n");
  87. SetValueProgressBar(SetValue, GetProgressValue()+1);
  88. if (Program.beginTable != "")
  89. {
  90. if(sourcetable.Name!=Program.beginTable)
  91. continue;
  92. }
  93. if (targettables.Contains(sourcetable.Name))
  94. {
  95. ColumnCollection sourcecolumns = sourcetable.Columns;
  96. foreach (Column sourcecolumn in sourcecolumns)
  97. {
  98. //检查字段是否包含
  99. if (targettables[sourcetable.Name].Columns.Contains(sourcecolumn.Name))
  100. {
  101. //检查数据类型
  102. if (targettables[sourcetable.Name].Columns[sourcecolumn.Name].DataType.Equals(sourcecolumn.DataType))
  103. {
  104. continue;
  105. }
  106. else
  107. {
  108. //修改数据类型
  109. databasetarget.Tables[sourcetable.Name].Columns[sourcecolumn.Name].DataType = sourcecolumn.DataType;
  110. databasetarget.Tables[sourcetable.Name].Alter();
  111. }
  112. }
  113. else
  114. {
  115. Column colmiss = new Column(databasetarget.Tables[sourcetable.Name], sourcecolumn.Name, sourcecolumn.DataType);
  116. colmiss.Collation = sourcecolumn.Collation;
  117. colmiss.Nullable = sourcecolumn.Nullable;
  118. colmiss.Identity = sourcecolumn.Identity;
  119. colmiss.Default = sourcecolumn.Default;
  120. foreach (ExtendedProperty property in sourcecolumn.ExtendedProperties)
  121. {
  122. colmiss.ExtendedProperties.Add(new ExtendedProperty(colmiss,property.Name,property.Value));
  123. }
  124. databasetarget.Tables[sourcetable.Name].Columns.Add(colmiss);
  125. databasetarget.Tables[sourcetable.Name].Alter();
  126. }
  127. }
  128. }
  129. else
  130. {
  131. //初始化Scripter
  132. ScriptingOptions scriptingOptions = new ScriptingOptions();
  133. scriptingOptions.Add(ScriptOption.DriAllConstraints);
  134. scriptingOptions.Add(ScriptOption.DriAllKeys);
  135. scriptingOptions.Add(ScriptOption.Default);
  136. scriptingOptions.Add(ScriptOption.ContinueScriptingOnError);
  137. scriptingOptions.Add(ScriptOption.ConvertUserDefinedDataTypesToBaseType);
  138. scriptingOptions.Add(ScriptOption.IncludeIfNotExists);
  139. scriptingOptions.Add(ScriptOption.DriPrimaryKey);
  140. scriptingOptions.Add(ScriptOption.ExtendedProperties);
  141. StringCollection sc= sourcetable.Script(scriptingOptions);
  142. StringBuilder sb=new StringBuilder();
  143. foreach (var s in sc)
  144. {
  145. sb.Append(s+"\r\n");
  146. }
  147. //新建表
  148. mainservice.GetDataRepository("target").ExecuteBySql(sb.ToString());
  149. }
  150. }
  151. AppendTextValue("正在保存表结构,请耐心等待...\r\n");
  152. CloseWindow();
  153. }
  154. catch (Exception e)
  155. {
  156. AppendTextValue("error:" + e.Message + "\r\n");
  157. }
  158. }
  159. public void SyncMisDB()
  160. {
  161. try
  162. {
  163. //获取所有表 core
  164. MainService mainservice = new MainService();
  165. SetValueProgressBar(SetValue, 0);
  166. SetWindowTitle("正在连接数据库...");
  167. AppendTextValue("正在连接mis源数据库...\r\n");
  168. //框架源
  169. Server servercoresource = new Server(new ServerConnection(new SqlConnection(mainservice.GetDataRepository("missource").getDbConnection().ConnectionString)));
  170. AppendTextValue("正在连接mis目标数据库...\r\n");
  171. Server servercoretarget = new Server(new ServerConnection(new SqlConnection(mainservice.GetDataRepository("mistarget").getDbConnection().ConnectionString)));
  172. if (servercoresource.Status == ServerStatus.Online)
  173. {
  174. AppendTextValue("mis源数据库连接成功...\r\n");
  175. }
  176. if (servercoretarget.Status == ServerStatus.Online)
  177. {
  178. AppendTextValue("mis目标数据库连接成功...\r\n");
  179. }
  180. if (servercoresource.Status != ServerStatus.Online || servercoretarget.Status != ServerStatus.Online)
  181. {
  182. AppendTextValue("数据库连接失败,请检查连接字符串或网络环境后重试!\r\n");
  183. return;
  184. }
  185. Database databasesource = servercoresource.Databases[mainservice.GetDataRepository("missource").getDbConnection().Database];
  186. Database databasetarget = servercoretarget.Databases[mainservice.GetDataRepository("mistarget").getDbConnection().Database];
  187. TableCollection sourcetables = databasesource.Tables;
  188. TableCollection targettables = databasetarget.Tables;
  189. SetWindowTitle("正在同步mis表结构...");
  190. AppendTextValue("正在同步mis表结构...\r\n");
  191. AppendTextValue("本次同步mis表结构共" + sourcetables.Count + "个表...\r\n");
  192. SetProgressMax(sourcetables.Count);
  193. foreach (Table sourcetable in sourcetables)
  194. {
  195. AppendTextValue("正在检测" + sourcetable.Name + "表结构...\r\n");
  196. SetValueProgressBar(SetValue, GetProgressValue() + 1);
  197. if (Program.beginTable != "")
  198. {
  199. if (sourcetable.Name != Program.beginTable)
  200. continue;
  201. }
  202. if (targettables.Contains(sourcetable.Name))
  203. {
  204. ColumnCollection sourcecolumns = sourcetable.Columns;
  205. foreach (Column sourcecolumn in sourcecolumns)
  206. {
  207. //检查字段是否包含
  208. if (targettables[sourcetable.Name].Columns.Contains(sourcecolumn.Name))
  209. {
  210. //检查数据类型
  211. if (targettables[sourcetable.Name].Columns[sourcecolumn.Name].DataType.Equals(sourcecolumn.DataType))
  212. {
  213. continue;
  214. }
  215. else
  216. {
  217. //修改数据类型
  218. databasetarget.Tables[sourcetable.Name].Columns[sourcecolumn.Name].DataType = sourcecolumn.DataType;
  219. databasetarget.Tables[sourcetable.Name].Alter();
  220. }
  221. }
  222. else
  223. {
  224. Column colmiss = new Column(databasetarget.Tables[sourcetable.Name], sourcecolumn.Name, sourcecolumn.DataType);
  225. colmiss.Collation = sourcecolumn.Collation;
  226. colmiss.Nullable = sourcecolumn.Nullable;
  227. colmiss.Identity = sourcecolumn.Identity;
  228. colmiss.Default = sourcecolumn.Default;
  229. foreach (ExtendedProperty property in sourcecolumn.ExtendedProperties)
  230. {
  231. colmiss.ExtendedProperties.Add(new ExtendedProperty(colmiss, property.Name, property.Value));
  232. }
  233. databasetarget.Tables[sourcetable.Name].Columns.Add(colmiss);
  234. databasetarget.Tables[sourcetable.Name].Alter();
  235. }
  236. }
  237. }
  238. else
  239. {
  240. //初始化Scripter
  241. ScriptingOptions scriptingOptions = new ScriptingOptions();
  242. scriptingOptions.Add(ScriptOption.DriAllConstraints);
  243. scriptingOptions.Add(ScriptOption.DriAllKeys);
  244. scriptingOptions.Add(ScriptOption.Default);
  245. scriptingOptions.Add(ScriptOption.ContinueScriptingOnError);
  246. scriptingOptions.Add(ScriptOption.ConvertUserDefinedDataTypesToBaseType);
  247. scriptingOptions.Add(ScriptOption.IncludeIfNotExists);
  248. scriptingOptions.Add(ScriptOption.DriPrimaryKey);
  249. scriptingOptions.Add(ScriptOption.ExtendedProperties);
  250. StringCollection sc = sourcetable.Script(scriptingOptions);
  251. StringBuilder sb = new StringBuilder();
  252. foreach (var s in sc)
  253. {
  254. sb.Append(s + "\r\n");
  255. }
  256. //新建表
  257. mainservice.GetDataRepository("mistarget").ExecuteBySql(sb.ToString());
  258. }
  259. }
  260. AppendTextValue("正在保存表结构,请耐心等待...\r\n");
  261. CloseWindow();
  262. }
  263. catch (Exception e)
  264. {
  265. AppendTextValue("error:" + e.Message + "\r\n");
  266. }
  267. }
  268. #region 委托
  269. public delegate void AppendTextValueDelegate(string text);
  270. public delegate void SetValueDelegate(int val);
  271. public delegate void CloseWindowDelegate();
  272. public delegate void SetWindowTitleDelegate(string text);
  273. public delegate int GetProgressValueDelegate();
  274. public delegate void SetProgressMaxDelegate(int max);
  275. public void AppendTextValue(string text)
  276. {
  277. if (this.rtblog.InvokeRequired)
  278. {
  279. AppendTextValueDelegate callback = new AppendTextValueDelegate(AppendTextValue);
  280. this.Invoke(callback, text);
  281. }
  282. else
  283. {
  284. rtblog.AppendText(text);
  285. }
  286. }
  287. public void SetProgressMax(int max)
  288. {
  289. if (this.progressBar1.InvokeRequired)
  290. {
  291. SetProgressMaxDelegate callback = new SetProgressMaxDelegate(SetProgressMax);
  292. this.Invoke(callback, max);
  293. }
  294. else
  295. {
  296. progressBar1.Maximum = max;
  297. }
  298. }
  299. public int GetProgressValue()
  300. {
  301. if (this.progressBar1.InvokeRequired)
  302. {
  303. GetProgressValueDelegate callback = new GetProgressValueDelegate(GetProgressValue);
  304. return Convert.ToInt32(this.Invoke(callback));
  305. }
  306. else
  307. {
  308. return progressBar1.Value;
  309. }
  310. }
  311. public void SetWindowTitle(string text)
  312. {
  313. if (this.InvokeRequired)
  314. {
  315. SetWindowTitleDelegate callback = new SetWindowTitleDelegate(SetWindowTitle);
  316. this.Invoke(callback, text);
  317. }
  318. else
  319. {
  320. this.Text = text;
  321. }
  322. }
  323. public void CloseWindow()
  324. {
  325. if (this.InvokeRequired)
  326. {
  327. CloseWindowDelegate callback = new CloseWindowDelegate(CloseWindow);
  328. this.Invoke(callback);
  329. }
  330. else
  331. {
  332. this.Close();
  333. }
  334. }
  335. public void SetValue(int val)
  336. {
  337. progressBar1.Value = val;
  338. }
  339. public void SetValueProgressBar(SetValueDelegate myDelegate, int val)
  340. {
  341. if (this.progressBar1.InvokeRequired)
  342. {
  343. this.Invoke(myDelegate, val);
  344. }
  345. else
  346. {
  347. myDelegate(val);
  348. }
  349. }
  350. #endregion
  351. private void btcancelsync_Click(object sender, EventArgs e)
  352. {
  353. CloseWindow();
  354. }
  355. }
  356. }