Guest User

Untitled

a guest
Jun 17th, 2017
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. using System;
  2. using Npgsql;
  3. using System.Collections.Generic;
  4.  
  5. namespace DaraAnalisator
  6. {
  7. public class DataModule
  8. {
  9. private static volatile DataModule instance;
  10. private static object syncRoot = new Object();
  11.  
  12. private NpgsqlConnection conn;
  13.  
  14. private static string Server = "127.0.0.1";
  15. private static string Port = "5432";
  16. private static string User = "ts_engineer";
  17. private static string Password = "engineer_pass";
  18. private static string DataBase = "turboshaft";
  19. public static DataModule Instance
  20. {
  21. get
  22. {
  23. if (instance == null)
  24. {
  25. lock (syncRoot)
  26. {
  27. if (instance == null)
  28. instance = new DataModule();
  29. }
  30. }
  31.  
  32. return instance;
  33. }
  34. }
  35. public DataModule()
  36. {
  37. try
  38. {
  39. string connectionString = String.Format(
  40. "Server={0};Port={1};User Id={2};Password={3};Database={4};",
  41. Server, Port, User, Password, DataBase);
  42. conn = new NpgsqlConnection(connectionString);
  43. conn.Open();
  44. }
  45. catch (Exception ex)
  46. {
  47. LogModule.Instance.Log(ex.StackTrace);
  48. }
  49. }
  50.  
  51. public void CloseDataBaseConnection()
  52. {
  53. try
  54. {
  55. if ( conn != null)
  56. {
  57. conn.Close();
  58. }
  59.  
  60. }
  61. catch (Exception ex)
  62. {
  63. LogModule.Instance.Log(ex.StackTrace);
  64. }
  65. }
  66.  
  67. public List<double> ExecuteDoubleSelect(string query)
  68. {
  69. var resultList = new List<double>();
  70. try
  71. {
  72. var command = new NpgsqlCommand(query, conn);
  73. var reader = command.ExecuteReader();
  74. while (reader.Read())
  75. {
  76. resultList.Add((double)reader[0]);
  77. }
  78. reader.Close();
  79. return resultList;
  80. }
  81. catch(Exception ex)
  82. {
  83. LogModule.Instance.Log(ex.StackTrace);
  84. return resultList;
  85. }
  86. }
  87.  
  88. public List<SensorData> ExecuteSelectForRealTime(string query)
  89. {
  90. var resultList = new List<SensorData>();
  91. try
  92. {
  93. var command = new NpgsqlCommand(query, conn);
  94. var reader = command.ExecuteReader();
  95. while (reader.Read())
  96. {
  97. resultList.Add(new SensorData(reader[0].ToString(), (double)reader[1]));
  98. }
  99. reader.Close();
  100. return resultList;
  101. }
  102. catch (Exception ex)
  103. {
  104. LogModule.Instance.Log(ex.StackTrace);
  105. return resultList;
  106. }
  107. }
  108.  
  109. public NpgsqlDataReader ExecuteSelect(string query)
  110. {
  111. try
  112. {
  113. var command = new NpgsqlCommand(query, conn);
  114. var reader = command.ExecuteReader();
  115. return reader;
  116. }
  117. catch (Exception ex)
  118. {
  119. LogModule.Instance.Log(ex.StackTrace);
  120. return null;
  121. }
  122. }
  123. }
  124. }
Add Comment
Please, Sign In to add comment