BaseUtil.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. using UAS_LabelMachine.CustomControl;
  8. using UAS_LabelMachine.CustomControl.GroupBoxWithBorder;
  9. using static System.Windows.Forms.Control;
  10. using System.Text.RegularExpressions;
  11. using UAS_LabelMachine.CustomControl.RichText;
  12. using System.IO;
  13. using System.Media;
  14. namespace UAS_LabelMachine
  15. {
  16. class BaseUtil
  17. {
  18. static string SysDisc;
  19. public static void CompactAccessDB(string dbFileName)
  20. {
  21. string tempFile = Path.Combine(Path.GetDirectoryName(dbFileName), Path.GetRandomFileName() + Path.GetExtension(dbFileName));
  22. var dbe = new Microsoft.Office.Interop.Access.Dao.DBEngine();
  23. try
  24. {
  25. dbe.CompactDatabase(dbFileName, tempFile);
  26. FileInfo temp = new FileInfo(tempFile);
  27. temp.CopyTo(dbFileName, true);
  28. temp.Delete();
  29. }
  30. catch (Exception e)
  31. {
  32. Console.WriteLine("Error: " + e.Message);
  33. }
  34. }
  35. public static void PlaySound(string FileName)
  36. {
  37. //要加载COM组件:Microsoft speech object Library
  38. if (!System.IO.File.Exists(FileName))
  39. {
  40. return;
  41. }
  42. try
  43. {
  44. SoundPlayer player = new SoundPlayer();
  45. player.SoundLocation = FileName;
  46. player.Load();
  47. player.Play();
  48. }
  49. catch (Exception e)
  50. {
  51. MessageBox.Show(e.Message);
  52. }
  53. }
  54. public static void GetDayFromDateCode(string DateCode, out int oYear, out int oMonth, out int oDay)
  55. {
  56. oYear = 0;
  57. oMonth = 0;
  58. oDay = 0;
  59. if (DateCode.Length == 4)
  60. {
  61. oYear = int.Parse(DateCode.Substring(0, 2));
  62. DateTime date = new DateTime(DateTime.Now.Year, 1, 1).AddDays(7 * int.Parse(DateCode.Substring(2, 2)));
  63. oMonth = date.Month;
  64. oDay = date.Day;
  65. }
  66. }
  67. public static string SysDisc1
  68. {
  69. get
  70. {
  71. return SysDisc = Environment.GetEnvironmentVariable("windir").Substring(0, 1);
  72. }
  73. set
  74. {
  75. SysDisc = value;
  76. }
  77. }
  78. public static string GetDataSQL(DataTable dt)
  79. {
  80. string sql = "";
  81. sql += ("select * from ");
  82. for (int j = 0; j < dt.Rows.Count; j++)
  83. {
  84. if (j != dt.Rows.Count - 1)
  85. sql += ("(" + dt.Rows[j]["lp_sql"].ToString() + "),");
  86. else
  87. sql += ("(" + dt.Rows[j]["lp_sql"].ToString() + ")");
  88. }
  89. return sql;
  90. }
  91. public static string DToAny(double DB, int Type)
  92. {
  93. string H = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  94. long D;
  95. double B;
  96. string tempD = "", tempB = "";
  97. D = (long)DB;
  98. B = DB - D;
  99. if (D == 0)
  100. {
  101. tempD = "0";
  102. }
  103. while (D != 0)
  104. {
  105. tempD = H[(((int)D % Type))] + tempD;
  106. D = D / Type;
  107. }
  108. for (int i = 0; i < 7; i++)
  109. {
  110. if (B == 0)
  111. {
  112. break;
  113. }
  114. tempB += H[((int)(B * Type))];
  115. B = B * Type - (int)(B * Type);
  116. }
  117. if (tempB == "")
  118. {
  119. return tempD;
  120. }
  121. else
  122. {
  123. return tempD + "." + tempB;
  124. }
  125. }
  126. public static DataTable GetExportDataTable(DataGridView dgv)
  127. {
  128. DataTable dt = ((DataTable)dgv.DataSource).Copy();
  129. for (int i = 0; i < dt.Columns.Count; i++)
  130. {
  131. for (int j = 0; j < dgv.Columns.Count; j++)
  132. {
  133. if (dt.Columns[i].ColumnName.ToLower() == dgv.Columns[j].DataPropertyName.ToLower())
  134. {
  135. dt.Columns[i].ColumnName = dgv.Columns[j].HeaderText;
  136. }
  137. }
  138. }
  139. return dt;
  140. }
  141. /// <summary>
  142. /// 通过DataTable的ColumnName和Caption来拼接一条语句
  143. /// </summary>
  144. /// <param name=""></param>
  145. /// <returns></returns>
  146. public static string GetGridViewSelectContent(DataGridView d)
  147. {
  148. StringBuilder selectConetnt = new StringBuilder();
  149. DataTable dt = (DataTable)d.DataSource;
  150. if (dt == null)
  151. {
  152. foreach (DataGridViewColumn dc in d.Columns)
  153. {
  154. if (dc.DataPropertyName != "" && dc.DataPropertyName != null)
  155. {
  156. selectConetnt.Append(dc.DataPropertyName + " as " + dc.DataPropertyName + ",");
  157. }
  158. }
  159. }
  160. else
  161. {
  162. foreach (DataColumn dc in dt.Columns)
  163. {
  164. selectConetnt.Append(dc.Caption + " as " + dc.ColumnName + ",");
  165. }
  166. }
  167. return selectConetnt.Remove(selectConetnt.Length - 1, 1).ToString();
  168. }
  169. /// <summary>
  170. /// 筛选DataTable
  171. /// </summary>
  172. /// <param name="dt"></param>
  173. /// <param name="condition"></param>
  174. /// <returns></returns>
  175. public static DataTable filterDataTable(DataTable dt, String condition)
  176. {
  177. if (dt == null)
  178. return new DataTable();
  179. //获取筛选条件中的列名,值
  180. DataRow[] dataRows = dt.Select(condition);
  181. DataTable ndt = dt.Clone();
  182. for (int i = 0; i < dataRows.Length; i++)
  183. {
  184. ndt.Rows.Add(dataRows[i].ItemArray);
  185. }
  186. return ndt;
  187. }
  188. /// <summary>
  189. /// 通过字段和其展示的中文值获取查询的内容
  190. /// </summary>
  191. /// <param name="field"></param>
  192. /// <param name="cnfield"></param>
  193. /// <returns></returns>
  194. public static string GetSelectContentByStringArray(string[] field, string[] cnfield)
  195. {
  196. StringBuilder sb = new StringBuilder();
  197. for (int i = 0; i < field.Length; i++)
  198. {
  199. sb.Append(field[i] + " as " + cnfield[i] + ",");
  200. }
  201. //去掉多余的逗号
  202. sb.Remove(sb.Length - 1, 1);
  203. return sb.ToString();
  204. }
  205. /// <summary>
  206. /// 传入控件的集合和DataTable,通过判断控件的名称和数据源的列的描述来匹配,支持单层的GroupBox和Panel
  207. /// </summary>
  208. /// <param name="collection"></param>
  209. /// <param name="dt"></param>
  210. public static void SetFormValue(ControlCollection collection, DataTable dt)
  211. {
  212. //DataTable存在数据才进行赋值操作
  213. if (dt.Rows.Count > 0)
  214. {
  215. for (int i = 0; i < collection.Count; i++)
  216. {
  217. if (collection[i].Controls.Count > 0)
  218. SetFormValue(collection[i].Controls, dt);
  219. string controlName = collection[i].Name;
  220. //默认给TextBox和Label赋值
  221. if (collection[i] is TextBox || collection[i] is Label || collection[i] is SearchTextBox || collection[i] is NumericUpDown || collection[i] is CheckBox || collection[i] is RichTextAutoBottom || collection[i] is RichTextBox)
  222. {
  223. for (int j = 0; j < dt.Columns.Count; j++)
  224. {
  225. if (controlName.ToUpper() == dt.Columns[j].Caption.ToUpper())
  226. {
  227. //字段含有Status内容的才进行转换
  228. if (controlName.ToUpper().Contains("STATUS"))
  229. {
  230. //对审批状态进行判断
  231. switch (dt.Rows[0][j].ToString().ToUpper())
  232. {
  233. case "ENTERING":
  234. collection[i].Text = "在录入";
  235. break;
  236. case "UNAPPROVED":
  237. collection[i].Text = "未批准";
  238. break;
  239. case "COMMITED":
  240. collection[i].Text = "已提交";
  241. break;
  242. case "APPROVE":
  243. collection[i].Text = "已批准";
  244. break;
  245. case "AUDITED":
  246. collection[i].Text = "已审核";
  247. break;
  248. case "STARTED":
  249. collection[i].Text = "已下发";
  250. break;
  251. case "UNCHECK":
  252. collection[i].Text = "待检验";
  253. break;
  254. case "CHECKING":
  255. collection[i].Text = "检验中";
  256. break;
  257. default:
  258. collection[i].Text = dt.Rows[0][j].ToString();
  259. break;
  260. }
  261. }
  262. else
  263. {
  264. if (collection[i] is CheckBox)
  265. {
  266. (collection[i] as CheckBox).Checked = dt.Rows[0][j].ToString() == "-1" ? true : false;
  267. }
  268. else if (collection[i] is NumericUpDown)
  269. {
  270. int num = 0;
  271. if (int.TryParse(dt.Rows[0][j].ToString(), out num))
  272. {
  273. (collection[i] as NumericUpDown).Value = num;
  274. }
  275. }
  276. else
  277. {
  278. collection[i].Text = dt.Rows[0][j].ToString();
  279. }
  280. }
  281. }
  282. }
  283. }
  284. //对封装在GroupBox的和Panel的控件进行批量赋值
  285. if (collection[i] is GroupBox || collection[i] is Panel || collection[i] is GroupBoxWithBorder)
  286. {
  287. for (int j = 0; j < collection[i].Controls.Count; j++)
  288. {
  289. controlName = collection[i].Controls[j].Name;
  290. if (collection[i].Controls[j] is TextBox || collection[i].Controls[j] is Label || collection[i].Controls[j] is SearchTextBox)
  291. {
  292. for (int k = 0; k < dt.Columns.Count; k++)
  293. {
  294. if (controlName.ToUpper() == dt.Columns[k].Caption.ToUpper())
  295. {
  296. collection[i].Controls[j].Text = dt.Rows[0][k].ToString();
  297. }
  298. }
  299. }
  300. }
  301. }
  302. }
  303. }
  304. //如果没有记录的话,将dt中含有的列的对应值设置为空
  305. else
  306. {
  307. for (int i = 0; i < collection.Count; i++)
  308. {
  309. if (collection[i] is TextBox || collection[i] is Label || collection[i] is SearchTextBox)
  310. {
  311. for (int j = 0; j < dt.Columns.Count; j++)
  312. {
  313. if (collection[i].Name.ToUpper() == dt.Columns[j].Caption.ToUpper())
  314. {
  315. collection[i].Text = "";
  316. }
  317. }
  318. }
  319. }
  320. }
  321. }
  322. /// <summary>
  323. /// 获取标签的路径
  324. /// </summary>
  325. /// <param name="URL"></param>
  326. /// <param name="LabelName"></param>
  327. /// <returns></returns>
  328. public static string GetLabelUrl(string URL, string LabelName, DateTime time)
  329. {
  330. //如果是传入的数据是从FTP取的文件
  331. if (URL.Contains("ftp:"))
  332. {
  333. ftpOperater ftp = new ftpOperater(false);
  334. return ftp.Download(LabelName, time);
  335. }
  336. else
  337. {
  338. return URL;
  339. }
  340. }
  341. /// <summary>
  342. /// 从DGV获取指定的列的数据形式是数组的形式
  343. /// </summary>
  344. public static ArrayList[] GetColumnDataFromDGV(DataGridView dgv, string[] ColumnName)
  345. {
  346. ArrayList[] array = new ArrayList[ColumnName.Length];
  347. //实例化和查询参数个数一样的ArrayList
  348. for (int i = 0; i < ColumnName.Length; i++)
  349. {
  350. array[i] = new ArrayList();
  351. }
  352. DataTable dt = (DataTable)dgv.DataSource;
  353. //如果第一列是否选框的话
  354. if (dgv.Columns[0] is DataGridViewCheckBoxColumn)
  355. {
  356. for (int i = 0; i < dt.Rows.Count; i++)
  357. {
  358. if (dgv.Rows[i].Cells[0].FormattedValue.ToString() == "True")
  359. {
  360. for (int j = 0; j < ColumnName.Length; j++)
  361. {
  362. array[j].Add(dt.Rows[i][ColumnName[j]]);
  363. }
  364. }
  365. }
  366. }
  367. //否则直接获取全部的数据
  368. else
  369. {
  370. for (int i = 0; i < dgv.RowCount; i++)
  371. {
  372. for (int j = 0; j < ColumnName.Length; j++)
  373. {
  374. array[j].Add(dt.Rows[i][ColumnName[j]]);
  375. }
  376. }
  377. }
  378. return array;
  379. }
  380. /// <summary>
  381. /// 通过DataGridView和需要隐藏的字段的数组来对字段进行隐藏
  382. /// </summary>
  383. /// <param name="dgv"></param>
  384. /// <param name="field"></param>
  385. public static void HideField(DataGridView dgv, string[] field)
  386. {
  387. DataTable dt = (DataTable)dgv.DataSource;
  388. foreach (DataColumn dc in dt.Columns)
  389. {
  390. foreach (string s in field)
  391. {
  392. if (dc.Caption == s)
  393. {
  394. dgv.Columns[dc.ColumnName].Visible = false;
  395. }
  396. }
  397. }
  398. }
  399. /// <summary>
  400. /// 通过查询的内容获取到字段的描述
  401. /// </summary>
  402. /// <param name="field"></param>
  403. /// <returns></returns>
  404. public static string[] GetCaptionFromField(string field)
  405. {
  406. string[] caption = field.Split(',');
  407. for (int i = 0; i < caption.Length; i++)
  408. {
  409. caption[i] = caption[i].Substring(0, caption[i].LastIndexOf("as")).Trim();
  410. }
  411. return caption;
  412. }
  413. /// <summary>
  414. /// 通过查询的语句获取查询的字段
  415. /// </summary>
  416. /// <param name="field"></param>
  417. /// <returns></returns>
  418. public static string[] GetField(string field)
  419. {
  420. string[] fields = field.Split(',');
  421. for (int i = 0; i < fields.Length; i++)
  422. {
  423. fields[i] = fields[i].Substring(fields[i].LastIndexOf("as") + 2, fields[i].Length - fields[i].LastIndexOf("as") - 2).Trim();
  424. }
  425. return fields;
  426. }
  427. /// <summary>
  428. /// 将DataTable转换为List的键值对
  429. /// </summary>
  430. /// <param name="dt"></param>
  431. /// <returns></returns>
  432. public static List<Dictionary<string, string>> DataTableToListDictionary(DataTable dt)
  433. {
  434. List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
  435. foreach (DataRow dr in dt.Rows)
  436. {
  437. Dictionary<string, string> dictionary = new Dictionary<string, string>();
  438. foreach (DataColumn dc in dt.Columns)
  439. {
  440. dictionary.Add(dc.Caption, dr[dt.Columns.IndexOf(dc)].ToString());
  441. }
  442. list.Add(dictionary);
  443. }
  444. return list;
  445. }
  446. /// <summary>
  447. /// 通过描述取DataTable的列名,主要用于从配置中取数据
  448. /// </summary>
  449. /// <param name="dt"></param>
  450. /// <param name="caption"></param>
  451. /// <returns></returns>
  452. public static string GetColumnNameByCaption(DataTable dt, string caption)
  453. {
  454. foreach (DataColumn dc in dt.Columns)
  455. {
  456. if (dc.Caption.ToLower() == caption)
  457. {
  458. return dc.ColumnName;
  459. }
  460. }
  461. return null;
  462. }
  463. //用于封装异常,也可以用于错误的提示
  464. public static void ShowError(string errorMessage)
  465. {
  466. throw new Exception(errorMessage);
  467. }
  468. /// <summary>
  469. /// 清除DataTable的结构和数据,清除列结构时需要从最后的一列开始删
  470. /// </summary>
  471. /// <param name="dt"></param>
  472. public static void CleanDataTable(DataTable dt)
  473. {
  474. for (int i = dt.Columns.Count - 1; i >= 0; i--)
  475. {
  476. dt.Columns.Remove(dt.Columns[i]);
  477. }
  478. }
  479. /// <summary>
  480. /// 不清楚表结构,只清楚数据
  481. /// </summary>
  482. /// <param name="dt"></param>
  483. public static void CleanDataTableData(DataTable dt)
  484. {
  485. for (int i = dt.Rows.Count - 1; i >= 0; i--)
  486. {
  487. dt.Rows.Remove(dt.Rows[i]);
  488. }
  489. }
  490. /// <summary>
  491. /// 获取拼接的字段
  492. /// </summary>
  493. /// <param name="dt"></param>
  494. /// <returns></returns>
  495. public static string GetFieldFromDataTable(DataTable dt)
  496. {
  497. StringBuilder sb = new StringBuilder();
  498. foreach (DataColumn dc in dt.Columns)
  499. {
  500. sb.Append(dc.Caption + ",");
  501. }
  502. return sb.ToString().Substring(sb.ToString().Length - 1, 1);
  503. }
  504. /// <summary>
  505. /// 已经定义好的DataGridView绑定数据,operate是用来添加操作列的
  506. /// </summary>
  507. /// <param name="dgv"></param>
  508. /// <param name="dt"></param>
  509. /// <param name="AddOpetateColumn"></param>
  510. /// <param name="operate"></param>
  511. public static void FillDgvWithDataTable(DataGridView dgv, DataTable dt, params DataGridViewImageColumn[] operate)
  512. {
  513. dgv.AutoGenerateColumns = false;
  514. dgv.DataSource = dt;
  515. if (operate.Length > 0)
  516. {
  517. if (dgv.Columns[operate[0].Name] != null)
  518. {
  519. dgv.Columns.Remove(dgv.Columns[operate[0].Name]);
  520. }
  521. dgv.Columns.Add(operate[0]);
  522. }
  523. //纯英文的列不予展示
  524. //Regex regEnglish = new Regex("^[_][a-z]$");
  525. //foreach (DataGridViewColumn dgvc in dgv.Columns)
  526. //{
  527. // if (dgvc.HeaderText.IndexOf("_id") > 0)
  528. // {
  529. // dgvc.Visible = false;
  530. // }
  531. //}
  532. }
  533. /// <summary>
  534. /// 清除DataGridView的数据
  535. /// </summary>
  536. /// <param name="dgv"></param>
  537. public static void CleanDGVData(DataGridView dgv)
  538. {
  539. for (int i = dgv.Rows.Count - 1; i >= 0; i--)
  540. {
  541. dgv.Rows.RemoveAt(i);
  542. }
  543. }
  544. /// <summary>
  545. /// 清除Form的指定类型的数据
  546. /// </summary>
  547. /// <param name="Form"></param>
  548. public static void CleanForm(Form Form)
  549. {
  550. for (int i = 0; i < Form.Controls.Count; i++)
  551. {
  552. if (Form.Controls[i] is EnterTextBox || Form.Controls[i] is TextBox || Form.Controls[i] is RichTextBox || Form.Controls[i] is SearchTextBox)
  553. Form.Controls[i].Text = "";
  554. if (Form.Controls[i] is DataGridView)
  555. CleanDGVData((DataGridView)Form.Controls[i]);
  556. }
  557. }
  558. /// <summary>
  559. /// 用于给DGV中的Combox列赋静态值
  560. /// </summary>
  561. /// <param name="dgvc"></param>
  562. /// <param name="displayField"></param>
  563. /// <param name="valueField"></param>
  564. /// <param name="Value"></param>
  565. /// <returns></returns>
  566. public static void SetDgvColumnComboxData(DataGridViewComboBoxColumn dgvc, string DataPropertyName, string displayField, string valueField, string[] Value)
  567. {
  568. DataTable dt = new DataTable();
  569. dt.Columns.Add(displayField);
  570. dt.Columns.Add(valueField);
  571. for (int i = 0; i < Value.Length; i++)
  572. {
  573. DataGridViewRow row = new DataGridViewRow();
  574. dt.Rows.Add(row);
  575. dt.Rows[i][displayField] = Value[i].Split('#')[0];
  576. dt.Rows[i][valueField] = Value[i].Split('#')[1];
  577. }
  578. dgvc.DataPropertyName = DataPropertyName;
  579. dgvc.DataSource = dt;
  580. dgvc.DisplayMember = displayField;
  581. dgvc.ValueMember = valueField;
  582. }
  583. /// <summary>
  584. /// 用于给DGV中的ComboxCell赋静态值
  585. /// </summary>
  586. /// <param name="dgvcc"></param>
  587. /// <param name="displayField"></param>
  588. /// <param name="valueField"></param>
  589. /// <param name="Value"></param>
  590. public static void SetDGVCellComboxData(DataGridViewComboBoxCell dgvcc, string displayField, string valueField, string[] Value)
  591. {
  592. DataTable dt = new DataTable();
  593. dt.Columns.Add(displayField);
  594. dt.Columns.Add(valueField);
  595. for (int i = 0; i < Value.Length; i++)
  596. {
  597. DataRow dr = dt.NewRow();
  598. dr[displayField] = Value[i].Split('#')[0];
  599. dr[valueField] = Value[i].Split('#')[1];
  600. dt.Rows.Add(dr);
  601. }
  602. dgvcc.DisplayMember = displayField;
  603. dgvcc.ValueMember = valueField;
  604. dgvcc.DataSource = dt;
  605. }
  606. /// <summary>
  607. /// 用于给DGV中的ComboxCell赋静态值
  608. /// </summary>
  609. /// <param name="dgvcc"></param>
  610. /// <param name="displayField"></param>
  611. /// <param name="valueField"></param>
  612. /// <param name="Value"></param>
  613. public static void SetComboxData(ComboBox dgvcc, string displayField, string valueField, string[] Value)
  614. {
  615. DataTable dt = new DataTable();
  616. dt.Columns.Add(displayField);
  617. dt.Columns.Add(valueField);
  618. for (int i = 0; i < Value.Length; i++)
  619. {
  620. DataRow dr = dt.NewRow();
  621. dr[displayField] = Value[i].Split('#')[0];
  622. dr[valueField] = Value[i].Split('#')[1];
  623. dt.Rows.Add(dr);
  624. }
  625. dgvcc.DisplayMember = displayField;
  626. dgvcc.ValueMember = valueField;
  627. dgvcc.DataSource = dt;
  628. }
  629. /// <summary>
  630. /// 获取刷选的SQL语句,传入的是TextBox的Control,传入的SQL不带Where条件
  631. /// </summary>
  632. /// <param name="SQL"></param>
  633. /// <param name="Condition"></param>
  634. /// <returns></returns>
  635. public static string GetScreenSqlCondition(params Control[] Condition)
  636. {
  637. string condition = " where ";
  638. for (int i = 0; i < Condition.Length; i++)
  639. {
  640. if (i != Condition.Length - 1)
  641. {
  642. if (Condition[i] is ComboBox)
  643. {
  644. condition += "(" + Condition[i].Tag + " like " + "'%" + (Condition[i] as ComboBox).SelectedValue + "%' or " + Condition[i].Tag + " is null) and ";
  645. }
  646. else
  647. {
  648. condition += "(" + Condition[i].Tag + " like " + "'%" + Condition[i].Text + "%' or " + Condition[i].Tag + " is null) and ";
  649. }
  650. }
  651. else
  652. {
  653. if (Condition[i] is ComboBox)
  654. {
  655. condition += "(" + Condition[i].Tag + " like " + "'%" + (Condition[i] as ComboBox).SelectedValue + "%' or " + Condition[i].Tag + " is null)";
  656. }
  657. else
  658. {
  659. condition += "(" + Condition[i].Tag + " like " + "'%" + Condition[i].Text + "%' or " + Condition[i].Tag + " is null)";
  660. }
  661. }
  662. }
  663. return condition;
  664. }
  665. public static void CleanDataGridView(DataGridView dgv)
  666. {
  667. for (int i = dgv.Columns.Count - 1; i >= 0; i--)
  668. {
  669. dgv.Columns.RemoveAt(i);
  670. }
  671. }
  672. /// <summary>
  673. /// 取出SQL中的参数占位符
  674. /// </summary>
  675. /// <param name="SQL"></param>
  676. /// <returns></returns>
  677. public static string[] GetParamFromSQL(string SQL)
  678. {
  679. string[] par = SQL.Split(':');
  680. //用来存参数的数组
  681. StringBuilder[] addpar = new StringBuilder[par.Length - 1];
  682. string[] param = new string[par.Length - 1];
  683. for (int i = 0; i < par.Length - 1; i++)
  684. {
  685. //新建一个char类型的数组用来存储每个字节的变量
  686. char[] c = par[i + 1].ToCharArray();
  687. addpar[i] = new StringBuilder();
  688. for (int j = 0; j < c.Length; j++)
  689. {
  690. if (c[j] != ' ' && c[j] != ',' && c[j] != ')')
  691. {
  692. addpar[i].Append(c[j]);
  693. }
  694. else
  695. {
  696. break;
  697. }
  698. }
  699. }
  700. for (int i = 0; i < par.Length - 1; i++)
  701. {
  702. param[i] = addpar[i].ToString();
  703. }
  704. return param;
  705. }
  706. public static void SetFormCenter(Form form)
  707. {
  708. form.StartPosition = FormStartPosition.CenterParent;
  709. }
  710. /// <summary>
  711. /// 设置DataGridView的指定列可编辑
  712. /// </summary>
  713. /// <param name="DGV"></param>
  714. /// <param name="EditAbleField"></param>
  715. public static void SetDataGridViewReadOnly(DataGridView DGV, string[] EditAbleField)
  716. {
  717. foreach (DataGridViewColumn dc in DGV.Columns)
  718. {
  719. dc.ReadOnly = true;
  720. foreach (string s in EditAbleField)
  721. {
  722. if (dc.Name.ToLower() == s.ToLower())
  723. {
  724. DGV.Columns[dc.Name].ReadOnly = false;
  725. }
  726. }
  727. }
  728. }
  729. //判断带有CheckBox的DGV是否有项目勾选了
  730. public static DataTable DGVIfChecked(DataGridView dgv)
  731. {
  732. int CheckCount = 0;
  733. DataTable dt = new DataTable();
  734. //第一列是勾选框,排除在循环之外
  735. for (int i = 1; i < dgv.Columns.Count; i++)
  736. {
  737. dt.Columns.Add(dgv.Columns[i].Name);
  738. }
  739. for (int i = 0; i < dgv.RowCount; i++)
  740. {
  741. if (dgv.Rows[i].Cells[0].Value != null)
  742. {
  743. if (dgv.Rows[i].Cells[0].Value.ToString() == "True")
  744. {
  745. if (dgv.Rows[i].Tag.ToString() == "SonRow")
  746. {
  747. DataRow dr = dt.NewRow();
  748. for (int j = 1; j < dgv.ColumnCount; j++)
  749. {
  750. dr[dgv.Columns[j].Name] = dgv.Rows[i].Cells[j].FormattedValue;
  751. }
  752. dt.Rows.Add(dr);
  753. CheckCount++;
  754. }
  755. }
  756. }
  757. }
  758. //判断是否勾选了明细
  759. if (CheckCount == 0)
  760. {
  761. return null;
  762. }
  763. return dt;
  764. }
  765. /// <summary>
  766. /// 设置只允许输入数字
  767. /// </summary>
  768. /// <param name="sender"></param>
  769. /// <param name="e"></param>
  770. public static void NumOnly(object sender, KeyPressEventArgs e)
  771. {
  772. if (e.KeyChar != '\b')//这是允许输入退格键
  773. {
  774. if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字
  775. {
  776. e.Handled = true;
  777. }
  778. }
  779. }
  780. public static Dictionary<string, object> ToDictionary(string JsonData)
  781. {
  782. object Data = null;
  783. Dictionary<string, object> Dic = new Dictionary<string, object>();
  784. if (JsonData.StartsWith("["))
  785. {
  786. //如果目标直接就为数组类型,则将会直接输出一个Key为List的List<Dictionary<string, object>>集合
  787. //使用示例List<Dictionary<string, object>> ListDic = (List<Dictionary<string, object>>)Dic["List"];
  788. List<Dictionary<string, object>> List = new List<Dictionary<string, object>>();
  789. MatchCollection ListMatch = Regex.Matches(JsonData, @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组
  790. foreach (Match ListItem in ListMatch)
  791. {
  792. List.Add(ToDictionary(ListItem.ToString()));//递归调用
  793. }
  794. Data = List;
  795. Dic.Add("List", Data);
  796. }
  797. else
  798. {
  799. MatchCollection Match = Regex.Matches(JsonData, @"""(.+?)"": {0,1}(\[[\s\S]+?\]|null|"".+?""|-{0,1}\d*)");//使用正则表达式匹配出JSON数据中的键与值
  800. foreach (Match item in Match)
  801. {
  802. try
  803. {
  804. if (item.Groups[2].ToString().StartsWith("["))
  805. {
  806. //如果目标是数组,将会输出一个Key为当前Json的List<Dictionary<string, object>>集合
  807. //使用示例List<Dictionary<string, object>> ListDic = (List<Dictionary<string, object>>)Dic["Json中的Key"];
  808. List<Dictionary<string, object>> List = new List<Dictionary<string, object>>();
  809. MatchCollection ListMatch = Regex.Matches(item.Groups[2].ToString(), @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组
  810. foreach (Match ListItem in ListMatch)
  811. {
  812. List.Add(ToDictionary(ListItem.ToString()));//递归调用
  813. }
  814. Data = List;
  815. }
  816. else if (item.Groups[2].ToString().ToLower() == "null") Data = null;//如果数据为null(字符串类型),直接转换成null
  817. else Data = item.Groups[2].ToString(); //数据为数字、字符串中的一类,直接写入
  818. Dic.Add(item.Groups[1].ToString(), Data);
  819. }
  820. catch { }
  821. }
  822. }
  823. return Dic;
  824. }
  825. public static string AddField(string[] Fields)
  826. {
  827. string sql = " ";
  828. foreach (string field in Fields)
  829. {
  830. sql += field + ",";
  831. }
  832. return sql.Substring(0, sql.Length - 1);
  833. }
  834. }
  835. }