BatchMessSqliteDataClass.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using CCDCount.MODEL.AuditTrailModel;
  2. using CCDCount.MODEL.SqlDataModel;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data.SQLite;
  6. using System.IO;
  7. namespace CCDCount.DLL.SqlDataClass
  8. {
  9. public class BatchMessSqliteDataClass
  10. {
  11. private string DatabaseFile;
  12. private readonly string _connectionString;
  13. private object locker = new object();
  14. public BatchMessSqliteDataClass(string dbPath)
  15. {
  16. DatabaseFile = dbPath;
  17. _connectionString = $"Data Source={DatabaseFile};Version=3;" +
  18. "Journal Mode=WAL;" + // 使用WAL模式提高并发性
  19. "Synchronous=Normal;" + // 平衡性能和安全性
  20. "Cache Size=10000;" + // 增加缓存大小
  21. "Pooling=true;" + // 启用连接池
  22. "Max Pool Size=100;"; // 设置最大连接池大小
  23. string directoryPath = Path.GetDirectoryName(DatabaseFile);
  24. if (!Directory.Exists(directoryPath))
  25. {
  26. // 创建文件夹
  27. Directory.CreateDirectory(directoryPath);
  28. }
  29. InitializeDatabase();
  30. }
  31. // 初始化数据库和表
  32. private void InitializeDatabase()
  33. {
  34. if (!File.Exists(DatabaseFile))
  35. {
  36. SQLiteConnection.CreateFile(DatabaseFile);
  37. }
  38. using (var conn = new SQLiteConnection(_connectionString))
  39. {
  40. conn.Open();
  41. string createTableSql = @"
  42. CREATE TABLE IF NOT EXISTS BatchMessage (
  43. Id INTEGER PRIMARY KEY AUTOINCREMENT,
  44. BatchNunber TEXT NOT NULL,
  45. MaterialCylinderVibrationTableSpeed REAL NOT NULL,
  46. FilterVibrationTableSpeed REAL NOT NULL,
  47. CountVibrationTableSpeed REAL NOT NULL,
  48. BottingCount INTEGER NOT NULL,
  49. BottingSpeed INTEGER NOT NULL,
  50. RecordTime TEXT NOT NULL
  51. )";
  52. new SQLiteCommand(createTableSql, conn).ExecuteNonQuery();
  53. }
  54. }
  55. // 插入数据
  56. public void InsertBatchMessage(BatchRecordModel Message)
  57. {
  58. lock (locker)
  59. {
  60. using (var conn = new SQLiteConnection($"Data Source={DatabaseFile};Version=3;"))
  61. {
  62. conn.Open();
  63. string sql = "INSERT INTO BatchMessage" +
  64. " (BatchNunber, MaterialCylinderVibrationTableSpeed, FilterVibrationTableSpeed,CountVibrationTableSpeed,BottingCount,BottingSpeed,RecordTime)" +
  65. " VALUES (@BatchNunber, @MaterialCylinderVibrationTableSpeed, @FilterVibrationTableSpeed,@CountVibrationTableSpeed, @BottingCount, @BottingSpeed,@RecordTime)";
  66. var cmd = new SQLiteCommand(sql, conn);
  67. cmd.Parameters.AddWithValue("@BatchNunber", Message.BatchNunber);
  68. cmd.Parameters.AddWithValue("@MaterialCylinderVibrationTableSpeed", Message.MaterialCylinderVibrationTableSpeed);
  69. cmd.Parameters.AddWithValue("@FilterVibrationTableSpeed", Message.FilterVibrationTableSpeed);
  70. cmd.Parameters.AddWithValue("@CountVibrationTableSpeed", Message.CountVibrationTableSpeed);
  71. cmd.Parameters.AddWithValue("@BottingCount", Message.BottingCount);
  72. cmd.Parameters.AddWithValue("@BottingSpeed", Message.BottingSpeed);
  73. cmd.Parameters.AddWithValue("@RecordTime", Message.RecordTime.ToString("yyyy-MM-dd HH:mm:ss.fff"));
  74. cmd.ExecuteNonQuery();
  75. }
  76. }
  77. }
  78. // 查询所有数据
  79. public List<BatchRecordModel> GetAllBatchMessage()
  80. {
  81. var BatchS = new List<BatchRecordModel>();
  82. using (var conn = new SQLiteConnection($"Data Source={DatabaseFile};Version=3;"))
  83. {
  84. conn.Open();
  85. string sql = "SELECT * FROM BatchMessage";
  86. var cmd = new SQLiteCommand(sql, conn);
  87. using (SQLiteDataReader reader = cmd.ExecuteReader())
  88. {
  89. while (reader.Read())
  90. {
  91. BatchS.Add(new BatchRecordModel
  92. {
  93. BatchNunber = reader["BatchNunber"].ToString(),
  94. MaterialCylinderVibrationTableSpeed = Convert.ToUInt16(reader["MaterialCylinderVibrationTableSpeed"]),
  95. FilterVibrationTableSpeed = Convert.ToUInt16(reader["FilterVibrationTableSpeed"]),
  96. CountVibrationTableSpeed = Convert.ToUInt16(reader["CountVibrationTableSpeed"]),
  97. BottingCount = Convert.ToInt32(reader["BottingCount"]),
  98. BottingSpeed = Convert.ToUInt16(reader["BottingSpeed"]),
  99. RecordTime = DateTime.Parse(reader["RecordTime"].ToString())
  100. });
  101. }
  102. }
  103. }
  104. return BatchS;
  105. }
  106. }
  107. }