CNAS取数仪器端升级
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 line
5.9KB

  1. using CnasLocalIDAL;
  2. using CnasSynchronousCommon;
  3. using CnasSynchrousModel;
  4. using SyncLocalDAL;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Data.SQLite;
  8. using System.Linq;
  9. using System.Text;
  10. namespace CNAS_SerialPort
  11. {
  12. public class SerialPortConfigInfoDAL : LocalDBbase, ISerialPortConfigInfo
  13. {
  14. public SerialPortConfigInfo GetSerialPortConfigInfo()
  15. {
  16. SerialPortConfigInfo serialPortConfig = null;
  17. try
  18. {
  19. //构建数据库连接
  20. SQLiteHelper.SetConnectionString(ConnectString);
  21. string strSql = $"Select * From SercialPortConfig Limit 1";
  22. SQLiteDataReader reader = SQLiteHelper.ExecuteReader(strSql, new SQLiteParameter[] { });
  23. while (reader.Read())
  24. {
  25. serialPortConfig = new SerialPortConfigInfo();
  26. serialPortConfig.PortName = reader["PortName"].ToString();
  27. serialPortConfig.BaudRate = Convert.ToInt32(reader["BaudRate"]);
  28. serialPortConfig.DataBits = Convert.ToInt32(reader["DataBits"]);
  29. serialPortConfig.Handshake = reader["Handshake"].ToString();
  30. serialPortConfig.IfAutoWrap = Convert.ToBoolean(reader["IfAutoWrap"]);
  31. serialPortConfig.IfMuffleFurnace = Convert.ToBoolean(reader["IfMuffleFurnace"]);
  32. serialPortConfig.IfPeelingWeighing = Convert.ToBoolean(reader["IfPeelingWeighing"]);
  33. serialPortConfig.Parity = reader["Parity"].ToString();
  34. serialPortConfig.StopBits = reader["StopBits"].ToString();
  35. }
  36. reader.Close();
  37. }
  38. catch (Exception ex)
  39. {
  40. AppLog.Error("获取本地存储串口配置失败," + ex.Message.ToString());
  41. throw ex;
  42. }
  43. return serialPortConfig;
  44. }
  45. public int InsertSerialPortConfigInfo(SerialPortConfigInfo serialPortConfig)
  46. {
  47. int iReturn = 0;
  48. string strSql = @"INSERT INTO SercialPortConfig (PortName
  49. ,BaudRate
  50. ,DataBits
  51. ,Handshake
  52. ,IfAutoWrap
  53. ,IfMuffleFurnace
  54. ,IfPeelingWeighing
  55. ,Parity
  56. ,StopBits)
  57. VALUES(@PortName
  58. ,@BaudRate
  59. ,@DataBits
  60. ,@Handshake
  61. ,@IfAutoWrap
  62. ,@IfMuffleFurnace
  63. ,@IfPeelingWeighing
  64. ,@Parity
  65. ,@StopBits)";
  66. try
  67. {
  68. //构建数据库连接
  69. SQLiteHelper.SetConnectionString(ConnectString);
  70. SQLiteParameter[] parameters = new SQLiteParameter[serialPortConfig.GetType().GetProperties().Count()];
  71. for (int i = 0; i < serialPortConfig.GetType().GetProperties().Count(); i++)
  72. {
  73. parameters[i] = new SQLiteParameter(serialPortConfig.GetType().GetProperties()[i].Name, serialPortConfig.GetType().GetProperties()[i].GetValue(serialPortConfig, null));
  74. }
  75. //执行SQL语句
  76. iReturn += SQLiteHelper.ExecuteNonQuery(strSql, parameters);
  77. }
  78. catch (Exception ex)
  79. {
  80. AppLog.Error("插入本地存储串口配置失败," + ex.Message.ToString());
  81. throw ex;
  82. }
  83. return iReturn;
  84. }
  85. public int UpdateSerialPortConfigInfo(SerialPortConfigInfo serialPortConfig)
  86. {
  87. int iReturn = 0;
  88. string strSql = @"UPDATE SercialPortConfig SET PortName=@PortName
  89. ,BaudRate=@BaudRate
  90. ,DataBits=@DataBits
  91. ,Handshake=@Handshake
  92. ,IfAutoWrap=@IfAutoWrap
  93. ,IfMuffleFurnace=@IfMuffleFurnace
  94. ,IfPeelingWeighing=@IfPeelingWeighing
  95. ,Parity=@Parity
  96. ,StopBits=@StopBits";
  97. try
  98. {
  99. //构建数据库连接
  100. SQLiteHelper.SetConnectionString(ConnectString);
  101. SQLiteParameter[] parameters = new SQLiteParameter[serialPortConfig.GetType().GetProperties().Count()];
  102. for (int i = 0; i < serialPortConfig.GetType().GetProperties().Count(); i++)
  103. {
  104. parameters[i] = new SQLiteParameter(serialPortConfig.GetType().GetProperties()[i].Name, serialPortConfig.GetType().GetProperties()[i].GetValue(serialPortConfig, null));
  105. }
  106. //执行SQL语句
  107. iReturn += SQLiteHelper.ExecuteNonQuery(strSql, parameters);
  108. }
  109. catch (Exception ex)
  110. {
  111. AppLog.Error("更改本地存储串口配置失败," + ex.Message.ToString());
  112. throw ex;
  113. }
  114. return iReturn;
  115. }
  116. }
  117. }