Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Npgsql;
- using System.Collections.Generic;
- namespace DaraAnalisator
- {
- public class DataModule
- {
- private static volatile DataModule instance;
- private static object syncRoot = new Object();
- private NpgsqlConnection conn;
- private static string Server = "127.0.0.1";
- private static string Port = "5432";
- private static string User = "ts_engineer";
- private static string Password = "engineer_pass";
- private static string DataBase = "turboshaft";
- public static DataModule Instance
- {
- get
- {
- if (instance == null)
- {
- lock (syncRoot)
- {
- if (instance == null)
- instance = new DataModule();
- }
- }
- return instance;
- }
- }
- public DataModule()
- {
- try
- {
- string connectionString = String.Format(
- "Server={0};Port={1};User Id={2};Password={3};Database={4};",
- Server, Port, User, Password, DataBase);
- conn = new NpgsqlConnection(connectionString);
- conn.Open();
- }
- catch (Exception ex)
- {
- LogModule.Instance.Log(ex.StackTrace);
- }
- }
- public void CloseDataBaseConnection()
- {
- try
- {
- if ( conn != null)
- {
- conn.Close();
- }
- }
- catch (Exception ex)
- {
- LogModule.Instance.Log(ex.StackTrace);
- }
- }
- public List<double> ExecuteDoubleSelect(string query)
- {
- var resultList = new List<double>();
- try
- {
- var command = new NpgsqlCommand(query, conn);
- var reader = command.ExecuteReader();
- while (reader.Read())
- {
- resultList.Add((double)reader[0]);
- }
- reader.Close();
- return resultList;
- }
- catch(Exception ex)
- {
- LogModule.Instance.Log(ex.StackTrace);
- return resultList;
- }
- }
- public List<SensorData> ExecuteSelectForRealTime(string query)
- {
- var resultList = new List<SensorData>();
- try
- {
- var command = new NpgsqlCommand(query, conn);
- var reader = command.ExecuteReader();
- while (reader.Read())
- {
- resultList.Add(new SensorData(reader[0].ToString(), (double)reader[1]));
- }
- reader.Close();
- return resultList;
- }
- catch (Exception ex)
- {
- LogModule.Instance.Log(ex.StackTrace);
- return resultList;
- }
- }
- public NpgsqlDataReader ExecuteSelect(string query)
- {
- try
- {
- var command = new NpgsqlCommand(query, conn);
- var reader = command.ExecuteReader();
- return reader;
- }
- catch (Exception ex)
- {
- LogModule.Instance.Log(ex.StackTrace);
- return null;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment