SQLiteHelper.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. using SXLibrary;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.Data;
  6. using System.Data.Common;
  7. using System.Data.SQLite;
  8. namespace MOKA_Factory_Tools
  9. {
  10. class SQLiteHelper
  11. {
  12. /// <summary>
  13. /// 新建数据库文件
  14. /// </summary>
  15. /// <param name="dbPath">数据库文件路径及名称</param>
  16. /// <returns>新建成功,返回true,否则返回false</returns>
  17. static public Boolean NewDbFile(string dbPath)
  18. {
  19. try
  20. {
  21. SQLiteConnection.CreateFile(dbPath);
  22. return true;
  23. }
  24. catch (Exception ex)
  25. {
  26. throw new Exception("Create DB" + dbPath + "fail:" + ex.Message);
  27. }
  28. }
  29. /// <summary>
  30. /// 创建表
  31. /// </summary>
  32. /// <param name="dbPath">指定数据库文件</param>
  33. /// <param name="tableName">表名称</param>
  34. static public void NewTable(string dbPath, string tableName,string ColumnMessage)
  35. {
  36. SQLiteConnection sqliteConn = new SQLiteConnection("data source=" + dbPath);
  37. if (sqliteConn.State != System.Data.ConnectionState.Open)
  38. {
  39. sqliteConn.Open();
  40. SQLiteCommand cmd = new SQLiteCommand();
  41. cmd.Connection = sqliteConn;
  42. cmd.CommandText = "CREATE TABLE " + tableName + ColumnMessage;
  43. cmd.ExecuteNonQuery();
  44. }
  45. sqliteConn.Close();
  46. sqliteConn.Dispose();
  47. }
  48. /// <summary>
  49. /// mid表插入一行
  50. /// </summary>
  51. /// <param name="dbPath"></param>
  52. /// <param name="param"></param>
  53. static public void AddOneLine(string dbPath,object[] param)
  54. {
  55. SQLiteConnection sqliteConn = new SQLiteConnection("data source=" + dbPath);
  56. if (sqliteConn.State != System.Data.ConnectionState.Open)
  57. {
  58. sqliteConn.Open();
  59. SQLiteCommand cmd = new SQLiteCommand();
  60. cmd.Connection = sqliteConn;
  61. cmd.CommandText = string.Format("insert into mid(bid,number,pid,ctype,version,host,purl,psize,pmd5,status) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}')", param[0], param[1], param[2], param[3], param[4], param[5], param[6], param[7], param[8], param[9]);
  62. cmd.ExecuteNonQuery();
  63. }
  64. sqliteConn.Close();
  65. sqliteConn.Dispose();
  66. }
  67. /// <summary>
  68. /// roku表插入一行
  69. /// </summary>
  70. /// <param name="dbPath"></param>
  71. /// <param name="param"></param>
  72. static public void AddRokuOneLine(string dbPath, object[] param)
  73. {
  74. SQLiteConnection sqliteConn = new SQLiteConnection("data source=" + dbPath);
  75. if (sqliteConn.State != System.Data.ConnectionState.Open)
  76. {
  77. sqliteConn.Open();
  78. SQLiteCommand cmd = new SQLiteCommand();
  79. cmd.Connection = sqliteConn;
  80. cmd.CommandText = string.Format("insert into rokuCustomer(ordernum,region,brand,oemmodel,supporturl,supportphone,productiondate,remotetype) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')", param[0], param[1], param[2], param[3], param[4], param[5], param[6], param[7]);
  81. cmd.ExecuteNonQuery();
  82. }
  83. sqliteConn.Close();
  84. sqliteConn.Dispose();
  85. }
  86. /// <summary>
  87. /// whitebalance表插入一行
  88. /// </summary>
  89. /// <param name="dbPath"></param>
  90. /// <param name="param"></param>
  91. static public void AddwbOneLine(string dbPath, object[] param)
  92. {
  93. SQLiteConnection sqliteConn = new SQLiteConnection("data source=" + dbPath);
  94. if (sqliteConn.State != System.Data.ConnectionState.Open)
  95. {
  96. sqliteConn.Open();
  97. SQLiteCommand cmd = new SQLiteCommand();
  98. cmd.Connection = sqliteConn;
  99. cmd.CommandText = string.Format("insert into whitebalance(ordernum,hdmirgain,hdmiggain,hdmibgain,nrgain,nggain,nbgain,lrgain,lggain,lbgain) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}')", param[0], param[1], param[2], param[3], param[4], param[5], param[6], param[7], param[8], param[9]);
  100. cmd.ExecuteNonQuery();
  101. }
  102. sqliteConn.Close();
  103. sqliteConn.Dispose();
  104. }
  105. /// <summary>
  106. /// dsn表插入一行
  107. /// </summary>
  108. /// <param name="dbPath"></param>
  109. /// <param name="param"></param>
  110. static public void AdddsnOneLine(string dbPath, object[] param)
  111. {
  112. SQLiteConnection sqliteConn = new SQLiteConnection("data source=" + dbPath);
  113. if (sqliteConn.State != System.Data.ConnectionState.Open)
  114. {
  115. sqliteConn.Open();
  116. SQLiteCommand cmd = new SQLiteCommand();
  117. cmd.Connection = sqliteConn;
  118. cmd.CommandText = string.Format("insert into dsn(ordernum,dsn) values('{0}','{1}')", param[0], param[1]);
  119. cmd.ExecuteNonQuery();
  120. }
  121. sqliteConn.Close();
  122. sqliteConn.Dispose();
  123. }
  124. static public void UpdateTime(string dbPath, string bid)
  125. {
  126. SQLiteConnection sqliteConn = new SQLiteConnection("data source=" + dbPath);
  127. if (sqliteConn.State != System.Data.ConnectionState.Open)
  128. {
  129. sqliteConn.Open();
  130. SQLiteCommand cmd = new SQLiteCommand();
  131. cmd.Connection = sqliteConn;
  132. cmd.CommandText = string.Format("update mid set finish_date=datetime(CURRENT_TIMESTAMP,'localtime') where bid='{0}'",bid);
  133. cmd.ExecuteNonQuery();
  134. }
  135. sqliteConn.Close();
  136. sqliteConn.Dispose();
  137. }
  138. static public void UpdateStatus(string dbPath, string str)
  139. {
  140. SQLiteConnection sqliteConn = new SQLiteConnection("data source=" + dbPath);
  141. if (sqliteConn.State != System.Data.ConnectionState.Open)
  142. {
  143. sqliteConn.Open();
  144. SQLiteCommand cmd = new SQLiteCommand();
  145. cmd.Connection = sqliteConn;
  146. cmd.CommandText = string.Format("update mid set status='{0}'", str);
  147. cmd.ExecuteNonQuery();
  148. }
  149. sqliteConn.Close();
  150. sqliteConn.Dispose();
  151. }
  152. /// <summary>
  153. /// 使用事务批量插入sn,key行
  154. /// </summary>
  155. /// <param name="dbPath"></param>
  156. /// <param name="keys"></param>
  157. /// <returns></returns>
  158. static public bool InsertKeys(string dbPath, List<object[]> keys)
  159. {
  160. DbTransaction trans = null;
  161. try
  162. {
  163. SQLiteConnection sqliteConn = new SQLiteConnection("data source=" + dbPath);
  164. if (sqliteConn.State != System.Data.ConnectionState.Open)
  165. {
  166. sqliteConn.Open();
  167. SQLiteCommand cmd = new SQLiteCommand();
  168. cmd.Connection = sqliteConn;
  169. trans = sqliteConn.BeginTransaction();
  170. foreach (var key in keys)
  171. {
  172. cmd.CommandText = string.Format("insert into keys(sn,keys) values('{0}','{1}')", key[0], key[1]);
  173. cmd.ExecuteNonQuery();
  174. }
  175. trans.Commit();
  176. }
  177. sqliteConn.Close();
  178. sqliteConn.Dispose();
  179. return true;
  180. }
  181. catch(Exception ex)
  182. {
  183. if (trans != null)
  184. trans.Rollback();
  185. Log.WriteErrorLog("\r\nFail to transfer key to DB:\r\n" + ex.Message);
  186. return false;
  187. }
  188. }
  189. /// <summary>
  190. /// 插入一条sn/key数据
  191. /// </summary>
  192. /// <param name="sqliteConn"></param>
  193. /// <param name="sn"></param>
  194. /// <param name="keydata"></param>
  195. /// <returns></returns>
  196. static public bool Insertonekey(SQLiteConnection sqliteConn,string sn,string keydata)
  197. {
  198. DbTransaction trans = null;
  199. try
  200. {
  201. SQLiteCommand cmd = new SQLiteCommand();
  202. cmd.Connection = sqliteConn;
  203. trans = sqliteConn.BeginTransaction();
  204. cmd.CommandText = string.Format("insert into keys(sn,keys,copy_date) values('{0}','{1}','{2}')", sn, keydata, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  205. cmd.ExecuteNonQuery();
  206. trans.Commit();
  207. return true;
  208. }
  209. catch (Exception ex)
  210. {
  211. if (trans != null)
  212. trans.Rollback();
  213. Log.WriteErrorLog("\r\nFail to transfer key to DB:\r\n" + ex.Message);
  214. return false;
  215. }
  216. }
  217. static public bool CheckDownloadStatus(string dbPath,string bid)
  218. {
  219. SQLiteConnection sqliteConn = new SQLiteConnection("data source=" + dbPath);
  220. if (sqliteConn.State != System.Data.ConnectionState.Open)
  221. {
  222. sqliteConn.Open();
  223. SQLiteCommand cmd = new SQLiteCommand();
  224. cmd.Connection = sqliteConn;
  225. cmd.CommandText = string.Format("select * from mid where bid='{0}'and status='1'", bid);
  226. using (SQLiteDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
  227. {
  228. if(dr.Read())
  229. {
  230. return true;
  231. }
  232. }
  233. }
  234. sqliteConn.Close();
  235. sqliteConn.Dispose();
  236. return false;
  237. }
  238. static public bool WholeCheckDownloadStatus(SQLiteConnection sqliteConn, string bid)
  239. {
  240. SQLiteCommand cmd = new SQLiteCommand();
  241. cmd.Connection = sqliteConn;
  242. cmd.CommandText = string.Format("select * from mid where bid='{0}'and status='1'", bid);
  243. SQLiteDataReader dr = cmd.ExecuteReader();
  244. if (dr.Read())
  245. {
  246. dr.Close();
  247. return true;
  248. }
  249. dr.Close();
  250. return false;
  251. }
  252. /// <summary>
  253. /// 获取DB文件内的key数据(json格式)
  254. /// </summary>
  255. /// <param name="sqliteConn"></param>
  256. /// <param name="SN"></param>
  257. /// <param name="copydone"></param>
  258. /// <returns></returns>
  259. static public string Getkeys(SQLiteConnection sqliteConn, string SN,out bool copydone)
  260. {
  261. copydone = false;
  262. SQLiteCommand cmd = new SQLiteCommand();
  263. cmd.Connection = sqliteConn;
  264. cmd.CommandText = string.Format("select * from keys where sn='{0}'", SN);
  265. SQLiteDataReader dr = cmd.ExecuteReader();
  266. if (dr.Read())
  267. {
  268. NameValueCollection user = dr.GetValues();
  269. string values = user.Get("keys");
  270. if (user.Get("copy_date").Length>0)
  271. copydone = true;
  272. dr.Close();
  273. return values;
  274. }
  275. dr.Close();
  276. Log.WriteErrorLog("\r\nFail to Get keys to DB:\r\n" + cmd.CommandText);
  277. return "";
  278. }
  279. /// <summary>
  280. /// 获取DB文件内的roku抄写数据
  281. /// </summary>
  282. /// <param name="sqliteConn"></param>
  283. /// <param name="order"></param>
  284. /// <param name="rokuCustomer"></param>
  285. /// <returns></returns>
  286. static public bool GetrokuCustomer(SQLiteConnection sqliteConn, string order, out RokuCustomer rokuCustomer)
  287. {
  288. rokuCustomer = null;
  289. SQLiteCommand cmd = new SQLiteCommand();
  290. cmd.Connection = sqliteConn;
  291. cmd.CommandText = string.Format("select * from rokuCustomer where ordernum='{0}'", order);
  292. SQLiteDataReader dr = cmd.ExecuteReader();
  293. if (dr.Read())
  294. {
  295. NameValueCollection user = dr.GetValues();
  296. rokuCustomer = new RokuCustomer()
  297. {
  298. ordernum = user.Get("ordernum"),
  299. brand = user.Get("brand"),
  300. region = user.Get("region"),
  301. oemmodel = user.Get("oemmodel"),
  302. supporturl = user.Get("supporturl"),
  303. supportphone = user.Get("supportphone"),
  304. productiondate = user.Get("productiondate"),
  305. remotetype= user.Get("remotetype")
  306. };
  307. dr.Close();
  308. return true;
  309. }
  310. dr.Close();
  311. return false;
  312. }
  313. /// <summary>
  314. /// 获取DB文件内的dsn数据
  315. /// </summary>
  316. /// <param name="sqliteConn"></param>
  317. /// <param name="order"></param>
  318. /// <param name="rokuCustomer"></param>
  319. /// <returns></returns>
  320. static public bool Getdsn(SQLiteConnection sqliteConn, string order, out string dsn)
  321. {
  322. dsn = null;
  323. SQLiteCommand cmd = new SQLiteCommand();
  324. cmd.Connection = sqliteConn;
  325. cmd.CommandText = string.Format("select * from dsn where ordernum='{0}'", order);
  326. SQLiteDataReader dr = cmd.ExecuteReader();
  327. if (dr.Read())
  328. {
  329. NameValueCollection user = dr.GetValues();
  330. dsn = user.Get("dsn");
  331. dr.Close();
  332. return true;
  333. }
  334. dr.Close();
  335. return false;
  336. }
  337. /// <summary>
  338. /// 获取DB文件内的白平衡数据
  339. /// </summary>
  340. /// <param name="sqliteConn"></param>
  341. /// <param name="order"></param>
  342. /// <param name="whiteBalanceInfo"></param>
  343. /// <returns></returns>
  344. static public bool Getwhitebalance(SQLiteConnection sqliteConn, string order, out WhiteBalanceInfo whiteBalanceInfo)
  345. {
  346. whiteBalanceInfo = null;
  347. try
  348. {
  349. SQLiteCommand cmd = new SQLiteCommand();
  350. cmd.Connection = sqliteConn;
  351. cmd.CommandText = string.Format("select * from whitebalance where ordernum='{0}'", order);
  352. SQLiteDataReader dr = cmd.ExecuteReader();
  353. if (dr.Read())
  354. {
  355. NameValueCollection user = dr.GetValues();
  356. whiteBalanceInfo = new WhiteBalanceInfo()
  357. {
  358. ordernum = user.Get("ordernum"),
  359. hdmirgain = user.Get("hdmirgain"),
  360. hdmiggain = user.Get("hdmiggain"),
  361. hdmibgain = user.Get("hdmibgain"),
  362. nrgain = user.Get("nrgain"),
  363. nggain = user.Get("nggain"),
  364. nbgain = user.Get("nbgain"),
  365. lrgain = user.Get("lrgain"),
  366. lggain = user.Get("lggain"),
  367. lbgain = user.Get("lbgain")
  368. };
  369. dr.Close();
  370. return true;
  371. }
  372. dr.Close();
  373. return false;
  374. }
  375. catch(Exception ex)
  376. {
  377. Log.WriteErrorLog(ex.Message);
  378. return false;
  379. }
  380. }
  381. static public bool GetDBMidInfo(SQLiteConnection sqliteConn, string bid,out MidAddress MidAddress1)
  382. {
  383. MidAddress1 = new MidAddress();
  384. SQLiteCommand cmd = new SQLiteCommand();
  385. cmd.Connection = sqliteConn;
  386. cmd.CommandText = string.Format("select * from mid where bid='{0}'", bid);
  387. SQLiteDataReader dr = cmd.ExecuteReader();
  388. if (dr.Read())
  389. {
  390. NameValueCollection user = dr.GetValues();
  391. MidAddress1.order = user.Get("bid");
  392. MidAddress1.pid= user.Get("pid");
  393. MidAddress1.des = user.Get("des");
  394. MidAddress1.number = user.Get("number");
  395. MidAddress1.ctype = user.Get("ctype");
  396. MidAddress1.purl = user.Get("purl");
  397. MidAddress1.psize = user.Get("psize");
  398. MidAddress1.pmd5 = user.Get("pmd5");
  399. MidAddress1.version = user.Get("version");
  400. MidAddress1.host = user.Get("host");
  401. dr.Close();
  402. return true;
  403. }
  404. dr.Close();
  405. return false;
  406. }
  407. static public bool UpdateCopyStatus(SQLiteConnection sqliteConn,string SN,out string copydate)
  408. {
  409. SQLiteCommand cmd = new SQLiteCommand();
  410. cmd.Connection = sqliteConn;
  411. cmd.CommandText = "select datetime(CURRENT_TIMESTAMP,'localtime')";
  412. SQLiteDataReader dr = cmd.ExecuteReader();
  413. copydate = "";
  414. if (dr.Read())
  415. {
  416. NameValueCollection user = dr.GetValues();
  417. copydate = user.Get("datetime(CURRENT_TIMESTAMP,'localtime')");
  418. }
  419. dr.Close();
  420. cmd.CommandText = string.Format("update keys set copy_date='"+ copydate+ "'where sn='{0}'", SN);
  421. int result = cmd.ExecuteNonQuery();
  422. cmd.Dispose();
  423. if (result > 0 && copydate.Length > 0)
  424. return true;
  425. else
  426. return false;
  427. }
  428. static public bool UpdateReportStatus(SQLiteConnection sqliteConn, string SN)
  429. {
  430. SQLiteCommand cmd = new SQLiteCommand();
  431. cmd.Connection = sqliteConn;
  432. cmd.CommandText = string.Format("update keys set report_date=datetime(CURRENT_TIMESTAMP,'localtime') where sn='{0}'", SN);
  433. int result = cmd.ExecuteNonQuery();
  434. cmd.Dispose();
  435. if (result > 0)
  436. return true;
  437. else
  438. return false;
  439. }
  440. static public bool UpdateReportListStatus(SQLiteConnection sqliteConn, List<string> SNs)
  441. {
  442. DbTransaction trans = null;
  443. try
  444. {
  445. SQLiteCommand cmd = new SQLiteCommand();
  446. cmd.Connection = sqliteConn;
  447. trans = sqliteConn.BeginTransaction();
  448. foreach (var SN in SNs)
  449. {
  450. cmd.CommandText = string.Format("update keys set report_date=datetime(CURRENT_TIMESTAMP,'localtime') where sn='{0}'", SN);
  451. cmd.ExecuteNonQuery();
  452. }
  453. trans.Commit();
  454. cmd.Dispose();
  455. return true;
  456. }
  457. catch(Exception ex)
  458. {
  459. return false;
  460. }
  461. }
  462. static public bool InsertDelayCopyReport(SQLiteConnection sqliteConn, object[] param)
  463. {
  464. SQLiteCommand cmd = new SQLiteCommand();
  465. cmd.Connection = sqliteConn;
  466. cmd.CommandText = string.Format("insert into CopyDelayReport(bid,url,content) values('{0}','{1}','{2}')", param[0], param[1], param[2]);
  467. int result = cmd.ExecuteNonQuery();
  468. cmd.Dispose();
  469. if (result > 0)
  470. return true;
  471. else
  472. return false;
  473. }
  474. static public bool DeleteDelayCopyReport(SQLiteConnection sqliteConn, string ID)
  475. {
  476. SQLiteCommand cmd = new SQLiteCommand();
  477. cmd.Connection = sqliteConn;
  478. cmd.CommandText = string.Format("Delete from CopyDelayReport where ID='{0}'",ID);
  479. int result = cmd.ExecuteNonQuery();
  480. cmd.Dispose();
  481. if (result > 0)
  482. return true;
  483. else
  484. return false;
  485. }
  486. static public bool IsErrorReportExist(SQLiteConnection sqliteConn, string url, string content)
  487. {
  488. try
  489. {
  490. SQLiteCommand cmd = new SQLiteCommand();
  491. cmd.Connection = sqliteConn;
  492. cmd.CommandText = string.Format("select count from ErrorReport where url='{0}' and content='{1}'", url, content);
  493. SQLiteDataReader dr = cmd.ExecuteReader();
  494. if (dr.Read())
  495. {
  496. NameValueCollection user = dr.GetValues();
  497. string values = user.Get("count");
  498. int count = Convert.ToInt32(values);
  499. dr.Close();
  500. if (count > 0)
  501. return true;
  502. cmd.Dispose();
  503. return false;
  504. }
  505. dr.Close();
  506. cmd.Dispose();
  507. return false;
  508. }
  509. catch (Exception ex)
  510. {
  511. Log.WriteErrorLog("\r\nGet Report Count Error:" + ex.Message);
  512. return false;
  513. }
  514. }
  515. static public bool InsertDelayErrorReport(SQLiteConnection sqliteConn, object[] param)
  516. {
  517. SQLiteCommand cmd = new SQLiteCommand();
  518. cmd.Connection = sqliteConn;
  519. if (param[0].ToString().EndsWith("smes/RecordKey"))
  520. {
  521. if (IsErrorReportExist(sqliteConn, param[0].ToString(), param[1].ToString()))
  522. {
  523. // 记录存在,退出;
  524. return true;
  525. }
  526. }
  527. cmd.CommandText = string.Format("insert into ErrorReport(url,content) values('{0}','{1}')", param[0], param[1]);
  528. int result = cmd.ExecuteNonQuery();
  529. cmd.Dispose();
  530. if (result > 0)
  531. return true;
  532. else
  533. return false;
  534. }
  535. public static bool CheckProductionNum(SQLiteConnection sqliteConn, string order)
  536. {
  537. SQLiteCommand cmd = new SQLiteCommand();
  538. cmd.Connection = sqliteConn;
  539. cmd.CommandText = string.Format("select * from ProductionCount where bid='{0}'", order);
  540. SQLiteDataReader dr = cmd.ExecuteReader();
  541. if (dr.Read())
  542. {
  543. dr.Close();
  544. return true;
  545. }
  546. dr.Close();
  547. cmd.CommandText = string.Format("insert into ProductionCount(bid,count) values('{0}','{1}')", order, "0");
  548. int result = cmd.ExecuteNonQuery();
  549. cmd.Dispose();
  550. if (result > 0)
  551. return true;
  552. else
  553. return false;
  554. }
  555. public static bool UpdateProductionNum(SQLiteConnection sqliteConn, string order)
  556. {
  557. try
  558. {
  559. SQLiteCommand cmd = new SQLiteCommand();
  560. cmd.Connection = sqliteConn;
  561. cmd.CommandText = string.Format("select count from ProductionCount where bid='{0}'", order);
  562. SQLiteDataReader dr = cmd.ExecuteReader();
  563. if (dr.Read())
  564. {
  565. NameValueCollection user = dr.GetValues();
  566. string values = user.Get("count");
  567. string count = (Convert.ToInt32(values) + 1).ToString();
  568. dr.Close();
  569. cmd.CommandText = string.Format("update ProductionCount set count ='{0}' where bid='{1}'", count, order);
  570. int result = cmd.ExecuteNonQuery();
  571. if (result > 0)
  572. return true;
  573. else
  574. Log.WriteErrorLog("\r\nFail to update ProductionCount from db:\r\n" + cmd.CommandText);
  575. cmd.Dispose();
  576. return false;
  577. }
  578. dr.Close();
  579. Log.WriteErrorLog("\r\nFail to get ProductionCount from db:\r\n" + cmd.CommandText);
  580. cmd.Dispose();
  581. return false;
  582. }
  583. catch(Exception ex)
  584. {
  585. Log.WriteErrorLog("\r\nFail to UpdateProductionNum:" + ex.Message);
  586. return false;
  587. }
  588. }
  589. /// <summary>
  590. /// 获取本地DB对应订单已经抄写的数量(包含重复抄写)
  591. /// </summary>
  592. /// <param name="sqliteConn"></param>
  593. /// <param name="order"></param>
  594. /// <returns></returns>
  595. public static string GetProductionNum(SQLiteConnection sqliteConn, string order)
  596. {
  597. try
  598. {
  599. SQLiteCommand cmd = new SQLiteCommand();
  600. cmd.Connection = sqliteConn;
  601. cmd.CommandText = string.Format("select count from ProductionCount where bid='{0}'", order);
  602. SQLiteDataReader dr = cmd.ExecuteReader();
  603. if (dr.Read())
  604. {
  605. NameValueCollection user = dr.GetValues();
  606. string values = user.Get("count");
  607. dr.Close();
  608. cmd.Dispose();
  609. return values;
  610. }
  611. dr.Close();
  612. cmd.Dispose();
  613. return "";
  614. }
  615. catch (Exception ex)
  616. {
  617. Log.WriteErrorLog("\r\nFail to GetProductionNum:" + ex.Message);
  618. return "";
  619. }
  620. }
  621. /// <summary>
  622. /// 删除30天之前已经上报的数据
  623. /// </summary>
  624. /// <param name="sqliteConn"></param>
  625. /// <returns></returns>
  626. public static bool DeleteOldData(SQLiteConnection sqliteConn)
  627. {
  628. try
  629. {
  630. SQLiteCommand cmd = new SQLiteCommand();
  631. cmd.Connection = sqliteConn;
  632. cmd.CommandText = "Delete from ErrorReport where report_date is not null and date('now', '-30 day') >= date(report_date)";
  633. cmd.ExecuteNonQuery();
  634. cmd.CommandText = "Delete from CopyDelayReport where report_date is not null and date('now', '-30 day') >= date(report_date)";
  635. cmd.ExecuteNonQuery();
  636. cmd.Dispose();
  637. return true;
  638. }
  639. catch(Exception ex)
  640. {
  641. GC.Collect();
  642. Log.WriteErrorLog("\r\nFail to DeleteOldData:" + ex.Message);
  643. return false;
  644. }
  645. }
  646. /// <summary>
  647. /// 获取本地DB记录的未上报异常数据
  648. /// </summary>
  649. /// <param name="sqliteConn"></param>
  650. /// <param name="url"></param>
  651. /// <param name="content"></param>
  652. /// <returns></returns>
  653. public static bool GetErrorReportData(SQLiteConnection sqliteConn,out string url,out string content,out string id)
  654. {
  655. url = "";
  656. content = "";
  657. id = "";
  658. try
  659. {
  660. SQLiteCommand cmd = new SQLiteCommand();
  661. cmd.Connection = sqliteConn;
  662. cmd.CommandText = "select url,content,ID from ErrorReport where report_date is null LIMIT 1";
  663. SQLiteDataReader dr = cmd.ExecuteReader();
  664. if (dr.Read())
  665. {
  666. NameValueCollection user = dr.GetValues();
  667. url = user.Get("url");
  668. content = user.Get("content");
  669. id = user.Get("ID");
  670. dr.Close();
  671. return true;
  672. }
  673. dr.Close();
  674. return false;
  675. }
  676. catch(Exception ex)
  677. {
  678. GC.Collect();
  679. Log.WriteErrorLog("\r\nFail to GetErrorReportData:" + ex.Message);
  680. return false;
  681. }
  682. }
  683. /// <summary>
  684. /// 获取本地DB记录的未上报烧录数据
  685. /// </summary>
  686. /// <param name="sqliteConn"></param>
  687. /// <param name="url"></param>
  688. /// <param name="content"></param>
  689. /// <returns></returns>
  690. public static bool GetDelayReportData(SQLiteConnection sqliteConn, out string url, out string content,out string id)
  691. {
  692. url = "";
  693. content = "";
  694. id = "";
  695. try
  696. {
  697. SQLiteCommand cmd = new SQLiteCommand();
  698. cmd.Connection = sqliteConn;
  699. cmd.CommandText = "select url,content,ID from CopyDelayReport where report_date is null LIMIT 1";
  700. SQLiteDataReader dr = cmd.ExecuteReader();
  701. if (dr.Read())
  702. {
  703. NameValueCollection user = dr.GetValues();
  704. url = user.Get("url");
  705. content = user.Get("content");
  706. id = user.Get("ID");
  707. dr.Close();
  708. return true;
  709. }
  710. dr.Close();
  711. return false;
  712. }
  713. catch (Exception ex)
  714. {
  715. GC.Collect();
  716. Log.WriteErrorLog("\r\nFail to GetDelayReportData:" + ex.Message);
  717. return false;
  718. }
  719. }
  720. public static bool GetEppormDataList(SQLiteConnection sqliteConn, out List<PostDateClass> postList,out List<string> SNs)
  721. {
  722. postList = new List<PostDateClass>();
  723. try
  724. {
  725. SQLiteCommand cmd = new SQLiteCommand();
  726. SNs = new List<string>();
  727. cmd.Connection = sqliteConn;
  728. cmd.CommandText = "select sn from keys where report_date is null LIMIT 200";
  729. SQLiteDataReader dr = cmd.ExecuteReader();
  730. while (dr.Read())
  731. {
  732. NameValueCollection user = dr.GetValues();
  733. string sn = user.Get("sn");
  734. SNs.Add(sn);
  735. postList.Add(new PostDateClass("sn", sn));
  736. postList.Add(new PostDateClass("date", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
  737. }
  738. dr.Close();
  739. return true;
  740. }
  741. catch (Exception ex)
  742. {
  743. GC.Collect();
  744. SNs = null;
  745. Log.WriteErrorLog("\r\nFail to GetEppormData:" + ex.Message);
  746. return false;
  747. }
  748. }
  749. public static bool GetEppormDataList2(SQLiteConnection sqliteConn, out List<PostDateClass> postList, out List<string> SNs)
  750. {
  751. postList = new List<PostDateClass>();
  752. try
  753. {
  754. SQLiteCommand cmd = new SQLiteCommand();
  755. SNs = new List<string>();
  756. cmd.Connection = sqliteConn;
  757. cmd.CommandText = "select sn,copy_date from keys where report_date is null and copy_date is not null LIMIT 20";
  758. SQLiteDataReader dr = cmd.ExecuteReader();
  759. while (dr.Read())
  760. {
  761. NameValueCollection user = dr.GetValues();
  762. string sn = user.Get("sn");
  763. string copy_date = user.Get("copy_date");
  764. SNs.Add(sn);
  765. postList.Add(new PostDateClass("sn", sn));
  766. postList.Add(new PostDateClass("date", copy_date));
  767. }
  768. dr.Close();
  769. return true;
  770. }
  771. catch (Exception ex)
  772. {
  773. GC.Collect();
  774. SNs = null;
  775. Log.WriteErrorLog("\r\nFail to GetEppormData:" + ex.Message);
  776. return false;
  777. }
  778. }
  779. public static bool GetEppormDataCount(SQLiteConnection sqliteConn, out string count)
  780. {
  781. count = "";
  782. try
  783. {
  784. SQLiteCommand cmd = new SQLiteCommand();
  785. cmd.Connection = sqliteConn;
  786. cmd.CommandText = "select count(*) from keys";
  787. SQLiteDataReader dr = cmd.ExecuteReader();
  788. if (dr.Read())
  789. {
  790. NameValueCollection user = dr.GetValues();
  791. count = user.Get("count(*)");
  792. dr.Close();
  793. return true;
  794. }
  795. dr.Close();
  796. return false;
  797. }
  798. catch (Exception ex)
  799. {
  800. GC.Collect();
  801. Log.WriteErrorLog("\r\nFail to GetEppormDataCount:" + ex.Message);
  802. return false;
  803. }
  804. }
  805. public static bool GetEppormUploadedDataCount(SQLiteConnection sqliteConn, out string count)
  806. {
  807. count = "";
  808. try
  809. {
  810. SQLiteCommand cmd = new SQLiteCommand();
  811. cmd.Connection = sqliteConn;
  812. cmd.CommandText = "select count(*) from keys where report_date is not null";
  813. SQLiteDataReader dr = cmd.ExecuteReader();
  814. if (dr.Read())
  815. {
  816. NameValueCollection user = dr.GetValues();
  817. count = user.Get("count(*)");
  818. dr.Close();
  819. return true;
  820. }
  821. dr.Close();
  822. return false;
  823. }
  824. catch (Exception ex)
  825. {
  826. GC.Collect();
  827. Log.WriteErrorLog("\r\nFail to GetEppormDataCount:" + ex.Message);
  828. return false;
  829. }
  830. }
  831. /// <summary>
  832. /// 更新本地DB记录的未上报数据
  833. /// </summary>
  834. public static bool UpdateReportData(SQLiteConnection sqliteConn,string table,string column,string data)
  835. {
  836. SQLiteCommand cmd = new SQLiteCommand();
  837. cmd.Connection = sqliteConn;
  838. cmd.CommandText = string.Format("update {0} set report_date = datetime(CURRENT_TIMESTAMP,'localtime') where {1}= '{2}'", table, column, data);
  839. int result = cmd.ExecuteNonQuery();
  840. cmd.Dispose();
  841. if (result > 0)
  842. return true;
  843. else
  844. return false;
  845. }
  846. }
  847. }