CNAS取数仪器端升级
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.

260 line
12KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. using System.Xml.Serialization;
  11. namespace CNAS_DBSync
  12. {
  13. public delegate void InstrumentItemDataHanlder(Dictionary<string,DataTable> dictInstrument);
  14. public partial class frmDatabaseParams : Form
  15. {
  16. public SyncInstrumentItemInfo syncInstrumentItem = new SyncInstrumentItemInfo();
  17. public Dictionary<string, DataTable> dictInstrument = new Dictionary<string, DataTable>();
  18. public DataBaseInfo dataBaseInfo = new DataBaseInfo();
  19. public InstumentCodeHanlder InstrumentDelegate;
  20. public InstrumentItemDataHanlder InstrumentItemData;
  21. public frmDatabaseParams(SyncInstrumentItemInfo syncInstrumentItem)
  22. {
  23. InitializeComponent();
  24. this.syncInstrumentItem = syncInstrumentItem;
  25. }
  26. private void frmDatabaseParams_Load(object sender, EventArgs e)
  27. {
  28. if (syncInstrumentItem.SyncInstrumentDSInfo == null)
  29. syncInstrumentItem.SyncInstrumentDSInfo = new InstrumentDataSourceInfo();
  30. //如果有旧值,加载旧的值
  31. switch (syncInstrumentItem.SyncInstrumentDSInfo.InstrumentDataSourceType)
  32. {
  33. case DataSourceType.Access:
  34. this.txtAccessPath.Text = syncInstrumentItem.SyncInstrumentDSInfo.LocalPath;
  35. this.txtAccessUser.Text = syncInstrumentItem.SyncInstrumentDSInfo.UserId;
  36. this.txtAceessPwd.Text = syncInstrumentItem.SyncInstrumentDSInfo.UserPwd;
  37. break;
  38. case DataSourceType.Excel:
  39. this.txtInportExcel.Text = syncInstrumentItem.SyncInstrumentDSInfo.LocalPath;
  40. break;
  41. case DataSourceType.SQLLite:
  42. this.txtSqlitePath.Text= syncInstrumentItem.SyncInstrumentDSInfo.LocalPath;
  43. this.txtSqliteUser.Text = syncInstrumentItem.SyncInstrumentDSInfo.UserId;
  44. this.txtSqlitePwd.Text = syncInstrumentItem.SyncInstrumentDSInfo.UserPwd;
  45. break;
  46. default:
  47. break;
  48. }
  49. //加载旧的CNAS数据库信息
  50. if (syncInstrumentItem.SyncTargetDBInfo == null)
  51. syncInstrumentItem.SyncTargetDBInfo = new DataBaseInfo();
  52. this.txtDBHost.Text = syncInstrumentItem.SyncTargetDBInfo.DBHost;
  53. this.txtDBName.Text = syncInstrumentItem.SyncTargetDBInfo.DBName;
  54. this.txtDBUser.Text = syncInstrumentItem.SyncTargetDBInfo.DBUser;
  55. this.txtDBPwd.Text = syncInstrumentItem.SyncTargetDBInfo.DBPwd;
  56. this.txtPort.Text= syncInstrumentItem.SyncTargetDBInfo.DBPort;
  57. }
  58. private void btnOK_Click(object sender, EventArgs e)
  59. {
  60. //点击确定按钮
  61. InstrumentData instrumentData = null;
  62. switch (syncInstrumentItem.SyncInstrumentDSInfo.InstrumentDataSourceType)
  63. {
  64. case DataSourceType.Access:
  65. syncInstrumentItem.SyncInstrumentDSInfo.LocalPath = this.txtAccessPath.Text.Trim();
  66. syncInstrumentItem.SyncInstrumentDSInfo.UserId = this.txtAccessUser.Text.Trim();
  67. syncInstrumentItem.SyncInstrumentDSInfo.UserPwd = this.txtAceessPwd.Text.Trim();
  68. instrumentData = InstrumentDataFact.CreateInstrumentDataSource(syncInstrumentItem.SyncInstrumentDSInfo.InstrumentDataSourceType, new object[] { syncInstrumentItem.SyncInstrumentDSInfo.LocalPath, syncInstrumentItem.SyncInstrumentDSInfo.UserId, syncInstrumentItem.SyncInstrumentDSInfo.UserPwd });
  69. dictInstrument = instrumentData.GetInstrumentData();
  70. break;
  71. case DataSourceType.SQLLite:
  72. syncInstrumentItem.SyncInstrumentDSInfo.LocalPath = this.txtSqlitePath.Text.Trim();
  73. syncInstrumentItem.SyncInstrumentDSInfo.UserId = this.txtSqliteUser.Text.Trim();
  74. syncInstrumentItem.SyncInstrumentDSInfo.UserPwd = this.txtSqlitePwd.Text.Trim();
  75. instrumentData = InstrumentDataFact.CreateInstrumentDataSource(syncInstrumentItem.SyncInstrumentDSInfo.InstrumentDataSourceType, new object[] { syncInstrumentItem.SyncInstrumentDSInfo.LocalPath, syncInstrumentItem.SyncInstrumentDSInfo.UserId, syncInstrumentItem.SyncInstrumentDSInfo.UserPwd });
  76. dictInstrument = instrumentData.GetInstrumentData();
  77. break;
  78. case DataSourceType.Excel:
  79. default:
  80. instrumentData = InstrumentDataFact.CreateInstrumentDataSource(syncInstrumentItem.SyncInstrumentDSInfo.InstrumentDataSourceType, new object[] { syncInstrumentItem.SyncInstrumentDSInfo.LocalPath });
  81. dictInstrument = instrumentData.GetInstrumentData();
  82. break;
  83. }
  84. //目标库的信息
  85. syncInstrumentItem.SyncTargetDBInfo.DBHost = this.txtDBHost.Text.Trim();
  86. syncInstrumentItem.SyncTargetDBInfo.DBName = this.txtDBName.Text.Trim();
  87. syncInstrumentItem.SyncTargetDBInfo.DBUser = this.txtDBUser.Text.Trim();
  88. syncInstrumentItem.SyncTargetDBInfo.DBPwd = this.txtDBPwd.Text.Trim();
  89. syncInstrumentItem.SyncTargetDBInfo.DBPort = this.txtPort.Text.Trim();
  90. this.InstrumentDelegate(syncInstrumentItem);
  91. this.InstrumentItemData(dictInstrument);
  92. this.Close();
  93. }
  94. private void btnOpenFileExcel_Click(object sender, EventArgs e)
  95. {
  96. if (syncInstrumentItem.Code == null) return;
  97. if (syncInstrumentItem.SyncInstrumentDSInfo == null)
  98. syncInstrumentItem.SyncInstrumentDSInfo = new InstrumentDataSourceInfo();
  99. syncInstrumentItem.SyncInstrumentDSInfo.InstrumentDataSourceType = DataSourceType.Excel;
  100. OpenFileDialog openFile = new OpenFileDialog
  101. {
  102. Filter = "Excel(*.xlsx)|*.xlsx|Excel(*.xls)|*.xls",
  103. InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
  104. Multiselect = false
  105. };
  106. if (openFile.ShowDialog() == DialogResult.Cancel) return;
  107. this.txtInportExcel.Text = syncInstrumentItem.SyncInstrumentDSInfo.LocalPath = openFile.FileName;
  108. string fileType = System.IO.Path.GetExtension(openFile.FileName);
  109. if (string.IsNullOrEmpty(fileType)) return;
  110. }
  111. private void tabcDS_Selected(object sender, TabControlEventArgs e)
  112. {
  113. //切换的时候,检查其他类型数据源是否有值,如果有值,将提示删除旧值才能配置新值
  114. if (e.TabPage == tabExcel)
  115. {
  116. //检查tabAccess页面是否为空 //检查tabSQLite页面是否为空
  117. if (!CheckAccessControl() || !CheckSQLiteControl())
  118. {
  119. if (MessageBox.Show("其他数据源存在已保存数据,继续将删除这些值,是否继续", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
  120. {
  121. this.txtAccessPath.Text = "";
  122. this.txtAccessUser.Text = "";
  123. this.txtAceessPwd.Text = "";
  124. this.txtSqliteUser.Text = "";
  125. this.txtSqlitePath.Text = "";
  126. this.txtSqlitePwd.Text = "";
  127. syncInstrumentItem.SyncInstrumentDSInfo.LocalPath = "";
  128. syncInstrumentItem.SyncInstrumentDSInfo.UserId = "";
  129. syncInstrumentItem.SyncInstrumentDSInfo.UserPwd = "";
  130. }
  131. }
  132. syncInstrumentItem.SyncInstrumentDSInfo.InstrumentDataSourceType = DataSourceType.Excel;
  133. }
  134. if (e.TabPage == tabAccess)
  135. {
  136. //检查tabAccess页面是否为空 //检查tabSQLite页面是否为空
  137. if (!CheckExcelControl() || !CheckSQLiteControl())
  138. {
  139. if (MessageBox.Show("其他数据源存在已保存数据,继续将删除这些值,是否继续", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
  140. {
  141. this.txtAccessPath.Text = "";
  142. this.txtAccessUser.Text = "";
  143. this.txtAceessPwd.Text = "";
  144. this.txtSqliteUser.Text = "";
  145. this.txtSqlitePath.Text = "";
  146. this.txtSqlitePwd.Text = "";
  147. syncInstrumentItem.SyncInstrumentDSInfo.LocalPath = "";
  148. syncInstrumentItem.SyncInstrumentDSInfo.UserId = "";
  149. syncInstrumentItem.SyncInstrumentDSInfo.UserPwd = "";
  150. }
  151. }
  152. syncInstrumentItem.SyncInstrumentDSInfo.InstrumentDataSourceType = DataSourceType.Access;
  153. }
  154. }
  155. private bool CheckExcelControl()
  156. {
  157. if (this.txtInportExcel.Text != "")
  158. return false;
  159. else
  160. return true;
  161. }
  162. private bool CheckAccessControl()
  163. {
  164. if (this.txtAccessPath.Text != "" && this.txtAccessUser.Text != "" && this.txtAceessPwd.Text != "")
  165. return false;
  166. else
  167. return true;
  168. }
  169. private bool CheckSQLiteControl()
  170. {
  171. if (this.txtSqlitePath.Text != "" && this.txtSqliteUser.Text != "" && this.txtSqlitePwd.Text != "")
  172. return false;
  173. else
  174. return true;
  175. }
  176. private void btnCNASTestLink_Click(object sender, EventArgs e)
  177. {
  178. if(CnasDataOperationFact.CnasDataOperation().TestConnect(this.txtDBHost.Text.Trim(), this.txtDBName.Text.Trim(), this.txtDBUser.Text.Trim(), this.txtDBPwd.Text.Trim(),this.txtPort.Text.Trim()))
  179. MessageBox.Show("连接成功!");
  180. else
  181. MessageBox.Show("连接失败!");
  182. }
  183. private void btnOpenFileAccess_Click(object sender, EventArgs e)
  184. {
  185. if (syncInstrumentItem.Code == null) return;
  186. if (syncInstrumentItem.SyncInstrumentDSInfo == null)
  187. syncInstrumentItem.SyncInstrumentDSInfo = new InstrumentDataSourceInfo();
  188. syncInstrumentItem.SyncInstrumentDSInfo.InstrumentDataSourceType = DataSourceType.Access;
  189. OpenFileDialog openFile = new OpenFileDialog
  190. {
  191. Filter = "Microsoft Access(*.mdb)|*.mdb",
  192. InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
  193. Multiselect = false
  194. };
  195. if (openFile.ShowDialog() == DialogResult.Cancel) return;
  196. this.txtAccessPath.Text = syncInstrumentItem.SyncInstrumentDSInfo.LocalPath = openFile.FileName;
  197. string fileType = System.IO.Path.GetExtension(openFile.FileName);
  198. if (string.IsNullOrEmpty(fileType)) return;
  199. }
  200. private void btnOpenFileSQLite_Click(object sender, EventArgs e)
  201. {
  202. if (syncInstrumentItem.Code == null) return;
  203. if (syncInstrumentItem.SyncInstrumentDSInfo == null)
  204. syncInstrumentItem.SyncInstrumentDSInfo = new InstrumentDataSourceInfo();
  205. syncInstrumentItem.SyncInstrumentDSInfo.InstrumentDataSourceType = DataSourceType.SQLLite;
  206. OpenFileDialog openFile = new OpenFileDialog
  207. {
  208. Filter = "SQLite(*.db)|*.db",
  209. InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
  210. Multiselect = false
  211. };
  212. if (openFile.ShowDialog() == DialogResult.Cancel) return;
  213. this.txtSqlitePath.Text = syncInstrumentItem.SyncInstrumentDSInfo.LocalPath = openFile.FileName;
  214. string fileType = System.IO.Path.GetExtension(openFile.FileName);
  215. if (string.IsNullOrEmpty(fileType)) return;
  216. }
  217. }
  218. }