Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

358 linhas
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 (targettables.Contains(sourcetable.Name))
  89. {
  90. ColumnCollection sourcecolumns = sourcetable.Columns;
  91. foreach (Column sourcecolumn in sourcecolumns)
  92. {
  93. //检查字段是否包含
  94. if (targettables[sourcetable.Name].Columns.Contains(sourcecolumn.Name))
  95. {
  96. //检查数据类型
  97. if (targettables[sourcetable.Name].Columns[sourcecolumn.Name].DataType.Equals(sourcecolumn.DataType))
  98. {
  99. continue;
  100. }
  101. else
  102. {
  103. //修改数据类型
  104. databasetarget.Tables[sourcetable.Name].Columns[sourcecolumn.Name].DataType = sourcecolumn.DataType;
  105. databasetarget.Tables[sourcetable.Name].Alter();
  106. }
  107. }
  108. else
  109. {
  110. Column colmiss = new Column(databasetarget.Tables[sourcetable.Name], sourcecolumn.Name, sourcecolumn.DataType);
  111. colmiss.Collation = sourcecolumn.Collation;
  112. colmiss.Nullable = sourcecolumn.Nullable;
  113. colmiss.Identity = sourcecolumn.Identity;
  114. colmiss.Default = sourcecolumn.Default;
  115. foreach (ExtendedProperty property in sourcecolumn.ExtendedProperties)
  116. {
  117. colmiss.ExtendedProperties.Add(new ExtendedProperty(colmiss,property.Name,property.Value));
  118. }
  119. databasetarget.Tables[sourcetable.Name].Columns.Add(colmiss);
  120. databasetarget.Tables[sourcetable.Name].Alter();
  121. }
  122. }
  123. }
  124. else
  125. {
  126. //初始化Scripter
  127. ScriptingOptions scriptingOptions = new ScriptingOptions();
  128. scriptingOptions.Add(ScriptOption.DriAllConstraints);
  129. scriptingOptions.Add(ScriptOption.DriAllKeys);
  130. scriptingOptions.Add(ScriptOption.Default);
  131. scriptingOptions.Add(ScriptOption.ContinueScriptingOnError);
  132. scriptingOptions.Add(ScriptOption.ConvertUserDefinedDataTypesToBaseType);
  133. scriptingOptions.Add(ScriptOption.IncludeIfNotExists);
  134. scriptingOptions.Add(ScriptOption.DriPrimaryKey);
  135. scriptingOptions.Add(ScriptOption.ExtendedProperties);
  136. StringCollection sc= sourcetable.Script(scriptingOptions);
  137. StringBuilder sb=new StringBuilder();
  138. foreach (var s in sc)
  139. {
  140. sb.Append(s+"\r\n");
  141. }
  142. //新建表
  143. mainservice.GetDataRepository("target").ExecuteBySql(sb.ToString());
  144. }
  145. }
  146. AppendTextValue("正在保存表结构,请耐心等待...\r\n");
  147. CloseWindow();
  148. }
  149. catch (Exception e)
  150. {
  151. AppendTextValue("error:" + e.Message + "\r\n");
  152. }
  153. }
  154. public void SyncMisDB()
  155. {
  156. try
  157. {
  158. //获取所有表 core
  159. MainService mainservice = new MainService();
  160. SetValueProgressBar(SetValue, 0);
  161. SetWindowTitle("正在连接数据库...");
  162. AppendTextValue("正在连接mis源数据库...\r\n");
  163. //框架源
  164. Server servercoresource = new Server(new ServerConnection(new SqlConnection(mainservice.GetDataRepository("missource").getDbConnection().ConnectionString)));
  165. AppendTextValue("正在连接mis目标数据库...\r\n");
  166. Server servercoretarget = new Server(new ServerConnection(new SqlConnection(mainservice.GetDataRepository("mistarget").getDbConnection().ConnectionString)));
  167. if (servercoresource.Status == ServerStatus.Online)
  168. {
  169. AppendTextValue("mis源数据库连接成功...\r\n");
  170. }
  171. if (servercoretarget.Status == ServerStatus.Online)
  172. {
  173. AppendTextValue("mis目标数据库连接成功...\r\n");
  174. }
  175. if (servercoresource.Status != ServerStatus.Online || servercoretarget.Status != ServerStatus.Online)
  176. {
  177. AppendTextValue("数据库连接失败,请检查连接字符串或网络环境后重试!\r\n");
  178. return;
  179. }
  180. Database databasesource = servercoresource.Databases[mainservice.GetDataRepository("missource").getDbConnection().Database];
  181. Database databasetarget = servercoretarget.Databases[mainservice.GetDataRepository("mistarget").getDbConnection().Database];
  182. TableCollection sourcetables = databasesource.Tables;
  183. TableCollection targettables = databasetarget.Tables;
  184. SetWindowTitle("正在同步mis表结构...");
  185. AppendTextValue("正在同步mis表结构...\r\n");
  186. AppendTextValue("本次同步mis表结构共" + sourcetables.Count + "个表...\r\n");
  187. SetProgressMax(sourcetables.Count);
  188. foreach (Table sourcetable in sourcetables)
  189. {
  190. AppendTextValue("正在检测" + sourcetable.Name + "表结构...\r\n");
  191. SetValueProgressBar(SetValue, GetProgressValue() + 1);
  192. if (targettables.Contains(sourcetable.Name))
  193. {
  194. ColumnCollection sourcecolumns = sourcetable.Columns;
  195. foreach (Column sourcecolumn in sourcecolumns)
  196. {
  197. //检查字段是否包含
  198. if (targettables[sourcetable.Name].Columns.Contains(sourcecolumn.Name))
  199. {
  200. //检查数据类型
  201. if (targettables[sourcetable.Name].Columns[sourcecolumn.Name].DataType.Equals(sourcecolumn.DataType))
  202. {
  203. continue;
  204. }
  205. else
  206. {
  207. //修改数据类型
  208. databasetarget.Tables[sourcetable.Name].Columns[sourcecolumn.Name].DataType = sourcecolumn.DataType;
  209. databasetarget.Tables[sourcetable.Name].Alter();
  210. }
  211. }
  212. else
  213. {
  214. Column colmiss = new Column(databasetarget.Tables[sourcetable.Name], sourcecolumn.Name, sourcecolumn.DataType);
  215. colmiss.Collation = sourcecolumn.Collation;
  216. colmiss.Nullable = sourcecolumn.Nullable;
  217. colmiss.Identity = sourcecolumn.Identity;
  218. colmiss.Default = sourcecolumn.Default;
  219. foreach (ExtendedProperty property in sourcecolumn.ExtendedProperties)
  220. {
  221. colmiss.ExtendedProperties.Add(new ExtendedProperty(colmiss, property.Name, property.Value));
  222. }
  223. databasetarget.Tables[sourcetable.Name].Columns.Add(colmiss);
  224. databasetarget.Tables[sourcetable.Name].Alter();
  225. }
  226. }
  227. }
  228. else
  229. {
  230. //初始化Scripter
  231. ScriptingOptions scriptingOptions = new ScriptingOptions();
  232. scriptingOptions.Add(ScriptOption.DriAllConstraints);
  233. scriptingOptions.Add(ScriptOption.DriAllKeys);
  234. scriptingOptions.Add(ScriptOption.Default);
  235. scriptingOptions.Add(ScriptOption.ContinueScriptingOnError);
  236. scriptingOptions.Add(ScriptOption.ConvertUserDefinedDataTypesToBaseType);
  237. scriptingOptions.Add(ScriptOption.IncludeIfNotExists);
  238. scriptingOptions.Add(ScriptOption.DriPrimaryKey);
  239. scriptingOptions.Add(ScriptOption.ExtendedProperties);
  240. StringCollection sc = sourcetable.Script(scriptingOptions);
  241. StringBuilder sb = new StringBuilder();
  242. foreach (var s in sc)
  243. {
  244. sb.Append(s + "\r\n");
  245. }
  246. //新建表
  247. mainservice.GetDataRepository("mistarget").ExecuteBySql(sb.ToString());
  248. }
  249. }
  250. AppendTextValue("正在保存表结构,请耐心等待...\r\n");
  251. CloseWindow();
  252. }
  253. catch (Exception e)
  254. {
  255. AppendTextValue("error:" + e.Message + "\r\n");
  256. }
  257. }
  258. #region 委托
  259. public delegate void AppendTextValueDelegate(string text);
  260. public delegate void SetValueDelegate(int val);
  261. public delegate void CloseWindowDelegate();
  262. public delegate void SetWindowTitleDelegate(string text);
  263. public delegate int GetProgressValueDelegate();
  264. public delegate void SetProgressMaxDelegate(int max);
  265. public void AppendTextValue(string text)
  266. {
  267. if (this.rtblog.InvokeRequired)
  268. {
  269. AppendTextValueDelegate callback = new AppendTextValueDelegate(AppendTextValue);
  270. this.Invoke(callback, text);
  271. }
  272. else
  273. {
  274. rtblog.AppendText(text);
  275. }
  276. }
  277. public void SetProgressMax(int max)
  278. {
  279. if (this.progressBar1.InvokeRequired)
  280. {
  281. SetProgressMaxDelegate callback = new SetProgressMaxDelegate(SetProgressMax);
  282. this.Invoke(callback, max);
  283. }
  284. else
  285. {
  286. progressBar1.Maximum = max;
  287. }
  288. }
  289. public int GetProgressValue()
  290. {
  291. if (this.progressBar1.InvokeRequired)
  292. {
  293. GetProgressValueDelegate callback = new GetProgressValueDelegate(GetProgressValue);
  294. return Convert.ToInt32(this.Invoke(callback));
  295. }
  296. else
  297. {
  298. return progressBar1.Value;
  299. }
  300. }
  301. public void SetWindowTitle(string text)
  302. {
  303. if (this.InvokeRequired)
  304. {
  305. SetWindowTitleDelegate callback = new SetWindowTitleDelegate(SetWindowTitle);
  306. this.Invoke(callback, text);
  307. }
  308. else
  309. {
  310. this.Text = text;
  311. }
  312. }
  313. public void CloseWindow()
  314. {
  315. if (this.InvokeRequired)
  316. {
  317. CloseWindowDelegate callback = new CloseWindowDelegate(CloseWindow);
  318. this.Invoke(callback);
  319. }
  320. else
  321. {
  322. this.Close();
  323. }
  324. }
  325. public void SetValue(int val)
  326. {
  327. progressBar1.Value = val;
  328. }
  329. public void SetValueProgressBar(SetValueDelegate myDelegate, int val)
  330. {
  331. if (this.progressBar1.InvokeRequired)
  332. {
  333. this.Invoke(myDelegate, val);
  334. }
  335. else
  336. {
  337. myDelegate(val);
  338. }
  339. }
  340. #endregion
  341. private void btcancelsync_Click(object sender, EventArgs e)
  342. {
  343. CloseWindow();
  344. }
  345. }
  346. }