SQliteManageClass.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using SqLiteHelperNamespace;
  7. using System.IO;
  8. using OTSINTERFACE;
  9. using System.Windows.Forms;
  10. namespace OTSSysMgrTools
  11. {
  12. public class SQliteManageClass
  13. {
  14. private List<string> IncAId = new List<string>();
  15. private SqLiteHelper sql = null;
  16. //系统STD文件路径
  17. static string RunPath = Application.StartupPath;
  18. static string str = "\\Config\\SysData";
  19. static string m_ParameterPath = RunPath + str;
  20. private string m_STDDBPath = m_ParameterPath;
  21. //系统STD文件 数据库名称
  22. private string m_STDDBName = "SySSTDData.db";
  23. //系统STD文件 表名
  24. private const string m_STDTableName = "IncALib";
  25. //测量结果本地数据库名称
  26. private string m_MeasureResultInclusionDBName = "Inclusion.db";
  27. //夹杂物表名称
  28. private string m_MeasureResultInclusionTableName = "IncAData";
  29. #region 通过FieldID、XrayID 获取归属元素名称
  30. /// <summary>
  31. /// 通过FieldID、XrayID 获取归属元素名称
  32. /// </summary>
  33. /// <param name="FieldID"></param>
  34. /// <param name="XrayID"></param>
  35. /// <param name="MeasureResultFilePath">测量结果文件路径</param>
  36. /// <param name="STDDBName">标准库名称</param>
  37. /// <returns>元素名称</returns>
  38. public string GetSySSTDData1IncALibName(int FieldID, int XrayID, string MeasureResultFilePath, string STDDBName)
  39. {
  40. try
  41. {
  42. //设置全局STD名称变量
  43. m_STDDBName = STDDBName;
  44. string IncAName = string.Empty;
  45. //需要循环文件夹判断在哪一个库Inclusion.db 获取IncAId
  46. //判断当前路径下是否存在FIELD_FILES文件夹,返回数据文件路径
  47. DataTable sqldt = ReturnDataInFilePath(FieldID, XrayID, MeasureResultFilePath);
  48. if (sqldt != null)
  49. {
  50. if (sqldt.Rows.Count > 0)
  51. {
  52. sql = new SqLiteHelper("data source='" + m_STDDBPath + "\\" + STDDBName + "'");
  53. //通过元素编号获取元素名称
  54. string IncAlibSqlString = CreateIncAlibQuerySql(sqldt.Rows[0]["IncAId"].ToString());
  55. sqldt = sql.ExecuteQuery(IncAlibSqlString);
  56. if (sqldt != null)
  57. {
  58. if (sqldt.Rows.Count > 0)
  59. {
  60. IncAName = sqldt.Rows[0]["Name"].ToString();
  61. }
  62. }
  63. }
  64. }
  65. return IncAName;
  66. }
  67. catch (Exception)
  68. {
  69. return "";
  70. }
  71. }
  72. #endregion
  73. #region 创建查询语句
  74. /// <summary>
  75. /// 创建查询分析Xray sql语句
  76. /// </summary>
  77. /// <param name="FieldID"></param>
  78. /// <param name="XrayID"></param>
  79. /// <returns></returns>
  80. protected string CreateInclusionQuerySql(int FieldID, int XrayID)
  81. {
  82. StringBuilder sqlString = new StringBuilder();
  83. sqlString.Append("select IncAId");
  84. sqlString.Append(" from " + m_MeasureResultInclusionTableName + "");
  85. sqlString.Append(" where [FieldId] = '" + FieldID + "'");
  86. sqlString.Append(" and [XrayId] = '" + XrayID + "'");
  87. return sqlString.ToString();
  88. }
  89. /// <summary>
  90. /// 创建查询元素信息 sql语句
  91. /// </summary>
  92. /// <param name="IncAId"></param>
  93. /// <returns>IncAName</returns>
  94. protected string CreateIncAlibQuerySql(string IncAId)
  95. {
  96. StringBuilder sqlString = new StringBuilder();
  97. sqlString.Append("select Name");
  98. sqlString.Append(" from " + m_STDTableName + "");
  99. sqlString.Append(" where IncAId = '" + IncAId + "'");
  100. return sqlString.ToString();
  101. }
  102. #endregion
  103. #region 判断当前路径下是否存在FIELD_FILES文件夹,返回数据文件路径
  104. public DataTable ReturnDataInFilePath(int FieldID, int XrayID, string m_FilePath)
  105. {
  106. DataTable dt = new DataTable();
  107. DirectoryInfo directory = new DirectoryInfo(m_FilePath);
  108. if (directory.Exists)
  109. {
  110. FileInfo fileInfo = new FileInfo(m_FilePath + "\\" + m_MeasureResultInclusionDBName);
  111. if (fileInfo.Exists)
  112. {
  113. //查询信息
  114. sql = new SqLiteHelper("data source='" + m_FilePath + "\\" + m_MeasureResultInclusionDBName + "'");
  115. string InclusionSqlString = CreateInclusionQuerySql(FieldID, XrayID);
  116. DataTable sqldt = sql.ExecuteQuery(InclusionSqlString);
  117. if (sqldt != null)
  118. {
  119. if (sqldt.Rows.Count>0)
  120. {
  121. dt = sqldt;
  122. return dt;
  123. }
  124. else
  125. {
  126. dt = ReturnDataInFilePath(FieldID, XrayID, m_FilePath + "\\FIELD_FILES");
  127. }
  128. }
  129. else
  130. {
  131. dt = ReturnDataInFilePath(FieldID, XrayID, m_FilePath + "\\FIELD_FILES");
  132. }
  133. }
  134. }
  135. return dt;
  136. }
  137. #endregion
  138. /// <summary>
  139. /// 创建查询元素信息 sql语句
  140. /// </summary>
  141. /// <param name="IncAId"></param>
  142. /// <returns>IncAName</returns>
  143. public string GetNameByIncAId(string IncAId)
  144. {
  145. string name = string.Empty;
  146. StringBuilder sqlString = new StringBuilder();
  147. sqlString.Append("select Name");
  148. sqlString.Append(" from " + m_STDTableName + "");
  149. sqlString.Append(" where IncAId = '" + IncAId + "'");
  150. DataTable dt = new DataTable();
  151. sql = new SqLiteHelper("data source='" + m_STDDBPath + "\\" + m_STDDBName + "'");
  152. dt = sql.ExecuteQuery(sqlString.ToString());
  153. if (dt.Rows.Count > 0)
  154. {
  155. name = dt.Rows[0]["Name"].ToString();
  156. }
  157. return name;
  158. }
  159. }
  160. }