CSQLiteDB.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using OTSModelSharp.DTLBase;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace OTSModelSharp.DTLBase
  9. {
  10. public class CSQLiteDB :IDBBase
  11. {
  12. //表结构
  13. protected IDBTableBase m_sqlitetable;
  14. //数据库连接地址
  15. protected IDBStoreBase m_DBStore;
  16. /// <summary>
  17. /// 初始换
  18. /// </summary>
  19. /// <param name="conStr">数据库地址</param>
  20. /// <param name="_table">表结构</param>
  21. public CSQLiteDB(IDBStoreBase a_dbStore, IDBTableBase a_table)
  22. {
  23. //dbStore =(CSQLiteDBStore) a_dbStore;
  24. m_sqlitetable = a_table;
  25. m_DBStore = a_dbStore;
  26. }
  27. /// <summary>
  28. /// 建表
  29. /// </summary>
  30. /// <param name="a_bClean">如果a_bClean删除表再建表</param>
  31. /// <returns></returns>
  32. public bool Init(bool a_bClean = false)
  33. {
  34. if (IsDBExist())
  35. {
  36. if (a_bClean)
  37. {
  38. DeleteTable();
  39. }
  40. else
  41. {
  42. return true;
  43. }
  44. }
  45. return CreateTable();
  46. }
  47. /// <summary>
  48. ///建表
  49. /// </summary>
  50. /// <returns></returns>
  51. public bool CreateTable( bool a_bForce=false)
  52. {
  53. if (m_DBStore.IsTableExists(m_sqlitetable.GetTableName()))
  54. {
  55. if (a_bForce)
  56. {
  57. if (!DeleteTable())
  58. {
  59. return false;
  60. }
  61. }
  62. else
  63. {
  64. return true;
  65. }
  66. }
  67. // create table
  68. string sSQLCommand = m_sqlitetable.GetCreateTableCommandString();
  69. return m_DBStore.RunCommand(sSQLCommand);
  70. }
  71. /// <summary>
  72. ///删除表
  73. /// </summary>
  74. /// <returns></returns>
  75. public bool DeleteTable()
  76. {
  77. return m_DBStore.DeleteTable(m_sqlitetable.GetTableName());
  78. }
  79. /// <summary>
  80. ///移除表数据
  81. /// </summary>
  82. /// <returns></returns>
  83. public bool RemoveAllRows()
  84. {
  85. if (!m_DBStore. IsTableExists(m_sqlitetable. GetTableName()))
  86. {
  87. return true;
  88. }
  89. // create table
  90. string sSQLCommand = m_sqlitetable.GetRemoveAllRowsCommandString();
  91. return m_DBStore. RunCommand(sSQLCommand);
  92. }
  93. /// <summary>
  94. /// 判断表是否存在
  95. /// </summary>
  96. /// <returns></returns>
  97. public bool IsDBExist()
  98. {
  99. return m_DBStore.IsTableExists(m_sqlitetable.GetTableName());
  100. }
  101. /// <summary>
  102. /// 获取连接字符串
  103. /// </summary>
  104. /// <returns></returns>
  105. public IDBStoreBase GetDatastore()
  106. {
  107. return m_DBStore;
  108. }
  109. /// <summary>
  110. /// 获取表结构
  111. /// </summary>
  112. /// <returns></returns>
  113. /// <summary>
  114. /// 获取表所有信息
  115. /// </summary>
  116. /// <param name="a_sOrderColumnName">排序字段</param>
  117. /// <returns></returns>
  118. public IDBQueryBase GetTableQuery(string a_sOrderColumnName="")
  119. {
  120. return m_DBStore.QueryByTableName(m_sqlitetable.GetTableName(),a_sOrderColumnName);
  121. }
  122. public DataTable GetTableQueryForDataTable(string a_sOrderColumnName = "")
  123. {
  124. return m_DBStore.QueryByTableNameForDataTable(m_sqlitetable.GetTableName(), a_sOrderColumnName);
  125. }
  126. public IDBTableBase GetTableInfo()
  127. {
  128. return m_sqlitetable;
  129. }
  130. }
  131. }