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.

648 lines
22 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using ManagementSystem.Utility;
  12. namespace ExportDataToFile
  13. {
  14. public partial class Form1 : Form
  15. {
  16. //參數設定
  17. int intExportIndex = -1;
  18. SqlConnection sqlSourceConn = new SqlConnection();
  19. SqlConnection sqlTargetConn = new SqlConnection();
  20. public Form1()
  21. {
  22. InitializeComponent();
  23. }
  24. private void btnSourceConnTest_Click(object sender, EventArgs e)
  25. {
  26. try
  27. {
  28. string strCheck = CheckSource();
  29. //查詢來源資料庫中的資料表
  30. string strIP = txtSourceIP.Text.Trim();
  31. string strDBName = txtSourceDBName.Text.Trim();
  32. string strID = txtSourceID.Text.Trim();
  33. string strPWD = txtSourcePWD.Text.Trim();
  34. if (strCheck == "")
  35. {
  36. if (cbSourceClass.Enabled)
  37. {
  38. LockSourceForm();
  39. switch (cbSourceClass.SelectedItem.ToString().Trim())
  40. {
  41. case "MS-SQL":
  42. sqlSourceConn = MSSQLUtility.GetConn(strIP, strDBName, strID, strPWD);
  43. ShowMSSQLSourceTables(sqlSourceConn);
  44. Application.DoEvents();
  45. break;
  46. case "MySQL":
  47. break;
  48. case "Oracle":
  49. break;
  50. case "PostgreSQL":
  51. break;
  52. }
  53. }
  54. else
  55. {
  56. UnLockSourceForm();
  57. }
  58. }
  59. else
  60. {
  61. MessageBox.Show(strCheck);
  62. }
  63. }
  64. catch (Exception ex)
  65. {
  66. ErrorHandler.WriteErrorLog("Form1.cs", ex);
  67. }
  68. }
  69. private void btnTargetConnTest_Click(object sender, EventArgs e)
  70. {
  71. try
  72. {
  73. string strCheck = CheckTarget();
  74. //查詢目標資料庫中的資料表
  75. string strIP = txtTargetIP.Text.Trim();
  76. string strDBName = txtTargetDBName.Text.Trim();
  77. string strID = txtTargetID.Text.Trim();
  78. string strPWD = txtTargetPWD.Text.Trim();
  79. if (strCheck == "")
  80. {
  81. if (cbTargetClass.Enabled)
  82. {
  83. switch (cbTargetClass.SelectedItem.ToString().Trim())
  84. {
  85. case "MS-SQL":
  86. sqlTargetConn = MSSQLUtility.GetConn(strIP, strDBName, strID, strPWD);
  87. ShowMSSQLTargetTableList(sqlTargetConn);
  88. Application.DoEvents();
  89. break;
  90. case "MySQL":
  91. break;
  92. case "Oracle":
  93. break;
  94. case "PostgreSQL":
  95. break;
  96. }
  97. LockTargetForm();
  98. }
  99. else
  100. {
  101. UnLockTargetForm();
  102. }
  103. }
  104. else
  105. {
  106. MessageBox.Show(strCheck);
  107. }
  108. }
  109. catch (Exception ex)
  110. {
  111. ErrorHandler.WriteErrorLog("Form1.cs", ex);
  112. }
  113. }
  114. private void dgvExportList_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  115. {
  116. //try
  117. //{
  118. // dgvExportList.Rows[e.RowIndex].Cells["clExport"].Value = true;
  119. //}
  120. //catch (Exception ex)
  121. //{
  122. // ErrorHandler.WriteErrorLog("Form1.cs", ex);
  123. //}
  124. }
  125. private void dgvExportList_CellClick(object sender, DataGridViewCellEventArgs e)
  126. {
  127. try
  128. {
  129. LocatedTarget(e.RowIndex);
  130. }
  131. catch (Exception ex)
  132. {
  133. ErrorHandler.WriteErrorLog("Form1.cs", ex);
  134. }
  135. }
  136. private void dgvExportList_CellContentClick(object sender, DataGridViewCellEventArgs e)
  137. {
  138. try
  139. {
  140. var senderGrid = (DataGridView)sender;
  141. if (e.RowIndex >= 0)
  142. {
  143. //清除該列資料
  144. if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn)
  145. {
  146. CleanExpRow(dgvExportList, e.RowIndex);
  147. }
  148. }
  149. }
  150. catch (Exception ex)
  151. {
  152. ErrorHandler.WriteErrorLog("Form1.cs", ex);
  153. }
  154. }
  155. private void txtWhere_Leave(object sender, EventArgs e)
  156. {
  157. try
  158. {
  159. dgvExportList.Rows[intExportIndex].Cells["clWhere"].Value = txtWhere.Text.Trim();
  160. }
  161. catch (Exception ex)
  162. {
  163. ErrorHandler.WriteErrorLog("Form1.cs", ex);
  164. }
  165. }
  166. private void cbTargetTable_SelectedIndexChanged(object sender, EventArgs e)
  167. {
  168. try
  169. {
  170. if (cbTargetTable.SelectedValue != null && intExportIndex != -1)
  171. {
  172. //重新修改DataGridView的ComboBox的內容
  173. switch (cbTargetClass.SelectedItem.ToString().Trim())
  174. {
  175. case "MS-SQL":
  176. ShowMSSQLTargetColumnList(sqlTargetConn, cbTargetTable.SelectedValue.ToString());
  177. break;
  178. case "MySQL":
  179. break;
  180. case "Oracle":
  181. break;
  182. case "PostgreSQL":
  183. break;
  184. }
  185. Application.DoEvents();
  186. }
  187. }
  188. catch (Exception ex)
  189. {
  190. ErrorHandler.WriteErrorLog("Form1.cs", ex);
  191. }
  192. }
  193. private void dgvColumnMapping_CellContentClick(object sender, DataGridViewCellEventArgs e)
  194. {
  195. try
  196. {
  197. var senderGrid = (DataGridView)sender;
  198. //觸發清除事件
  199. if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
  200. {
  201. senderGrid.Rows[e.RowIndex].Cells["clExpColumn"].Value = false;
  202. ((DataGridViewComboBoxCell)senderGrid.Rows[e.RowIndex].Cells["clTargetColumn"]).Value = null;
  203. }
  204. }
  205. catch (Exception ex)
  206. {
  207. ErrorHandler.WriteErrorLog("Form1.cs", ex);
  208. }
  209. }
  210. private void dgvColumnMapping_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  211. {
  212. try
  213. {
  214. if (cbTargetTable.Enabled)
  215. {
  216. //dgvColumnMapping.Rows[e.RowIndex].Cells["clExpColumn"].Value = true;
  217. //dgvExportList.Rows[intExportIndex].Cells["clExport"].Value = true;
  218. }
  219. }
  220. catch (Exception ex)
  221. {
  222. ErrorHandler.WriteErrorLog("Form1.cs", ex);
  223. }
  224. }
  225. private void btnExport_Click(object sender, EventArgs e)
  226. {
  227. try
  228. {
  229. }
  230. catch (Exception ex)
  231. {
  232. ErrorHandler.WriteErrorLog("Form1.cs", ex);
  233. }
  234. }
  235. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  236. {
  237. sqlSourceConn.Close();
  238. sqlSourceConn = null;
  239. sqlTargetConn.Close();
  240. sqlTargetConn = null;
  241. }
  242. private void btnMapping_Click(object sender, EventArgs e)
  243. {
  244. //自動預設產生所有的Mapping檔案
  245. dgvExportList.Rows[intExportIndex].Cells["clMappingData"].Value = GenMappingColumn();
  246. dgvExportList.Rows[intExportIndex].Cells["clExport"].Value = true;
  247. //設定來源與目標的資料表對應
  248. if (cbTargetTable.SelectedValue != null)
  249. {
  250. dgvExportList.Rows[intExportIndex].Cells["clTargetTable"].Value = cbTargetTable.SelectedValue.ToString();
  251. }
  252. }
  253. #region 自定義功能
  254. #region MS-SQL
  255. private void ShowMSSQLSourceTables(SqlConnection sqlConn)
  256. {
  257. try
  258. {
  259. string strGetMSSQLTableList = "Select [name] as TableName from sys.tables order by name";
  260. dgvExportList.DataSource = MSSQLUtility.GetSQLResult(strGetMSSQLTableList, sqlConn).Tables["Result"];
  261. }
  262. catch (Exception ex)
  263. {
  264. ErrorHandler.WriteErrorLog("Form1.cs", ex);
  265. }
  266. }
  267. private void ShowMSSQLTargetTableList(SqlConnection sqlConn)
  268. {
  269. try
  270. {
  271. string strGetMSSQLTableList = "Select [name] as TableName from sys.tables order by name";
  272. cbTargetTable.DataSource = MSSQLUtility.GetSQLResult(strGetMSSQLTableList, sqlConn).Tables["Result"];
  273. }
  274. catch (Exception ex)
  275. {
  276. ErrorHandler.WriteErrorLog("Form1.cs", ex);
  277. }
  278. }
  279. private void ShowMSSQLSourceColumnList(SqlConnection sqlConn, string strTableName)
  280. {
  281. try
  282. {
  283. string strGetMSSQLColumnList = "Select COLUMN_NAME From INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME ='" + strTableName + "' Order by ORDINAL_POSITION";
  284. dgvColumnMapping.DataSource = MSSQLUtility.GetSQLResult(strGetMSSQLColumnList, sqlConn).Tables["Result"];
  285. }
  286. catch (Exception ex)
  287. {
  288. ErrorHandler.WriteErrorLog("Form1.cs", ex);
  289. }
  290. }
  291. private void ShowMSSQLTargetColumnList(SqlConnection sqlConn, string strTableName)
  292. {
  293. try
  294. {
  295. string strGetMSSQLColumnList = "Select COLUMN_NAME From INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME ='" + strTableName + "' Order by ORDINAL_POSITION";
  296. this.clTargetColumn.DisplayMember = "COLUMN_NAME";
  297. this.clTargetColumn.DataSource = MSSQLUtility.GetSQLResult(strGetMSSQLColumnList, sqlConn).Tables["Result"];
  298. Application.DoEvents();
  299. }
  300. catch (Exception ex)
  301. {
  302. ErrorHandler.WriteErrorLog("Form1.cs", ex);
  303. }
  304. }
  305. #endregion //End of MS-SQL
  306. private string CheckSource()
  307. {
  308. try
  309. {
  310. string strResult = "";
  311. if (cbSourceClass.SelectedItem == null)
  312. {
  313. strResult = "請選擇來源資料庫類別";
  314. cbSourceClass.Focus();
  315. return strResult;
  316. }
  317. else
  318. {
  319. if (txtSourceIP.Text.Trim() == "")
  320. {
  321. strResult = "來源資料庫IP為必填";
  322. txtSourceIP.Focus();
  323. return strResult;
  324. }
  325. if (txtSourceDBName.Text.Trim() == "")
  326. {
  327. strResult = "來源資料庫名稱為必填";
  328. txtSourceDBName.Focus();
  329. return strResult;
  330. }
  331. if (txtSourceID.Text.Trim() == "")
  332. {
  333. strResult = "來源資料庫帳號為必填";
  334. txtSourceID.Focus();
  335. return strResult;
  336. }
  337. }
  338. return strResult;
  339. }
  340. catch (Exception ex)
  341. {
  342. ErrorHandler.WriteErrorLog("Form1.cs", ex);
  343. return "發生不明錯誤!";
  344. }
  345. }
  346. private string CheckTarget()
  347. {
  348. try
  349. {
  350. string strResult = "";
  351. if (cbTargetClass.SelectedItem != null)
  352. {
  353. if (txtTargetIP.Text.Trim() == "")
  354. {
  355. strResult = "目標資料庫IP為必填";
  356. txtTargetIP.Focus();
  357. return strResult;
  358. }
  359. if (txtTargetDBName.Text.Trim() == "")
  360. {
  361. strResult = "目標資料庫名稱為必填";
  362. txtTargetDBName.Focus();
  363. return strResult;
  364. }
  365. if (txtTargetID.Text.Trim() == "")
  366. {
  367. strResult = "目標資料庫帳號為必填";
  368. txtTargetID.Focus();
  369. return strResult;
  370. }
  371. }
  372. return strResult;
  373. }
  374. catch (Exception ex)
  375. {
  376. ErrorHandler.WriteErrorLog("Form1.cs", ex);
  377. return "發生不明錯誤!";
  378. }
  379. }
  380. private void LockSourceForm()
  381. {
  382. try
  383. {
  384. cbSourceClass.Enabled = false;
  385. txtSourceIP.Enabled = false;
  386. txtSourceID.Enabled = false;
  387. txtSourceDBName.Enabled = false;
  388. txtSourcePWD.Enabled = false;
  389. btnSourceConnTest.Text = "連線中…";
  390. }
  391. catch (Exception ex)
  392. {
  393. ErrorHandler.WriteErrorLog("Form1.cs", ex);
  394. }
  395. }
  396. private void UnLockSourceForm()
  397. {
  398. try
  399. {
  400. cbSourceClass.Enabled = true;
  401. txtSourceIP.Enabled = true;
  402. txtSourceID.Enabled = true;
  403. txtSourceDBName.Enabled = true;
  404. txtSourcePWD.Enabled = true;
  405. btnSourceConnTest.Text = "建立來源連線";
  406. }
  407. catch (Exception ex)
  408. {
  409. ErrorHandler.WriteErrorLog("Form1.cs", ex);
  410. }
  411. }
  412. private void LockTargetForm()
  413. {
  414. try
  415. {
  416. cbTargetClass.Enabled = false;
  417. cbTargetTable.Enabled = true;
  418. cbTargetTable.SelectedIndex = -1;
  419. txtTargetIP.Enabled = false;
  420. txtTargetID.Enabled = false;
  421. txtTargetDBName.Enabled = false;
  422. txtTargetPWD.Enabled = false;
  423. btnTargetConnTest.Text = "連線中…";
  424. }
  425. catch (Exception ex)
  426. {
  427. ErrorHandler.WriteErrorLog("Form1.cs", ex);
  428. }
  429. }
  430. private void UnLockTargetForm()
  431. {
  432. try
  433. {
  434. cbTargetClass.Enabled = true;
  435. cbTargetTable.DataSource = null;
  436. cbTargetTable.Enabled = false;
  437. txtTargetIP.Enabled = true;
  438. txtTargetID.Enabled = true;
  439. txtTargetDBName.Enabled = true;
  440. txtTargetPWD.Enabled = true;
  441. btnTargetConnTest.Text = "建立目標連線";
  442. }
  443. catch (Exception ex)
  444. {
  445. ErrorHandler.WriteErrorLog("Form1.cs", ex);
  446. }
  447. }
  448. private string GenMappingColumn()
  449. {
  450. string strExportColumns = "";
  451. string strSourceColumn = "";
  452. string strTargetColumn = "";
  453. //預設所有欄位均匯出
  454. foreach (DataGridViewRow dgRow in dgvColumnMapping.Rows)
  455. {
  456. //處理下拉欄位空白的問題
  457. if (dgRow.Cells["clExpColumn"].Value != null)
  458. {
  459. if ((bool)dgRow.Cells["clExpColumn"].Value == true)
  460. {
  461. if (dgRow.Cells["clSourceColumn"].Value == null)
  462. {
  463. strSourceColumn = " ";
  464. }
  465. else
  466. {
  467. strSourceColumn = dgRow.Cells["clSourceColumn"].Value.ToString();
  468. }
  469. if (dgRow.Cells["clTargetColumn"].Value == null)
  470. {
  471. strTargetColumn = " ";
  472. }
  473. else
  474. {
  475. strTargetColumn = dgRow.Cells["clTargetColumn"].Value.ToString();
  476. }
  477. if (strExportColumns == "")
  478. {
  479. strExportColumns += strSourceColumn + "," + strTargetColumn;
  480. }
  481. else
  482. {
  483. strExportColumns += "|" + strSourceColumn + "," + strTargetColumn;
  484. }
  485. }
  486. }
  487. }
  488. return strExportColumns;
  489. }
  490. private void LocatedTarget(int intRowIndex)
  491. {
  492. if (cbTargetClass.SelectedIndex >= 0 && cbTargetClass.Enabled == false)
  493. {
  494. //cbTargetTable.SelectedValue = dgvExportList.Rows[intRowIndex].Cells["clSourceTable"].Value.ToString();
  495. string strTarget = "";
  496. string[] strColumnMapping = null;
  497. if (intRowIndex != -1)
  498. {
  499. intExportIndex = intRowIndex;
  500. //設定目標Table下拉式選單的內容
  501. if (dgvExportList.Rows[intRowIndex].Cells["clTargetTable"].Value != null && dgvExportList.Rows[intRowIndex].Cells["clTargetTable"].Value.ToString() != "")
  502. {
  503. strTarget = dgvExportList.Rows[intRowIndex].Cells["clTargetTable"].Value.ToString();
  504. }
  505. else
  506. {
  507. strTarget = dgvExportList.Rows[intRowIndex].Cells["clSourceTable"].Value.ToString();
  508. }
  509. cbTargetTable.SelectedValue = strTarget;
  510. //取得欄位對應字串陣列
  511. if (dgvExportList.Rows[intRowIndex].Cells["clMappingData"].Value != null)
  512. {
  513. strColumnMapping = dgvExportList.Rows[intRowIndex].Cells["clMappingData"].Value.ToString().Split('|');
  514. }
  515. //顯示欄位對應表的內容
  516. if (cbTargetTable.SelectedValue != null)
  517. {
  518. switch (cbTargetClass.SelectedItem.ToString().Trim())
  519. {
  520. case "MS-SQL":
  521. ShowMSSQLSourceColumnList(sqlSourceConn, strTarget.ToString());
  522. Application.DoEvents();
  523. break;
  524. case "MySQL":
  525. break;
  526. case "Oracle":
  527. break;
  528. case "PostgreSQL":
  529. break;
  530. }
  531. }
  532. if (strColumnMapping != null)
  533. {
  534. string strSourceColumn = "";
  535. string strTargetColumn = "";
  536. foreach (string strColumn in strColumnMapping)
  537. {
  538. strSourceColumn = strColumn.Substring(0, strColumn.IndexOf(',')).Trim();
  539. strTargetColumn = strColumn.Substring(strColumn.IndexOf(',') + 1).Trim();
  540. foreach (DataGridViewRow dgvRow in dgvColumnMapping.Rows)
  541. {
  542. if (dgvRow.Cells["clSourceColumn"].Value.ToString() == strSourceColumn)
  543. {
  544. dgvRow.Cells[0].Value = true;
  545. }
  546. }
  547. }
  548. }
  549. else
  550. {
  551. //預設所有欄位均匯出
  552. foreach (DataGridViewRow dgRow in dgvColumnMapping.Rows)
  553. {
  554. ((DataGridViewCheckBoxCell)dgRow.Cells["clExpColumn"]).Value = true;
  555. }
  556. }
  557. }
  558. }
  559. }
  560. private void CleanExpRow(DataGridView senderGrid, int intRowIndex)
  561. {
  562. senderGrid.Rows[intRowIndex].Cells["clExport"].Value = false;
  563. senderGrid.Rows[intRowIndex].Cells["clTargetTable"].Value = "";
  564. senderGrid.Rows[intRowIndex].Cells["clTableDel"].Value = false;
  565. senderGrid.Rows[intRowIndex].Cells["clDataTrim"].Value = false;
  566. senderGrid.Rows[intRowIndex].Cells["clMaxRecord"].Value = "";
  567. senderGrid.Rows[intRowIndex].Cells["clWhere"].Value = "";
  568. senderGrid.Rows[intRowIndex].Cells["clMappingData"].Value = "";
  569. CleanMappingRow(senderGrid, intRowIndex);
  570. txtWhere.Text = "";
  571. Application.DoEvents();
  572. }
  573. private void CleanMappingRow(DataGridView senderGrid,int intRowIndex)
  574. {
  575. DataTable CleanTable = (DataTable)dgvColumnMapping.DataSource;
  576. CleanTable.Rows.Clear();
  577. dgvColumnMapping.DataSource = CleanTable;
  578. }
  579. #endregion
  580. }
  581. }