MeasureDB.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. public MeasureDB(MeasureFile mf)
  19. {
  20. Init(mf);
  21. CreateDBconn();
  22. CreateAnalysisPointsTable();
  23. CreateXrayDataTable();
  24. CreateAreaPositionTable();
  25. CreateElementTable();
  26. }
  27. public void Init(MeasureFile mf)
  28. {
  29. m_measureFile = mf;
  30. }
  31. //创建一个空的数据库
  32. public void CreateDBconn()
  33. {
  34. string path;//= Path.GetDirectoryName(m_measureFile.FilePath);
  35. //获取测量文件所在路径
  36. string savePath = m_measureFile.FilePath;
  37. //数据库名称
  38. string dbFileName = "MeasureDB.db";
  39. path = savePath + dbFileName;
  40. //if (File.Exists(path))
  41. //{
  42. // File.Delete(path);
  43. //}
  44. m_db = new SQLiteConnection("data source =" + path);
  45. m_db.Open();
  46. }
  47. //关闭数据库
  48. public void CloseDB()
  49. {
  50. m_db.Close();
  51. }
  52. //创建分析点表
  53. public void CreateAnalysisPointsTable()
  54. {
  55. string sql = "CREATE TABLE AnalysisPoints(";
  56. string sPrimary = ", PRIMARY KEY( ";
  57. //第1列, ID,是主键
  58. sql = sql + "ID INTEGER";
  59. //sPrimary = sPrimary + "ID";
  60. //第2列, ImageID,是主键
  61. sql = sql + ",";
  62. sql = sql + "ImageID INTEGER";
  63. //第3列,Name
  64. sql = sql + ",";
  65. sql = sql + "Name TEXT";
  66. //第4列,EDSImagePath
  67. sql = sql + ",";
  68. sql = sql + "EDSImagePath TEXT";
  69. //第5列,HavePoints
  70. sql = sql + ",";
  71. sql = sql + "HavePoints INTEGER";
  72. //第6列,PointsNum
  73. sql = sql + ",";
  74. sql = sql + "PointsNum INTEGER";
  75. //第7列,HaveAreas
  76. sql = sql + ",";
  77. sql = sql + "HaveAreas INTEGER";
  78. //第8列,AreasNum
  79. sql = sql + ",";
  80. sql = sql + "AreasNum INTEGER";
  81. //sPrimary = sPrimary + ")";
  82. //sql = sql + sPrimary + ")";
  83. sql = sql + ")";
  84. SQLiteCommand command = new SQLiteCommand(sql, m_db);
  85. command.ExecuteNonQuery();
  86. }
  87. //XrayData表
  88. public void CreateXrayDataTable()
  89. {
  90. string sql = "CREATE TABLE XrayData(";
  91. string sPrimary = ", PRIMARY KEY( ";
  92. //第1列, ID,是主键
  93. sql = sql + "ID INTEGER";
  94. sPrimary = sPrimary + "ID";
  95. //第2列,APID
  96. sql = sql + ",";
  97. sql = sql + "APID INTEGER";
  98. sPrimary = sPrimary + ", APID";
  99. //第3列,图像ID
  100. sql = sql + ",";
  101. sql = sql + "ImageID INTEGER";
  102. //第4列,Type
  103. sql = sql + ",";
  104. sql = sql + "Type INTEGER";
  105. //第5列,X
  106. sql = sql + ",";
  107. sql = sql + "X INTEGER";
  108. //第6列,Y
  109. sql = sql + ",";
  110. sql = sql + "Y INTEGER";
  111. //第7列,AreasID
  112. sql = sql + ",";
  113. sql = sql + "AreasID INTEGER";
  114. //第8列,ElementNum
  115. sql = sql + ",";
  116. sql = sql + "ElementNum INTEGER";
  117. //第9列,XrayData
  118. sql = sql + ",";
  119. sql = sql + "XrayData BLOB";
  120. sPrimary = sPrimary + ")";
  121. sql = sql + sPrimary + ")";
  122. SQLiteCommand command = new SQLiteCommand(sql, m_db);
  123. command.ExecuteNonQuery();
  124. }
  125. //创建元素表
  126. public void CreateElementTable()
  127. {
  128. string sql = "CREATE TABLE Element(";
  129. string sPrimary = ", PRIMARY KEY( ";
  130. //第1列, ID,是主键
  131. sql = sql + "XayID INTEGER";
  132. sPrimary = sPrimary + "XayID";
  133. //第3列,ElementNum,是主键
  134. sql = sql + ",";
  135. sql = sql + "ElementNum INTEGER";
  136. sPrimary = sPrimary + ", ElementNum";
  137. //第4列,ElementID,是主键
  138. sql = sql + ",";
  139. sql = sql + "ElementID INTEGER";
  140. sPrimary = sPrimary + ", ElementID";
  141. //第5列,Name
  142. sql = sql + ",";
  143. sql = sql + "Name TEXT";
  144. //第6列,Percent
  145. sql = sql + ",";
  146. sql = sql + "Percent FLOAT";
  147. sPrimary = sPrimary + ")";
  148. sql = sql + sPrimary + ")";
  149. SQLiteCommand command = new SQLiteCommand(sql, m_db);
  150. command.ExecuteNonQuery();
  151. }
  152. //创建位置信息表
  153. public void CreateAreaPositionTable()
  154. {
  155. string sql = "CREATE TABLE AreaPosition(";
  156. string sPrimary = ", PRIMARY KEY( ";
  157. //第1列, ID,是主键
  158. sql = sql + "XrayID INTEGER";
  159. sPrimary = sPrimary + "XrayID";
  160. //第2列,AID
  161. sql = sql + ",";
  162. sql = sql + "AID INTEGER";
  163. sPrimary = sPrimary + ", AID";
  164. //第3列,SegNum
  165. sql = sql + ",";
  166. sql = sql + "SegNum INTEGER";
  167. sPrimary = sPrimary + ", SegNum";
  168. //第4列,SegID
  169. sql = sql + ",";
  170. sql = sql + "SegID INTEGER";
  171. sPrimary = sPrimary + ", SegID";
  172. //第5列,X
  173. sql = sql + ",";
  174. sql = sql + "X INTEGER";
  175. //第6列,Y
  176. sql = sql + ",";
  177. sql = sql + "Y INTEGER";
  178. //第7列,Length
  179. sql = sql + ",";
  180. sql = sql + "Length INTEGER";
  181. sPrimary = sPrimary + ")";
  182. sql = sql + sPrimary + ")";
  183. SQLiteCommand command = new SQLiteCommand(sql, m_db);
  184. command.ExecuteNonQuery();
  185. }
  186. public bool InsertAnylysisField(int PointsId, string PointName, string EDSImagePath, int PointsNum, int AreasNum, int ImageID)
  187. {
  188. //开启一个事务
  189. using (SQLiteTransaction tr = m_db.BeginTransaction())
  190. {
  191. //向分析点表插入数据
  192. string sql = "INSERT INTO AnalysisPoints(ID, ImageID, Name, EDSImagePath, HavePoints, PointsNum, HaveAreas, AreasNum) values ("
  193. + PointsId.ToString() + ","
  194. + ImageID.ToString() + ","
  195. + "'" + PointName + "'" + ","
  196. + "'" + EDSImagePath + "'" + ","
  197. + Convert.ToInt32(Convert.ToBoolean(PointsNum)).ToString() + ","
  198. + PointsNum.ToString() + ","
  199. + Convert.ToInt32(Convert.ToBoolean(AreasNum)).ToString() + ","
  200. + AreasNum.ToString() + ")";
  201. SQLiteCommand command = new SQLiteCommand(sql, m_db);
  202. command.ExecuteNonQuery();
  203. tr.Commit();
  204. }
  205. return true;
  206. }
  207. //插入一条点扫描Xray的信息
  208. public bool InsertAPointXray(int PointsId, int XrayId, int X, int Y, long[] XrayData, Dictionary<string, double> listElement, int ImageID)
  209. {
  210. byte[] bArray = new byte[8000];
  211. for (int i = 0; i < 2000; i++)
  212. {
  213. byte[] temps = BitConverter.GetBytes(XrayData[i]);
  214. bArray[i * 4] = temps[0];
  215. bArray[i * 4 + 1] = temps[1];
  216. bArray[i * 4 + 2] = temps[2];
  217. bArray[i * 4 + 3] = temps[3];
  218. }
  219. //开启一个事务
  220. using (SQLiteTransaction tr = m_db.BeginTransaction())
  221. {
  222. //向能谱数据表插入数据
  223. string sql = "INSERT INTO XrayData(ID, APID, ImageID, Type, X, Y, AreasID, ElementNum, XrayData) values ("
  224. + XrayId.ToString() + ","
  225. + PointsId.ToString() + ","
  226. + ImageID.ToString() + ","
  227. + 0.ToString() + ","
  228. + X.ToString() + ","
  229. + Y.ToString() + ","
  230. + (-1).ToString() + ","
  231. + listElement.Count.ToString() + ",:XrayData)";
  232. SQLiteCommand command = new SQLiteCommand(sql, m_db);
  233. command.Parameters.Add("XrayData", System.Data.DbType.Binary).Value = bArray; // BLOB
  234. command.ExecuteNonQuery();
  235. //向能谱元素表插入数据
  236. int i = 1;
  237. foreach (KeyValuePair<string, double> element in listElement)
  238. {
  239. sql = "INSERT INTO Element(XayID, ElementNum, ElementID, Name, Percent) values ("
  240. + XrayId.ToString() + ","
  241. + listElement.Count.ToString() + ","
  242. + i.ToString() + ",'"
  243. + element.Key + "',"
  244. + element.Value.ToString() + ")";
  245. command = new SQLiteCommand(sql, m_db);
  246. command.ExecuteNonQuery();
  247. i++;
  248. }
  249. //向位置表插入数据
  250. sql = "INSERT INTO AreaPosition(XrayID, AID, SegNum, SegID, X, Y, Length) values ("
  251. + XrayId.ToString() + ","
  252. + PointsId.ToString() + ","
  253. + (-1).ToString() + ","
  254. + (-1).ToString() + ","
  255. + X.ToString() + ","
  256. + Y.ToString() + ","
  257. + (0).ToString() + ")";
  258. command = new SQLiteCommand(sql, m_db);
  259. command.ExecuteNonQuery();
  260. tr.Commit();
  261. Thread.Sleep(500);
  262. }
  263. return true;
  264. }
  265. //插入一条面扫描Xray的信息
  266. public bool InsertAAreaXay(int PointsId, int XrayId, int AreaId, List<Segment> listSeg, long[] XrayData, Dictionary<string, double> listElement, int ImageID)
  267. {
  268. byte[] bArray = new byte[8000];
  269. for (int i = 0; i < 2000; i++)
  270. {
  271. byte[] temps = BitConverter.GetBytes(XrayData[i]);
  272. bArray[i] = temps[0];
  273. bArray[i + 1] = temps[1];
  274. bArray[i + 2] = temps[2];
  275. bArray[i + 3] = temps[3];
  276. }
  277. //开启一个事务
  278. using (SQLiteTransaction tr = m_db.BeginTransaction())
  279. {
  280. //向能谱数据表插入数据
  281. string sql = "INSERT INTO XrayData(ID, APID, ImageID, Type, X, Y, AreasID, ElementNum, XrayData) values ("
  282. + XrayId.ToString() + ","
  283. + PointsId.ToString() + ","
  284. + ImageID.ToString() + ","
  285. + 1.ToString() + ","
  286. + (-1).ToString() + ","
  287. + (-1).ToString() + ","
  288. + AreaId.ToString() + ","
  289. + listElement.Count.ToString() + ",:XrayData)";
  290. SQLiteCommand command = new SQLiteCommand(sql, m_db);
  291. command.Parameters.Add("XrayData", System.Data.DbType.Binary).Value = bArray; // BLOB
  292. command.ExecuteNonQuery();
  293. //向能谱元素表插入数据
  294. int i = 1;
  295. foreach (KeyValuePair<string, double> element in listElement)
  296. {
  297. sql = "INSERT INTO Element(XayID, ElementNum, ElementID, Name, Percent) values ("
  298. + XrayId.ToString() + ","
  299. + listElement.Count.ToString() + ","
  300. + i.ToString() + ",'"
  301. + element.Key + "',"
  302. + element.Value.ToString() + ")";
  303. command = new SQLiteCommand(sql, m_db);
  304. command.ExecuteNonQuery();
  305. i++;
  306. }
  307. //向位置表插入数据
  308. i = 1;
  309. foreach (Segment seg in listSeg)
  310. {
  311. sql = "INSERT INTO AreaPosition(XrayID, AID, SegNum, SegID, X, Y, Length) values ("
  312. + XrayId.ToString() + ","
  313. + PointsId.ToString() + ","
  314. + listSeg.Count.ToString() + ","
  315. + i.ToString() + ","
  316. + seg.X.ToString() + ","
  317. + seg.Y.ToString() + ","
  318. + seg.Length.ToString() + ")";
  319. command = new SQLiteCommand(sql, m_db);
  320. command.ExecuteNonQuery();
  321. i++;
  322. }
  323. tr.Commit();
  324. }
  325. return true;
  326. }
  327. }
  328. }