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