MeasureDB.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. //20201104 数据库基本操作 只包含创建和写入的动作,删除和读出查询的动作不包括在其中
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.IO;
  8. using System.Threading;
  9. using System.Data.SQLite;
  10. using MeasureData;
  11. using Extender;
  12. namespace DBManager
  13. {
  14. public class MeasureDB
  15. {
  16. public SQLiteConnection m_db;
  17. private MeasureFile m_measureFile; //测量文件
  18. private List<byte[]> LineXraybyte = new List<byte[]>();
  19. private List<string> LineXraystring = new List<string>();
  20. private List<string> LineXray = new List<string>();
  21. private List<byte[]> PoinXraybyte = new List<byte[]>();
  22. private List<string> PoinXraystring = new List<string>();
  23. private List<string> PoinXray = new List<string>();
  24. public MeasureDB(MeasureFile mf)
  25. {
  26. Init(mf);
  27. CreateDBconn();
  28. CreateAnalysisPointsTable();
  29. CreateXrayDataTable();
  30. CreateAreaPositionTable();
  31. CreateElementTable();
  32. }
  33. public void Init(MeasureFile mf)
  34. {
  35. m_measureFile = mf;
  36. }
  37. //创建一个空的数据库
  38. public void CreateDBconn()
  39. {
  40. string path;//= Path.GetDirectoryName(m_measureFile.FilePath);
  41. //获取测量文件所在路径
  42. string savePath = m_measureFile.FilePath;
  43. //数据库名称
  44. string dbFileName = "MeasureDB.db";
  45. path = savePath + "//" + dbFileName;
  46. //if (File.Exists(path))
  47. //{
  48. // File.Delete(path);
  49. //}
  50. m_db = new SQLiteConnection("data source =" + path);
  51. //m_db = new SQLiteConnection("data source =" + @"D:\HOZ\数据库测试\MeasureDB.db");
  52. m_db.Open();
  53. }
  54. //关闭数据库
  55. public void CloseDB()
  56. {
  57. m_db.Close();
  58. }
  59. //创建分析点表
  60. public void CreateAnalysisPointsTable()
  61. {
  62. string sql = "CREATE TABLE AnalysisPoints(";
  63. string sPrimary = ", PRIMARY KEY( ";
  64. //第1列, ID,是主键
  65. sql = sql + "ID INTEGER";
  66. //sPrimary = sPrimary + "ID";
  67. //第2列, ImageID,是主键
  68. sql = sql + ",";
  69. sql = sql + "ImageID INTEGER";
  70. //第3列,Name
  71. sql = sql + ",";
  72. sql = sql + "Name TEXT";
  73. //第4列,EDSImagePath
  74. sql = sql + ",";
  75. sql = sql + "EDSImagePath TEXT";
  76. //第5列,HavePoints
  77. sql = sql + ",";
  78. sql = sql + "HavePoints INTEGER";
  79. //第6列,PointsNum
  80. sql = sql + ",";
  81. sql = sql + "PointsNum INTEGER";
  82. //第7列,HaveAreas
  83. sql = sql + ",";
  84. sql = sql + "HaveAreas INTEGER";
  85. //第8列,AreasNum
  86. sql = sql + ",";
  87. sql = sql + "AreasNum INTEGER";
  88. //sPrimary = sPrimary + ")";
  89. //sql = sql + sPrimary + ")";
  90. sql = sql + ")";
  91. SQLiteCommand command = new SQLiteCommand(sql, m_db);
  92. command.ExecuteNonQuery();
  93. }
  94. //XrayData表
  95. public void CreateXrayDataTable()
  96. {
  97. string sql = "CREATE TABLE XrayData(";
  98. string sPrimary = ", PRIMARY KEY( ";
  99. //第1列, ID,是主键
  100. sql = sql + "ID INTEGER";
  101. sPrimary = sPrimary + "ID";
  102. //第2列,APID
  103. sql = sql + ",";
  104. sql = sql + "APID INTEGER";
  105. sPrimary = sPrimary + ", APID";
  106. //第3列,图像ID
  107. sql = sql + ",";
  108. sql = sql + "ImageID INTEGER";
  109. //第4列,Type
  110. sql = sql + ",";
  111. sql = sql + "Type INTEGER";
  112. //第5列,X
  113. sql = sql + ",";
  114. sql = sql + "X INTEGER";
  115. //第6列,Y
  116. sql = sql + ",";
  117. sql = sql + "Y INTEGER";
  118. //第7列,AreasID
  119. sql = sql + ",";
  120. sql = sql + "AreasID INTEGER";
  121. //第8列,ElementNum
  122. sql = sql + ",";
  123. sql = sql + "ElementNum INTEGER";
  124. //第9列,XrayData
  125. sql = sql + ",";
  126. sql = sql + "XrayData BLOB";
  127. sPrimary = sPrimary + ")";
  128. sql = sql + sPrimary + ")";
  129. SQLiteCommand command = new SQLiteCommand(sql, m_db);
  130. command.ExecuteNonQuery();
  131. }
  132. //创建元素表
  133. public void CreateElementTable()
  134. {
  135. string sql = "CREATE TABLE Element(";
  136. string sPrimary = ", PRIMARY KEY( ";
  137. //第1列, ID,是主键
  138. sql = sql + "XayID INTEGER";
  139. sPrimary = sPrimary + "XayID";
  140. //第3列,ElementNum,是主键
  141. sql = sql + ",";
  142. sql = sql + "ElementNum INTEGER";
  143. sPrimary = sPrimary + ", ElementNum";
  144. //第4列,ElementID,是主键
  145. sql = sql + ",";
  146. sql = sql + "ElementID INTEGER";
  147. sPrimary = sPrimary + ", ElementID";
  148. //第5列,Name
  149. sql = sql + ",";
  150. sql = sql + "Name TEXT";
  151. //第6列,Percent
  152. sql = sql + ",";
  153. sql = sql + "Percent FLOAT";
  154. sPrimary = sPrimary + ")";
  155. sql = sql + sPrimary + ")";
  156. SQLiteCommand command = new SQLiteCommand(sql, m_db);
  157. command.ExecuteNonQuery();
  158. }
  159. //创建位置信息表
  160. public void CreateAreaPositionTable()
  161. {
  162. string sql = "CREATE TABLE AreaPosition(";
  163. string sPrimary = ", PRIMARY KEY( ";
  164. //第1列, ID,是主键
  165. sql = sql + "XrayID INTEGER";
  166. sPrimary = sPrimary + "XrayID";
  167. //第2列,AID
  168. sql = sql + ",";
  169. sql = sql + "AID INTEGER";
  170. sPrimary = sPrimary + ", AID";
  171. //第3列,SegNum
  172. sql = sql + ",";
  173. sql = sql + "SegNum INTEGER";
  174. sPrimary = sPrimary + ", SegNum";
  175. //第4列,SegID
  176. sql = sql + ",";
  177. sql = sql + "SegID INTEGER";
  178. sPrimary = sPrimary + ", SegID";
  179. //第5列,X
  180. sql = sql + ",";
  181. sql = sql + "X INTEGER";
  182. //第6列,Y
  183. sql = sql + ",";
  184. sql = sql + "Y INTEGER";
  185. //第7列,Length
  186. sql = sql + ",";
  187. sql = sql + "Length INTEGER";
  188. sPrimary = sPrimary + ")";
  189. sql = sql + sPrimary + ")";
  190. SQLiteCommand command = new SQLiteCommand(sql, m_db);
  191. command.ExecuteNonQuery();
  192. }
  193. public bool InsertAnylysisField(int PointsId, string PointName, string EDSImagePath, int PointsNum, int AreasNum, int ImageID)
  194. {
  195. //开启一个事务
  196. using (SQLiteTransaction tr = m_db.BeginTransaction())
  197. {
  198. //向分析点表插入数据
  199. string sql = "INSERT INTO AnalysisPoints(ID, ImageID, Name, EDSImagePath, HavePoints, PointsNum, HaveAreas, AreasNum) values ("
  200. + PointsId.ToString() + ","
  201. + ImageID.ToString() + ","
  202. + "'" + PointName + "'" + ","
  203. + "'" + EDSImagePath + "'" + ","
  204. + Convert.ToInt32(Convert.ToBoolean(PointsNum)).ToString() + ","
  205. + PointsNum.ToString() + ","
  206. + Convert.ToInt32(Convert.ToBoolean(AreasNum)).ToString() + ","
  207. + AreasNum.ToString() + ")";
  208. SQLiteCommand command = new SQLiteCommand(sql, m_db);
  209. command.ExecuteNonQuery();
  210. tr.Commit();
  211. }
  212. return true;
  213. }
  214. //插入一条点扫描Xray的信息
  215. public bool InsertAPointXray(int PointsId, int XrayId, int X, int Y, long[] XrayData, Dictionary<string, double> listElement, int ImageID)
  216. {
  217. byte[] bArray = new byte[8000];
  218. for (int a = 0; a < 2000; a++)
  219. {
  220. byte[] temps = BitConverter.GetBytes(XrayData[a]);
  221. bArray[a * 4] = temps[0];
  222. bArray[a * 4 + 1] = temps[1];
  223. bArray[a * 4 + 2] = temps[2];
  224. bArray[a * 4 + 3] = temps[3];
  225. }
  226. //开启一个事务
  227. //using (SQLiteTransaction tr = m_db.BeginTransaction())
  228. //{
  229. //向能谱数据表插入数据
  230. string sql = "INSERT INTO XrayData(ID, APID, ImageID, Type, X, Y, AreasID, ElementNum, XrayData) values ("
  231. + XrayId.ToString() + ","
  232. + PointsId.ToString() + ","
  233. + ImageID.ToString() + ","
  234. + 0.ToString() + ","
  235. + X.ToString() + ","
  236. + Y.ToString() + ","
  237. + (-1).ToString() + ","
  238. + listElement.Count.ToString() + ",:XrayData)";
  239. PoinXraybyte.Add(bArray);
  240. PoinXraystring.Add(sql);
  241. //SQLiteCommand command = new SQLiteCommand(sql, m_db);
  242. //command.Parameters.Add("XrayData", System.Data.DbType.Binary).Value = bArray; // BLOB
  243. //command.ExecuteNonQuery();
  244. //向能谱元素表插入数据
  245. int i = 1;
  246. foreach (KeyValuePair<string, double> element in listElement)
  247. {
  248. sql = "INSERT INTO Element(XayID, ElementNum, ElementID, Name, Percent) values ("
  249. + XrayId.ToString() + ","
  250. + listElement.Count.ToString() + ","
  251. + i.ToString() + ",'"
  252. + element.Key + "',"
  253. + element.Value.ToString() + ")";
  254. PoinXray.Add(sql);
  255. //command = new SQLiteCommand(sql, m_db);
  256. //command.ExecuteNonQuery();
  257. i++;
  258. }
  259. //向位置表插入数据
  260. sql = "INSERT INTO AreaPosition(XrayID, AID, SegNum, SegID, X, Y, Length) values ("
  261. + XrayId.ToString() + ","
  262. + PointsId.ToString() + ","
  263. + (-1).ToString() + ","
  264. + (-1).ToString() + ","
  265. + X.ToString() + ","
  266. + Y.ToString() + ","
  267. + (0).ToString() + ")";
  268. PoinXray.Add(sql);
  269. //command = new SQLiteCommand(sql, m_db);
  270. //command.ExecuteNonQuery();
  271. //tr.Commit();
  272. Thread.Sleep(200);
  273. //}
  274. return true;
  275. }
  276. //插入一条面扫描Xray的信息
  277. public bool InsertAAreaXay(int PointsId, int XrayId, int AreaId, List<Segment> listSeg, long[] XrayData, Dictionary<string, double> listElement, int ImageID)
  278. {
  279. byte[] bArray = new byte[8000];
  280. for (int i = 0; i < 2000; i++)
  281. {
  282. byte[] temps = BitConverter.GetBytes(XrayData[i]);
  283. bArray[i*4] = temps[0];
  284. bArray[i*4 + 1] = temps[1];
  285. bArray[i*4 + 2] = temps[2];
  286. bArray[i*4 + 3] = temps[3];
  287. }
  288. //开启一个事务
  289. using (SQLiteTransaction tr = m_db.BeginTransaction())
  290. {
  291. //向能谱数据表插入数据
  292. string sql = "INSERT INTO XrayData(ID, APID, ImageID, Type, X, Y, AreasID, ElementNum, XrayData) values ("
  293. + XrayId.ToString() + ","
  294. + PointsId.ToString() + ","
  295. + ImageID.ToString() + ","
  296. + 1.ToString() + ","
  297. + (-1).ToString() + ","
  298. + (-1).ToString() + ","
  299. + AreaId.ToString() + ","
  300. + listElement.Count.ToString() + ",:XrayData)";
  301. SQLiteCommand command = new SQLiteCommand(sql, m_db);
  302. command.Parameters.Add("XrayData", System.Data.DbType.Binary).Value = bArray; // BLOB
  303. command.ExecuteNonQuery();
  304. //向能谱元素表插入数据
  305. int i = 1;
  306. foreach (KeyValuePair<string, double> element in listElement)
  307. {
  308. sql = "INSERT INTO Element(XayID, ElementNum, ElementID, Name, Percent) values ("
  309. + XrayId.ToString() + ","
  310. + listElement.Count.ToString() + ","
  311. + i.ToString() + ",'"
  312. + element.Key + "',"
  313. + element.Value.ToString() + ")";
  314. command = new SQLiteCommand(sql, m_db);
  315. command.ExecuteNonQuery();
  316. i++;
  317. }
  318. //向位置表插入数据
  319. i = 1;
  320. foreach (Segment seg in listSeg)
  321. {
  322. sql = "INSERT INTO AreaPosition(XrayID, AID, SegNum, SegID, X, Y, Length) values ("
  323. + XrayId.ToString() + ","
  324. + PointsId.ToString() + ","
  325. + listSeg.Count.ToString() + ","
  326. + i.ToString() + ","
  327. + seg.X.ToString() + ","
  328. + seg.Y.ToString() + ","
  329. + seg.Length.ToString() + ")";
  330. command = new SQLiteCommand(sql, m_db);
  331. command.ExecuteNonQuery();
  332. i++;
  333. }
  334. tr.Commit();
  335. }
  336. return true;
  337. }
  338. public bool SaveToMemory(int PointsId, int XrayId, int AreaId, List<Segment> listSeg, long[] XrayData, Dictionary<string, double> listElement, int ImageID)
  339. {
  340. byte[] bArray = new byte[8000];
  341. for (int a = 0; a < 2000; a++)
  342. {
  343. byte[] temps = BitConverter.GetBytes(XrayData[a]);
  344. bArray[a * 4] = temps[0];
  345. bArray[a * 4 + 1] = temps[1];
  346. bArray[a * 4 + 2] = temps[2];
  347. bArray[a * 4 + 3] = temps[3];
  348. }
  349. //向能谱数据表插入数据
  350. string sql = "INSERT INTO XrayData(ID, APID, ImageID, Type, X, Y, AreasID, ElementNum, XrayData) values ("
  351. + XrayId.ToString() + ","
  352. + PointsId.ToString() + ","
  353. + ImageID.ToString() + ","
  354. + 1.ToString() + ","
  355. + (-1).ToString() + ","
  356. + (-1).ToString() + ","
  357. + AreaId.ToString() + ","
  358. + listElement.Count.ToString() + ",:XrayData)";
  359. LineXraystring.Add(sql);
  360. LineXraybyte.Add(bArray);
  361. //向能谱元素表插入数据
  362. int i = 1;
  363. foreach (KeyValuePair<string, double> element in listElement)
  364. {
  365. sql = "INSERT INTO Element(XayID, ElementNum, ElementID, Name, Percent) values ("
  366. + XrayId.ToString() + ","
  367. + listElement.Count.ToString() + ","
  368. + i.ToString() + ",'"
  369. + element.Key + "',"
  370. + element.Value.ToString() + ")";
  371. LineXray.Add(sql);
  372. i++;
  373. }
  374. //向位置表插入数据
  375. i = 1;
  376. foreach (Segment seg in listSeg)
  377. {
  378. sql = "INSERT INTO AreaPosition(XrayID, AID, SegNum, SegID, X, Y, Length) values ("
  379. + XrayId.ToString() + ","
  380. + PointsId.ToString() + ","
  381. + listSeg.Count.ToString() + ","
  382. + i.ToString() + ","
  383. + seg.X.ToString() + ","
  384. + seg.Y.ToString() + ","
  385. + seg.Length.ToString() + ")";
  386. LineXray.Add(sql);
  387. i++;
  388. }
  389. return true;
  390. }
  391. public bool PointDataSubmitted()
  392. {
  393. using (SQLiteTransaction tr = m_db.BeginTransaction())
  394. {
  395. for (int i = 0; i < PoinXraystring.Count(); i++)
  396. {
  397. SQLiteCommand command = new SQLiteCommand(PoinXraystring[i], m_db);
  398. command.Parameters.Add("XrayData", System.Data.DbType.Binary).Value = PoinXraybyte[i]; // BLOB
  399. command.ExecuteNonQuery();
  400. }
  401. for (int i = 0; i < PoinXray.Count(); i++)
  402. {
  403. SQLiteCommand command = new SQLiteCommand(PoinXray[i], m_db);
  404. command.ExecuteNonQuery();
  405. }
  406. tr.Commit();
  407. }
  408. PoinXraystring.Clear();
  409. PoinXraybyte.Clear();
  410. PoinXray.Clear();
  411. return true;
  412. }
  413. public bool LineDataSubmitted()
  414. {
  415. using (SQLiteTransaction tr = m_db.BeginTransaction())
  416. {
  417. for (int i = 0; i < LineXraystring.Count(); i++)
  418. {
  419. SQLiteCommand command = new SQLiteCommand(LineXraystring[i], m_db);
  420. command.Parameters.Add("XrayData", System.Data.DbType.Binary).Value = LineXraybyte[i]; // BLOB
  421. command.ExecuteNonQuery();
  422. }
  423. for (int i = 0; i < LineXray.Count(); i++)
  424. {
  425. SQLiteCommand command = new SQLiteCommand(LineXray[i], m_db);
  426. command.ExecuteNonQuery();
  427. }
  428. tr.Commit();
  429. }
  430. LineXraystring.Clear();
  431. LineXraybyte.Clear();
  432. LineXray.Clear();
  433. return true;
  434. }
  435. }
  436. }