BaseUtil.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. (collection[i] as NumericUpDown).Value = int.Parse(dt.Rows[0][j].ToString());
  271. }
  272. else
  273. {
  274. collection[i].Text = dt.Rows[0][j].ToString();
  275. }
  276. }
  277. }
  278. }
  279. }
  280. //对封装在GroupBox的和Panel的控件进行批量赋值
  281. if (collection[i] is GroupBox || collection[i] is Panel || collection[i] is GroupBoxWithBorder)
  282. {
  283. for (int j = 0; j < collection[i].Controls.Count; j++)
  284. {
  285. controlName = collection[i].Controls[j].Name;
  286. if (collection[i].Controls[j] is TextBox || collection[i].Controls[j] is Label || collection[i].Controls[j] is SearchTextBox)
  287. {
  288. for (int k = 0; k < dt.Columns.Count; k++)
  289. {
  290. if (controlName.ToUpper() == dt.Columns[k].Caption.ToUpper())
  291. {
  292. collection[i].Controls[j].Text = dt.Rows[0][k].ToString();
  293. }
  294. }
  295. }
  296. }
  297. }
  298. }
  299. }
  300. //如果没有记录的话,将dt中含有的列的对应值设置为空
  301. else
  302. {
  303. for (int i = 0; i < collection.Count; i++)
  304. {
  305. if (collection[i] is TextBox || collection[i] is Label || collection[i] is SearchTextBox)
  306. {
  307. for (int j = 0; j < dt.Columns.Count; j++)
  308. {
  309. if (collection[i].Name.ToUpper() == dt.Columns[j].Caption.ToUpper())
  310. {
  311. collection[i].Text = "";
  312. }
  313. }
  314. }
  315. }
  316. }
  317. }
  318. /// <summary>
  319. /// 获取标签的路径
  320. /// </summary>
  321. /// <param name="URL"></param>
  322. /// <param name="LabelName"></param>
  323. /// <returns></returns>
  324. public static string GetLabelUrl(string URL, string LabelName, DateTime time)
  325. {
  326. //如果是传入的数据是从FTP取的文件
  327. if (URL.Contains("ftp:"))
  328. {
  329. ftpOperater ftp = new ftpOperater();
  330. return ftp.Download(LabelName, time);
  331. }
  332. else
  333. {
  334. return URL;
  335. }
  336. }
  337. /// <summary>
  338. /// 从DGV获取指定的列的数据形式是数组的形式
  339. /// </summary>
  340. public static ArrayList[] GetColumnDataFromDGV(DataGridView dgv, string[] ColumnName)
  341. {
  342. ArrayList[] array = new ArrayList[ColumnName.Length];
  343. //实例化和查询参数个数一样的ArrayList
  344. for (int i = 0; i < ColumnName.Length; i++)
  345. {
  346. array[i] = new ArrayList();
  347. }
  348. DataTable dt = (DataTable)dgv.DataSource;
  349. //如果第一列是否选框的话
  350. if (dgv.Columns[0] is DataGridViewCheckBoxColumn)
  351. {
  352. for (int i = 0; i < dt.Rows.Count; i++)
  353. {
  354. if (dgv.Rows[i].Cells[0].FormattedValue.ToString() == "True")
  355. {
  356. for (int j = 0; j < ColumnName.Length; j++)
  357. {
  358. array[j].Add(dt.Rows[i][ColumnName[j]]);
  359. }
  360. }
  361. }
  362. }
  363. //否则直接获取全部的数据
  364. else
  365. {
  366. for (int i = 0; i < dgv.RowCount; i++)
  367. {
  368. for (int j = 0; j < ColumnName.Length; j++)
  369. {
  370. array[j].Add(dt.Rows[i][ColumnName[j]]);
  371. }
  372. }
  373. }
  374. return array;
  375. }
  376. /// <summary>
  377. /// 通过DataGridView和需要隐藏的字段的数组来对字段进行隐藏
  378. /// </summary>
  379. /// <param name="dgv"></param>
  380. /// <param name="field"></param>
  381. public static void HideField(DataGridView dgv, string[] field)
  382. {
  383. DataTable dt = (DataTable)dgv.DataSource;
  384. foreach (DataColumn dc in dt.Columns)
  385. {
  386. foreach (string s in field)
  387. {
  388. if (dc.Caption == s)
  389. {
  390. dgv.Columns[dc.ColumnName].Visible = false;
  391. }
  392. }
  393. }
  394. }
  395. /// <summary>
  396. /// 通过查询的内容获取到字段的描述
  397. /// </summary>
  398. /// <param name="field"></param>
  399. /// <returns></returns>
  400. public static string[] GetCaptionFromField(string field)
  401. {
  402. string[] caption = field.Split(',');
  403. for (int i = 0; i < caption.Length; i++)
  404. {
  405. caption[i] = caption[i].Substring(0, caption[i].LastIndexOf("as")).Trim();
  406. }
  407. return caption;
  408. }
  409. /// <summary>
  410. /// 通过查询的语句获取查询的字段
  411. /// </summary>
  412. /// <param name="field"></param>
  413. /// <returns></returns>
  414. public static string[] GetField(string field)
  415. {
  416. string[] fields = field.Split(',');
  417. for (int i = 0; i < fields.Length; i++)
  418. {
  419. fields[i] = fields[i].Substring(fields[i].LastIndexOf("as") + 2, fields[i].Length - fields[i].LastIndexOf("as") - 2).Trim();
  420. }
  421. return fields;
  422. }
  423. /// <summary>
  424. /// 将DataTable转换为List的键值对
  425. /// </summary>
  426. /// <param name="dt"></param>
  427. /// <returns></returns>
  428. public static List<Dictionary<string, string>> DataTableToListDictionary(DataTable dt)
  429. {
  430. List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
  431. foreach (DataRow dr in dt.Rows)
  432. {
  433. Dictionary<string, string> dictionary = new Dictionary<string, string>();
  434. foreach (DataColumn dc in dt.Columns)
  435. {
  436. dictionary.Add(dc.Caption, dr[dt.Columns.IndexOf(dc)].ToString());
  437. }
  438. list.Add(dictionary);
  439. }
  440. return list;
  441. }
  442. /// <summary>
  443. /// 通过描述取DataTable的列名,主要用于从配置中取数据
  444. /// </summary>
  445. /// <param name="dt"></param>
  446. /// <param name="caption"></param>
  447. /// <returns></returns>
  448. public static string GetColumnNameByCaption(DataTable dt, string caption)
  449. {
  450. foreach (DataColumn dc in dt.Columns)
  451. {
  452. if (dc.Caption.ToLower() == caption)
  453. {
  454. return dc.ColumnName;
  455. }
  456. }
  457. return null;
  458. }
  459. //用于封装异常,也可以用于错误的提示
  460. public static void ShowError(string errorMessage)
  461. {
  462. throw new Exception(errorMessage);
  463. }
  464. /// <summary>
  465. /// 清除DataTable的结构和数据,清除列结构时需要从最后的一列开始删
  466. /// </summary>
  467. /// <param name="dt"></param>
  468. public static void CleanDataTable(DataTable dt)
  469. {
  470. for (int i = dt.Columns.Count - 1; i >= 0; i--)
  471. {
  472. dt.Columns.Remove(dt.Columns[i]);
  473. }
  474. }
  475. /// <summary>
  476. /// 不清楚表结构,只清楚数据
  477. /// </summary>
  478. /// <param name="dt"></param>
  479. public static void CleanDataTableData(DataTable dt)
  480. {
  481. for (int i = dt.Rows.Count - 1; i >= 0; i--)
  482. {
  483. dt.Rows.Remove(dt.Rows[i]);
  484. }
  485. }
  486. /// <summary>
  487. /// 获取拼接的字段
  488. /// </summary>
  489. /// <param name="dt"></param>
  490. /// <returns></returns>
  491. public static string GetFieldFromDataTable(DataTable dt)
  492. {
  493. StringBuilder sb = new StringBuilder();
  494. foreach (DataColumn dc in dt.Columns)
  495. {
  496. sb.Append(dc.Caption + ",");
  497. }
  498. return sb.ToString().Substring(sb.ToString().Length - 1, 1);
  499. }
  500. /// <summary>
  501. /// 已经定义好的DataGridView绑定数据,operate是用来添加操作列的
  502. /// </summary>
  503. /// <param name="dgv"></param>
  504. /// <param name="dt"></param>
  505. /// <param name="AddOpetateColumn"></param>
  506. /// <param name="operate"></param>
  507. public static void FillDgvWithDataTable(DataGridView dgv, DataTable dt, params DataGridViewImageColumn[] operate)
  508. {
  509. dgv.AutoGenerateColumns = false;
  510. dgv.DataSource = dt;
  511. if (operate.Length > 0)
  512. {
  513. if (dgv.Columns[operate[0].Name] != null)
  514. {
  515. dgv.Columns.Remove(dgv.Columns[operate[0].Name]);
  516. }
  517. dgv.Columns.Add(operate[0]);
  518. }
  519. //纯英文的列不予展示
  520. //Regex regEnglish = new Regex("^[_][a-z]$");
  521. //foreach (DataGridViewColumn dgvc in dgv.Columns)
  522. //{
  523. // if (dgvc.HeaderText.IndexOf("_id") > 0)
  524. // {
  525. // dgvc.Visible = false;
  526. // }
  527. //}
  528. }
  529. /// <summary>
  530. /// 清除DataGridView的数据
  531. /// </summary>
  532. /// <param name="dgv"></param>
  533. public static void CleanDGVData(DataGridView dgv)
  534. {
  535. for (int i = dgv.Rows.Count - 1; i >= 0; i--)
  536. {
  537. dgv.Rows.RemoveAt(i);
  538. }
  539. }
  540. /// <summary>
  541. /// 清除Form的指定类型的数据
  542. /// </summary>
  543. /// <param name="Form"></param>
  544. public static void CleanForm(Form Form)
  545. {
  546. for (int i = 0; i < Form.Controls.Count; i++)
  547. {
  548. if (Form.Controls[i] is EnterTextBox || Form.Controls[i] is TextBox || Form.Controls[i] is RichTextBox || Form.Controls[i] is SearchTextBox)
  549. Form.Controls[i].Text = "";
  550. if (Form.Controls[i] is DataGridView)
  551. CleanDGVData((DataGridView)Form.Controls[i]);
  552. }
  553. }
  554. /// <summary>
  555. /// 用于给DGV中的Combox列赋静态值
  556. /// </summary>
  557. /// <param name="dgvc"></param>
  558. /// <param name="displayField"></param>
  559. /// <param name="valueField"></param>
  560. /// <param name="Value"></param>
  561. /// <returns></returns>
  562. public static void SetDgvColumnComboxData(DataGridViewComboBoxColumn dgvc, string DataPropertyName, string displayField, string valueField, string[] Value)
  563. {
  564. DataTable dt = new DataTable();
  565. dt.Columns.Add(displayField);
  566. dt.Columns.Add(valueField);
  567. for (int i = 0; i < Value.Length; i++)
  568. {
  569. DataGridViewRow row = new DataGridViewRow();
  570. dt.Rows.Add(row);
  571. dt.Rows[i][displayField] = Value[i].Split('#')[0];
  572. dt.Rows[i][valueField] = Value[i].Split('#')[1];
  573. }
  574. dgvc.DataPropertyName = DataPropertyName;
  575. dgvc.DataSource = dt;
  576. dgvc.DisplayMember = displayField;
  577. dgvc.ValueMember = valueField;
  578. }
  579. /// <summary>
  580. /// 用于给DGV中的ComboxCell赋静态值
  581. /// </summary>
  582. /// <param name="dgvcc"></param>
  583. /// <param name="displayField"></param>
  584. /// <param name="valueField"></param>
  585. /// <param name="Value"></param>
  586. public static void SetDGVCellComboxData(DataGridViewComboBoxCell dgvcc, string displayField, string valueField, string[] Value)
  587. {
  588. DataTable dt = new DataTable();
  589. dt.Columns.Add(displayField);
  590. dt.Columns.Add(valueField);
  591. for (int i = 0; i < Value.Length; i++)
  592. {
  593. DataRow dr = dt.NewRow();
  594. dr[displayField] = Value[i].Split('#')[0];
  595. dr[valueField] = Value[i].Split('#')[1];
  596. dt.Rows.Add(dr);
  597. }
  598. dgvcc.DisplayMember = displayField;
  599. dgvcc.ValueMember = valueField;
  600. dgvcc.DataSource = dt;
  601. }
  602. /// <summary>
  603. /// 用于给DGV中的ComboxCell赋静态值
  604. /// </summary>
  605. /// <param name="dgvcc"></param>
  606. /// <param name="displayField"></param>
  607. /// <param name="valueField"></param>
  608. /// <param name="Value"></param>
  609. public static void SetComboxData(ComboBox dgvcc, string displayField, string valueField, string[] Value)
  610. {
  611. DataTable dt = new DataTable();
  612. dt.Columns.Add(displayField);
  613. dt.Columns.Add(valueField);
  614. for (int i = 0; i < Value.Length; i++)
  615. {
  616. DataRow dr = dt.NewRow();
  617. dr[displayField] = Value[i].Split('#')[0];
  618. dr[valueField] = Value[i].Split('#')[1];
  619. dt.Rows.Add(dr);
  620. }
  621. dgvcc.DisplayMember = displayField;
  622. dgvcc.ValueMember = valueField;
  623. dgvcc.DataSource = dt;
  624. }
  625. /// <summary>
  626. /// 获取刷选的SQL语句,传入的是TextBox的Control,传入的SQL不带Where条件
  627. /// </summary>
  628. /// <param name="SQL"></param>
  629. /// <param name="Condition"></param>
  630. /// <returns></returns>
  631. public static string GetScreenSqlCondition(params Control[] Condition)
  632. {
  633. string condition = " where ";
  634. for (int i = 0; i < Condition.Length; i++)
  635. {
  636. if (i != Condition.Length - 1)
  637. {
  638. if (Condition[i] is ComboBox)
  639. {
  640. condition += "(" + Condition[i].Tag + " like " + "'%" + (Condition[i] as ComboBox).SelectedValue + "%' or " + Condition[i].Tag + " is null) and ";
  641. }
  642. else
  643. {
  644. condition += "(" + Condition[i].Tag + " like " + "'%" + Condition[i].Text + "%' or " + Condition[i].Tag + " is null) and ";
  645. }
  646. }
  647. else
  648. {
  649. if (Condition[i] is ComboBox)
  650. {
  651. condition += "(" + Condition[i].Tag + " like " + "'%" + (Condition[i] as ComboBox).SelectedValue + "%' or " + Condition[i].Tag + " is null)";
  652. }
  653. else
  654. {
  655. condition += "(" + Condition[i].Tag + " like " + "'%" + Condition[i].Text + "%' or " + Condition[i].Tag + " is null)";
  656. }
  657. }
  658. }
  659. return condition;
  660. }
  661. public static void CleanDataGridView(DataGridView dgv)
  662. {
  663. for (int i = dgv.Columns.Count - 1; i >= 0; i--)
  664. {
  665. dgv.Columns.RemoveAt(i);
  666. }
  667. }
  668. /// <summary>
  669. /// 取出SQL中的参数占位符
  670. /// </summary>
  671. /// <param name="SQL"></param>
  672. /// <returns></returns>
  673. public static string[] GetParamFromSQL(string SQL)
  674. {
  675. string[] par = SQL.Split(':');
  676. //用来存参数的数组
  677. StringBuilder[] addpar = new StringBuilder[par.Length - 1];
  678. string[] param = new string[par.Length - 1];
  679. for (int i = 0; i < par.Length - 1; i++)
  680. {
  681. //新建一个char类型的数组用来存储每个字节的变量
  682. char[] c = par[i + 1].ToCharArray();
  683. addpar[i] = new StringBuilder();
  684. for (int j = 0; j < c.Length; j++)
  685. {
  686. if (c[j] != ' ' && c[j] != ',' && c[j] != ')')
  687. {
  688. addpar[i].Append(c[j]);
  689. }
  690. else
  691. {
  692. break;
  693. }
  694. }
  695. }
  696. for (int i = 0; i < par.Length - 1; i++)
  697. {
  698. param[i] = addpar[i].ToString();
  699. }
  700. return param;
  701. }
  702. public static void SetFormCenter(Form form)
  703. {
  704. form.StartPosition = FormStartPosition.CenterParent;
  705. }
  706. /// <summary>
  707. /// 设置DataGridView的指定列可编辑
  708. /// </summary>
  709. /// <param name="DGV"></param>
  710. /// <param name="EditAbleField"></param>
  711. public static void SetDataGridViewReadOnly(DataGridView DGV, string[] EditAbleField)
  712. {
  713. foreach (DataGridViewColumn dc in DGV.Columns)
  714. {
  715. dc.ReadOnly = true;
  716. foreach (string s in EditAbleField)
  717. {
  718. if (dc.Name.ToLower() == s.ToLower())
  719. {
  720. DGV.Columns[dc.Name].ReadOnly = false;
  721. }
  722. }
  723. }
  724. }
  725. //判断带有CheckBox的DGV是否有项目勾选了
  726. public static DataTable DGVIfChecked(DataGridView dgv)
  727. {
  728. int CheckCount = 0;
  729. DataTable dt = new DataTable();
  730. //第一列是勾选框,排除在循环之外
  731. for (int i = 1; i < dgv.Columns.Count; i++)
  732. {
  733. dt.Columns.Add(dgv.Columns[i].Name);
  734. }
  735. for (int i = 0; i < dgv.RowCount; i++)
  736. {
  737. if (dgv.Rows[i].Cells[0].Value != null)
  738. {
  739. if (dgv.Rows[i].Cells[0].Value.ToString() == "True")
  740. {
  741. if (dgv.Rows[i].Tag.ToString() == "SonRow")
  742. {
  743. DataRow dr = dt.NewRow();
  744. for (int j = 1; j < dgv.ColumnCount; j++)
  745. {
  746. dr[dgv.Columns[j].Name] = dgv.Rows[i].Cells[j].FormattedValue;
  747. }
  748. dt.Rows.Add(dr);
  749. CheckCount++;
  750. }
  751. }
  752. }
  753. }
  754. //判断是否勾选了明细
  755. if (CheckCount == 0)
  756. {
  757. return null;
  758. }
  759. return dt;
  760. }
  761. /// <summary>
  762. /// 设置只允许输入数字
  763. /// </summary>
  764. /// <param name="sender"></param>
  765. /// <param name="e"></param>
  766. public static void NumOnly(object sender, KeyPressEventArgs e)
  767. {
  768. if (e.KeyChar != '\b')//这是允许输入退格键
  769. {
  770. if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字
  771. {
  772. e.Handled = true;
  773. }
  774. }
  775. }
  776. public static Dictionary<string, object> ToDictionary(string JsonData)
  777. {
  778. object Data = null;
  779. Dictionary<string, object> Dic = new Dictionary<string, object>();
  780. if (JsonData.StartsWith("["))
  781. {
  782. //如果目标直接就为数组类型,则将会直接输出一个Key为List的List<Dictionary<string, object>>集合
  783. //使用示例List<Dictionary<string, object>> ListDic = (List<Dictionary<string, object>>)Dic["List"];
  784. List<Dictionary<string, object>> List = new List<Dictionary<string, object>>();
  785. MatchCollection ListMatch = Regex.Matches(JsonData, @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组
  786. foreach (Match ListItem in ListMatch)
  787. {
  788. List.Add(ToDictionary(ListItem.ToString()));//递归调用
  789. }
  790. Data = List;
  791. Dic.Add("List", Data);
  792. }
  793. else
  794. {
  795. MatchCollection Match = Regex.Matches(JsonData, @"""(.+?)"": {0,1}(\[[\s\S]+?\]|null|"".+?""|-{0,1}\d*)");//使用正则表达式匹配出JSON数据中的键与值
  796. foreach (Match item in Match)
  797. {
  798. try
  799. {
  800. if (item.Groups[2].ToString().StartsWith("["))
  801. {
  802. //如果目标是数组,将会输出一个Key为当前Json的List<Dictionary<string, object>>集合
  803. //使用示例List<Dictionary<string, object>> ListDic = (List<Dictionary<string, object>>)Dic["Json中的Key"];
  804. List<Dictionary<string, object>> List = new List<Dictionary<string, object>>();
  805. MatchCollection ListMatch = Regex.Matches(item.Groups[2].ToString(), @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组
  806. foreach (Match ListItem in ListMatch)
  807. {
  808. List.Add(ToDictionary(ListItem.ToString()));//递归调用
  809. }
  810. Data = List;
  811. }
  812. else if (item.Groups[2].ToString().ToLower() == "null") Data = null;//如果数据为null(字符串类型),直接转换成null
  813. else Data = item.Groups[2].ToString(); //数据为数字、字符串中的一类,直接写入
  814. Dic.Add(item.Groups[1].ToString(), Data);
  815. }
  816. catch { }
  817. }
  818. }
  819. return Dic;
  820. }
  821. public static string AddField(string[] Fields)
  822. {
  823. string sql = " ";
  824. foreach (string field in Fields)
  825. {
  826. sql += field + ",";
  827. }
  828. return sql.Substring(0, sql.Length - 1);
  829. }
  830. }
  831. }