SQLiteHelper.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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,data) values('{0}','{1}')", param[0], param[1]);
  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 = V2Method.GetRokuInfo(user.Get("data"));
  297. dr.Close();
  298. return true;
  299. }
  300. dr.Close();
  301. return false;
  302. }
  303. /// <summary>
  304. /// 获取DB文件内的dsn数据
  305. /// </summary>
  306. /// <param name="sqliteConn"></param>
  307. /// <param name="order"></param>
  308. /// <param name="rokuCustomer"></param>
  309. /// <returns></returns>
  310. static public bool Getdsn(SQLiteConnection sqliteConn, string order, out string dsn)
  311. {
  312. dsn = null;
  313. SQLiteCommand cmd = new SQLiteCommand();
  314. cmd.Connection = sqliteConn;
  315. cmd.CommandText = string.Format("select * from dsn where ordernum='{0}'", order);
  316. SQLiteDataReader dr = cmd.ExecuteReader();
  317. if (dr.Read())
  318. {
  319. NameValueCollection user = dr.GetValues();
  320. dsn = user.Get("dsn");
  321. dr.Close();
  322. return true;
  323. }
  324. dr.Close();
  325. return false;
  326. }
  327. /// <summary>
  328. /// 获取DB文件内的白平衡数据
  329. /// </summary>
  330. /// <param name="sqliteConn"></param>
  331. /// <param name="order"></param>
  332. /// <param name="whiteBalanceInfo"></param>
  333. /// <returns></returns>
  334. static public bool Getwhitebalance(SQLiteConnection sqliteConn, string order, out WhiteBalanceInfo whiteBalanceInfo)
  335. {
  336. whiteBalanceInfo = null;
  337. try
  338. {
  339. SQLiteCommand cmd = new SQLiteCommand();
  340. cmd.Connection = sqliteConn;
  341. cmd.CommandText = string.Format("select * from whitebalance where ordernum='{0}'", order);
  342. SQLiteDataReader dr = cmd.ExecuteReader();
  343. if (dr.Read())
  344. {
  345. NameValueCollection user = dr.GetValues();
  346. whiteBalanceInfo = new WhiteBalanceInfo()
  347. {
  348. ordernum = user.Get("ordernum"),
  349. hdmirgain = user.Get("hdmirgain"),
  350. hdmiggain = user.Get("hdmiggain"),
  351. hdmibgain = user.Get("hdmibgain"),
  352. nrgain = user.Get("nrgain"),
  353. nggain = user.Get("nggain"),
  354. nbgain = user.Get("nbgain"),
  355. lrgain = user.Get("lrgain"),
  356. lggain = user.Get("lggain"),
  357. lbgain = user.Get("lbgain")
  358. };
  359. dr.Close();
  360. return true;
  361. }
  362. dr.Close();
  363. return false;
  364. }
  365. catch(Exception ex)
  366. {
  367. Log.WriteErrorLog(ex.Message);
  368. return false;
  369. }
  370. }
  371. static public bool GetDBMidInfo(SQLiteConnection sqliteConn, string bid,out MidAddress MidAddress1)
  372. {
  373. MidAddress1 = new MidAddress();
  374. SQLiteCommand cmd = new SQLiteCommand();
  375. cmd.Connection = sqliteConn;
  376. cmd.CommandText = string.Format("select * from mid where bid='{0}'", bid);
  377. SQLiteDataReader dr = cmd.ExecuteReader();
  378. if (dr.Read())
  379. {
  380. NameValueCollection user = dr.GetValues();
  381. MidAddress1.order = user.Get("bid");
  382. MidAddress1.pid= user.Get("pid");
  383. MidAddress1.des = user.Get("des");
  384. MidAddress1.number = user.Get("number");
  385. MidAddress1.ctype = user.Get("ctype");
  386. MidAddress1.purl = user.Get("purl");
  387. MidAddress1.psize = user.Get("psize");
  388. MidAddress1.pmd5 = user.Get("pmd5");
  389. MidAddress1.version = user.Get("version");
  390. MidAddress1.host = user.Get("host");
  391. dr.Close();
  392. return true;
  393. }
  394. dr.Close();
  395. return false;
  396. }
  397. static public bool UpdateCopyStatus(SQLiteConnection sqliteConn,string SN,out string copydate)
  398. {
  399. SQLiteCommand cmd = new SQLiteCommand();
  400. cmd.Connection = sqliteConn;
  401. cmd.CommandText = "select datetime(CURRENT_TIMESTAMP,'localtime')";
  402. SQLiteDataReader dr = cmd.ExecuteReader();
  403. copydate = "";
  404. if (dr.Read())
  405. {
  406. NameValueCollection user = dr.GetValues();
  407. copydate = user.Get("datetime(CURRENT_TIMESTAMP,'localtime')");
  408. }
  409. dr.Close();
  410. cmd.CommandText = string.Format("update keys set copy_date='"+ copydate+ "'where sn='{0}'", SN);
  411. int result = cmd.ExecuteNonQuery();
  412. cmd.Dispose();
  413. if (result > 0 && copydate.Length > 0)
  414. return true;
  415. else
  416. return false;
  417. }
  418. static public bool UpdateReportStatus(SQLiteConnection sqliteConn, string SN)
  419. {
  420. SQLiteCommand cmd = new SQLiteCommand();
  421. cmd.Connection = sqliteConn;
  422. cmd.CommandText = string.Format("update keys set report_date=datetime(CURRENT_TIMESTAMP,'localtime') where sn='{0}'", SN);
  423. int result = cmd.ExecuteNonQuery();
  424. cmd.Dispose();
  425. if (result > 0)
  426. return true;
  427. else
  428. return false;
  429. }
  430. static public bool UpdateReportListStatus(SQLiteConnection sqliteConn, List<string> SNs)
  431. {
  432. DbTransaction trans = null;
  433. try
  434. {
  435. SQLiteCommand cmd = new SQLiteCommand();
  436. cmd.Connection = sqliteConn;
  437. trans = sqliteConn.BeginTransaction();
  438. foreach (var SN in SNs)
  439. {
  440. cmd.CommandText = string.Format("update keys set report_date=datetime(CURRENT_TIMESTAMP,'localtime') where sn='{0}'", SN);
  441. cmd.ExecuteNonQuery();
  442. }
  443. trans.Commit();
  444. cmd.Dispose();
  445. return true;
  446. }
  447. catch(Exception ex)
  448. {
  449. return false;
  450. }
  451. }
  452. static public bool InsertDelayCopyReport(SQLiteConnection sqliteConn, object[] param)
  453. {
  454. SQLiteCommand cmd = new SQLiteCommand();
  455. cmd.Connection = sqliteConn;
  456. cmd.CommandText = string.Format("insert into CopyDelayReport(bid,url,content) values('{0}','{1}','{2}')", param[0], param[1], param[2]);
  457. int result = cmd.ExecuteNonQuery();
  458. cmd.Dispose();
  459. if (result > 0)
  460. return true;
  461. else
  462. return false;
  463. }
  464. static public bool DeleteDelayCopyReport(SQLiteConnection sqliteConn, string ID)
  465. {
  466. SQLiteCommand cmd = new SQLiteCommand();
  467. cmd.Connection = sqliteConn;
  468. cmd.CommandText = string.Format("Delete from CopyDelayReport where ID='{0}'",ID);
  469. int result = cmd.ExecuteNonQuery();
  470. cmd.Dispose();
  471. if (result > 0)
  472. return true;
  473. else
  474. return false;
  475. }
  476. static public bool IsErrorReportExist(SQLiteConnection sqliteConn, string url, string content)
  477. {
  478. try
  479. {
  480. SQLiteCommand cmd = new SQLiteCommand();
  481. cmd.Connection = sqliteConn;
  482. cmd.CommandText = string.Format("select count from ErrorReport where url='{0}' and content='{1}'", url, content);
  483. SQLiteDataReader dr = cmd.ExecuteReader();
  484. if (dr.Read())
  485. {
  486. NameValueCollection user = dr.GetValues();
  487. string values = user.Get("count");
  488. int count = Convert.ToInt32(values);
  489. dr.Close();
  490. if (count > 0)
  491. return true;
  492. cmd.Dispose();
  493. return false;
  494. }
  495. dr.Close();
  496. cmd.Dispose();
  497. return false;
  498. }
  499. catch (Exception ex)
  500. {
  501. Log.WriteErrorLog("\r\nGet Report Count Error:" + ex.Message);
  502. return false;
  503. }
  504. }
  505. static public bool InsertDelayErrorReport(SQLiteConnection sqliteConn, object[] param)
  506. {
  507. SQLiteCommand cmd = new SQLiteCommand();
  508. cmd.Connection = sqliteConn;
  509. if (param[0].ToString().EndsWith("smes/RecordKey"))
  510. {
  511. if (IsErrorReportExist(sqliteConn, param[0].ToString(), param[1].ToString()))
  512. {
  513. // 记录存在,退出;
  514. return true;
  515. }
  516. }
  517. cmd.CommandText = string.Format("insert into ErrorReport(url,content) values('{0}','{1}')", param[0], param[1]);
  518. int result = cmd.ExecuteNonQuery();
  519. cmd.Dispose();
  520. if (result > 0)
  521. return true;
  522. else
  523. return false;
  524. }
  525. public static bool CheckProductionNum(SQLiteConnection sqliteConn, string order)
  526. {
  527. SQLiteCommand cmd = new SQLiteCommand();
  528. cmd.Connection = sqliteConn;
  529. cmd.CommandText = string.Format("select * from ProductionCount where bid='{0}'", order);
  530. SQLiteDataReader dr = cmd.ExecuteReader();
  531. if (dr.Read())
  532. {
  533. dr.Close();
  534. return true;
  535. }
  536. dr.Close();
  537. cmd.CommandText = string.Format("insert into ProductionCount(bid,count) values('{0}','{1}')", order, "0");
  538. int result = cmd.ExecuteNonQuery();
  539. cmd.Dispose();
  540. if (result > 0)
  541. return true;
  542. else
  543. return false;
  544. }
  545. public static bool UpdateProductionNum(SQLiteConnection sqliteConn, string order)
  546. {
  547. try
  548. {
  549. SQLiteCommand cmd = new SQLiteCommand();
  550. cmd.Connection = sqliteConn;
  551. cmd.CommandText = string.Format("select count from ProductionCount where bid='{0}'", order);
  552. SQLiteDataReader dr = cmd.ExecuteReader();
  553. if (dr.Read())
  554. {
  555. NameValueCollection user = dr.GetValues();
  556. string values = user.Get("count");
  557. string count = (Convert.ToInt32(values) + 1).ToString();
  558. dr.Close();
  559. cmd.CommandText = string.Format("update ProductionCount set count ='{0}' where bid='{1}'", count, order);
  560. int result = cmd.ExecuteNonQuery();
  561. if (result > 0)
  562. return true;
  563. else
  564. Log.WriteErrorLog("\r\nFail to update ProductionCount from db:\r\n" + cmd.CommandText);
  565. cmd.Dispose();
  566. return false;
  567. }
  568. dr.Close();
  569. Log.WriteErrorLog("\r\nFail to get ProductionCount from db:\r\n" + cmd.CommandText);
  570. cmd.Dispose();
  571. return false;
  572. }
  573. catch(Exception ex)
  574. {
  575. Log.WriteErrorLog("\r\nFail to UpdateProductionNum:" + ex.Message);
  576. return false;
  577. }
  578. }
  579. /// <summary>
  580. /// 获取本地DB对应订单已经抄写的数量(包含重复抄写)
  581. /// </summary>
  582. /// <param name="sqliteConn"></param>
  583. /// <param name="order"></param>
  584. /// <returns></returns>
  585. public static string GetProductionNum(SQLiteConnection sqliteConn, string order)
  586. {
  587. try
  588. {
  589. SQLiteCommand cmd = new SQLiteCommand();
  590. cmd.Connection = sqliteConn;
  591. cmd.CommandText = string.Format("select count from ProductionCount where bid='{0}'", order);
  592. SQLiteDataReader dr = cmd.ExecuteReader();
  593. if (dr.Read())
  594. {
  595. NameValueCollection user = dr.GetValues();
  596. string values = user.Get("count");
  597. dr.Close();
  598. cmd.Dispose();
  599. return values;
  600. }
  601. dr.Close();
  602. cmd.Dispose();
  603. return "";
  604. }
  605. catch (Exception ex)
  606. {
  607. Log.WriteErrorLog("\r\nFail to GetProductionNum:" + ex.Message);
  608. return "";
  609. }
  610. }
  611. /// <summary>
  612. /// 删除30天之前已经上报的数据
  613. /// </summary>
  614. /// <param name="sqliteConn"></param>
  615. /// <returns></returns>
  616. public static bool DeleteOldData(SQLiteConnection sqliteConn)
  617. {
  618. try
  619. {
  620. SQLiteCommand cmd = new SQLiteCommand();
  621. cmd.Connection = sqliteConn;
  622. cmd.CommandText = "Delete from ErrorReport where report_date is not null and date('now', '-30 day') >= date(report_date)";
  623. cmd.ExecuteNonQuery();
  624. cmd.CommandText = "Delete from CopyDelayReport where report_date is not null and date('now', '-30 day') >= date(report_date)";
  625. cmd.ExecuteNonQuery();
  626. cmd.Dispose();
  627. return true;
  628. }
  629. catch(Exception ex)
  630. {
  631. GC.Collect();
  632. Log.WriteErrorLog("\r\nFail to DeleteOldData:" + ex.Message);
  633. return false;
  634. }
  635. }
  636. /// <summary>
  637. /// 获取本地DB记录的未上报异常数据
  638. /// </summary>
  639. /// <param name="sqliteConn"></param>
  640. /// <param name="url"></param>
  641. /// <param name="content"></param>
  642. /// <returns></returns>
  643. public static bool GetErrorReportData(SQLiteConnection sqliteConn,out string url,out string content,out string id)
  644. {
  645. url = "";
  646. content = "";
  647. id = "";
  648. try
  649. {
  650. SQLiteCommand cmd = new SQLiteCommand();
  651. cmd.Connection = sqliteConn;
  652. cmd.CommandText = "select url,content,ID from ErrorReport where report_date is null LIMIT 1";
  653. SQLiteDataReader dr = cmd.ExecuteReader();
  654. if (dr.Read())
  655. {
  656. NameValueCollection user = dr.GetValues();
  657. url = user.Get("url");
  658. content = user.Get("content");
  659. id = user.Get("ID");
  660. dr.Close();
  661. return true;
  662. }
  663. dr.Close();
  664. return false;
  665. }
  666. catch(Exception ex)
  667. {
  668. GC.Collect();
  669. Log.WriteErrorLog("\r\nFail to GetErrorReportData:" + ex.Message);
  670. return false;
  671. }
  672. }
  673. /// <summary>
  674. /// 获取本地DB记录的未上报烧录数据
  675. /// </summary>
  676. /// <param name="sqliteConn"></param>
  677. /// <param name="url"></param>
  678. /// <param name="content"></param>
  679. /// <returns></returns>
  680. public static bool GetDelayReportData(SQLiteConnection sqliteConn, out string url, out string content,out string id)
  681. {
  682. url = "";
  683. content = "";
  684. id = "";
  685. try
  686. {
  687. SQLiteCommand cmd = new SQLiteCommand();
  688. cmd.Connection = sqliteConn;
  689. cmd.CommandText = "select url,content,ID from CopyDelayReport where report_date is null LIMIT 1";
  690. SQLiteDataReader dr = cmd.ExecuteReader();
  691. if (dr.Read())
  692. {
  693. NameValueCollection user = dr.GetValues();
  694. url = user.Get("url");
  695. content = user.Get("content");
  696. id = user.Get("ID");
  697. dr.Close();
  698. return true;
  699. }
  700. dr.Close();
  701. return false;
  702. }
  703. catch (Exception ex)
  704. {
  705. GC.Collect();
  706. Log.WriteErrorLog("\r\nFail to GetDelayReportData:" + ex.Message);
  707. return false;
  708. }
  709. }
  710. public static bool GetEppormDataList(SQLiteConnection sqliteConn, out List<PostDateClass> postList,out List<string> SNs)
  711. {
  712. postList = new List<PostDateClass>();
  713. try
  714. {
  715. SQLiteCommand cmd = new SQLiteCommand();
  716. SNs = new List<string>();
  717. cmd.Connection = sqliteConn;
  718. cmd.CommandText = "select sn from keys where report_date is null LIMIT 200";
  719. SQLiteDataReader dr = cmd.ExecuteReader();
  720. while (dr.Read())
  721. {
  722. NameValueCollection user = dr.GetValues();
  723. string sn = user.Get("sn");
  724. SNs.Add(sn);
  725. postList.Add(new PostDateClass("sn", sn));
  726. postList.Add(new PostDateClass("date", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
  727. }
  728. dr.Close();
  729. return true;
  730. }
  731. catch (Exception ex)
  732. {
  733. GC.Collect();
  734. SNs = null;
  735. Log.WriteErrorLog("\r\nFail to GetEppormData:" + ex.Message);
  736. return false;
  737. }
  738. }
  739. public static bool GetEppormDataListV2(SQLiteConnection sqliteConn, out List<string> listSN, out List<string> listCopydates)
  740. {
  741. try
  742. {
  743. listSN = new List<string>();
  744. listCopydates = new List<string>();
  745. SQLiteCommand cmd = new SQLiteCommand();
  746. cmd.Connection = sqliteConn;
  747. cmd.CommandText = "select sn,copydate from keys where report_date is null LIMIT 200";
  748. SQLiteDataReader dr = cmd.ExecuteReader();
  749. while (dr.Read())
  750. {
  751. NameValueCollection user = dr.GetValues();
  752. listSN.Add(user.Get("sn"));
  753. listCopydates.Add(user.Get("copydate").ToString());
  754. }
  755. dr.Close();
  756. return true;
  757. }
  758. catch (Exception ex)
  759. {
  760. GC.Collect();
  761. listSN = null;
  762. listCopydates = null;
  763. Log.WriteErrorLog("\r\nFail to GetEppormData:" + ex.Message);
  764. return false;
  765. }
  766. }
  767. public static bool GetEppormDataCount(SQLiteConnection sqliteConn, out string count)
  768. {
  769. count = "";
  770. try
  771. {
  772. SQLiteCommand cmd = new SQLiteCommand();
  773. cmd.Connection = sqliteConn;
  774. cmd.CommandText = "select count(*) from keys";
  775. SQLiteDataReader dr = cmd.ExecuteReader();
  776. if (dr.Read())
  777. {
  778. NameValueCollection user = dr.GetValues();
  779. count = user.Get("count(*)");
  780. dr.Close();
  781. return true;
  782. }
  783. dr.Close();
  784. return false;
  785. }
  786. catch (Exception ex)
  787. {
  788. GC.Collect();
  789. Log.WriteErrorLog("\r\nFail to GetEppormDataCount:" + ex.Message);
  790. return false;
  791. }
  792. }
  793. public static bool GetEppormUploadedDataCount(SQLiteConnection sqliteConn, out string count)
  794. {
  795. count = "";
  796. try
  797. {
  798. SQLiteCommand cmd = new SQLiteCommand();
  799. cmd.Connection = sqliteConn;
  800. cmd.CommandText = "select count(*) from keys where report_date is not null";
  801. SQLiteDataReader dr = cmd.ExecuteReader();
  802. if (dr.Read())
  803. {
  804. NameValueCollection user = dr.GetValues();
  805. count = user.Get("count(*)");
  806. dr.Close();
  807. return true;
  808. }
  809. dr.Close();
  810. return false;
  811. }
  812. catch (Exception ex)
  813. {
  814. GC.Collect();
  815. Log.WriteErrorLog("\r\nFail to GetEppormDataCount:" + ex.Message);
  816. return false;
  817. }
  818. }
  819. /// <summary>
  820. /// 更新本地DB记录的未上报数据
  821. /// </summary>
  822. public static bool UpdateReportData(SQLiteConnection sqliteConn,string table,string column,string data)
  823. {
  824. SQLiteCommand cmd = new SQLiteCommand();
  825. cmd.Connection = sqliteConn;
  826. cmd.CommandText = string.Format("update {0} set report_date = datetime(CURRENT_TIMESTAMP,'localtime') where {1}= '{2}'", table, column, data);
  827. int result = cmd.ExecuteNonQuery();
  828. cmd.Dispose();
  829. if (result > 0)
  830. return true;
  831. else
  832. return false;
  833. }
  834. public static bool AddReportOnlineData(SQLiteConnection connection, string url, string content)
  835. {
  836. if (connection == null)
  837. return false;
  838. SQLiteCommand cmd = new SQLiteCommand();
  839. cmd.Connection = connection;
  840. cmd.CommandText = string.Format("insert into ReportData(url,content) values('{0}','{1}')", url, content);
  841. int result = cmd.ExecuteNonQuery();
  842. cmd.Dispose();
  843. return result > 0 ? true : false;
  844. }
  845. public static bool GetReportData(SQLiteConnection sqliteConn, out string id, out string url, out string content)
  846. {
  847. url = "";
  848. content = "";
  849. id = "";
  850. try
  851. {
  852. SQLiteCommand cmd = new SQLiteCommand();
  853. cmd.Connection = sqliteConn;
  854. cmd.CommandText = "select id, url, content from ReportData Order by random() limit 1";
  855. SQLiteDataReader dr = cmd.ExecuteReader();
  856. if (dr.Read())
  857. {
  858. NameValueCollection user = dr.GetValues();
  859. url = user.Get("url");
  860. content = user.Get("content");
  861. id = user.Get("id");
  862. dr.Close();
  863. return true;
  864. }
  865. dr.Close();
  866. return false;
  867. }
  868. catch (Exception ex)
  869. {
  870. GC.Collect();
  871. Log.WriteErrorLog("\r\nFail to GetReportData:" + ex.Message);
  872. return false;
  873. }
  874. }
  875. }
  876. }