Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

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