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