CGC_Codes

DataHelper

May 21st, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.39 KB | None | 0 0
  1. using Microsoft.Extensions.Configuration;
  2. using Microsoft.Extensions.Logging;
  3. using Microsoft.Extensions.Logging.Debug;
  4. using MySql.Data.MySqlClient;
  5. using System;
  6. using System.Data;
  7. using System.Data.Common;
  8. using System.IO;
  9. using System.Net;
  10. using System.Text;
  11.  
  12. namespace DatabaseLib
  13. {
  14.     public static class DataHelper
  15.     {
  16.         static string m_ConnStr = @"Data Source=.\SQLEXPRESS;Initial Catalog=SharpSCADA;Integrated Security=True";
  17.         static string m_Path = @"D:\HDA";
  18.         static string m_host = Environment.MachineName;
  19.         static string m_type = "MSSQL";
  20.        
  21.         const string CFGPATH = @"C:\DataConfig\host.cfg";
  22.         const string INIPATH = @"C:\DataConfig\host.ini";
  23.         const string DATALOGSOURCE = "Data Operations";
  24.         const string DATALOGNAME = "Data Log";
  25.         const int STRINGMAX = 255;
  26.  
  27.         static ILogger Log;
  28.         #region GetInstance
  29.         private static IDataFactory _ins;
  30.  
  31.         public static IDataFactory Instance
  32.         {
  33.             get
  34.             {
  35.                 return _ins;
  36.             }
  37.         }
  38.  
  39.         public static string HostName
  40.         {
  41.             get { return m_host; }
  42.         }
  43.  
  44.         public static string ConnectString
  45.         {
  46.             get { return m_ConnStr; }
  47.         }
  48.  
  49.         public static string HdaPath
  50.         {
  51.             get { return m_Path; }
  52.         }
  53.        
  54.  
  55.         static DataHelper()
  56.         {
  57.             var loggerFactory = new LoggerFactory();
  58.             Func<string, LogLevel, bool> filter = (category, level) => true;
  59.             loggerFactory.AddProvider(new DebugLoggerProvider(filter));
  60.             Log = loggerFactory.CreateLogger(DATALOGSOURCE);
  61.             try
  62.             {
  63.                 if (File.Exists(INIPATH))
  64.                 {
  65.                     var builder = new ConfigurationBuilder();
  66.                     var ibuild = builder.AddIniFile(INIPATH);
  67.                     var root = ibuild.Build();
  68.                     var host = root.GetSection("HOST");
  69.                     m_host = host.GetSection("SERVER").Value;
  70.                     var db = root.GetSection("DATABASE");
  71.                     m_ConnStr = db.GetSection("CONNSTRING").Value;
  72.                     m_Path = db.GetSection("ARCHIVE").Value;
  73.                     m_type = db.GetSection("TYPE").Value;
  74.                 }
  75.                 else if (File.Exists(CFGPATH))
  76.                 {
  77.                     using (StreamReader objReader = new StreamReader(CFGPATH))
  78.                     {
  79.                         m_host = objReader.ReadLine();
  80.                         m_ConnStr = objReader.ReadLine();
  81.                         m_Path = objReader.ReadLine();
  82.                     }
  83.                 }
  84.                 IPAddress addr;
  85.                 if (string.IsNullOrEmpty(m_host) || !IPAddress.TryParse(m_host, out addr))
  86.                 {
  87.                     m_host = Environment.MachineName;
  88.                 }
  89.                 _ins = new MysqlFactory();
  90.             }
  91.             catch (Exception e)
  92.             {
  93.                 AddErrorLog(e);
  94.             }
  95.         }
  96.  
  97.         public static DbParameter CreateParam(string paramName, SqlDbType dbType, object objValue, int size = 0, ParameterDirection   direction = ParameterDirection.Input)
  98.         {
  99.             return _ins.CreateParam(paramName, dbType, objValue, size, direction);
  100.         }
  101.  
  102.         public static string DataTableToCsv(DataTable table)
  103.         {
  104.  
  105.             StringBuilder sb = new StringBuilder();
  106.             DataColumn colum;
  107.             foreach (DataRow row in table.Rows)
  108.             {
  109.                 for (int i = 0; i < table.Columns.Count; i++)
  110.                 {
  111.                     colum = table.Columns[i];
  112.                     if (i != 0) sb.Append(",");
  113.                     var txt = row[colum] == null ? "" : row[colum].ToString();
  114.                     if (colum.DataType == typeof(string) && txt.Contains(","))
  115.                     {
  116.                         sb.Append("\"" + txt.Replace("\"", "\"\"") + "\"");
  117.                     }
  118.                     else sb.Append(txt);
  119.                 }
  120.                 sb.AppendLine();
  121.             }
  122.             return sb.ToString();
  123.         }
  124.  
  125.         public static string ReaderToCsv(IDataReader reader)
  126.         {
  127.             StringBuilder sb = new StringBuilder();
  128.             var colcount = reader.FieldCount;
  129.             while (reader.Read())
  130.             {
  131.                 for (int i = 0; i < colcount; i++)
  132.                 {
  133.                     if (i != 0) sb.Append(",");
  134.                     var txt = reader[i] == null ? "" : reader[i].ToString();
  135.                     if (txt.Contains(","))
  136.                     {
  137.                         sb.Append("\"" + txt.Replace("\"", "\"\"") + "\"");
  138.                     }
  139.                     else sb.Append(txt);
  140.                 }
  141.                 sb.AppendLine();
  142.             }
  143.             return sb.ToString();
  144.         }
  145.  
  146.         public static void AddErrorLog(Exception e)
  147.         {
  148.             string err = "";
  149.             Exception exp = e;
  150.             while (exp != null)
  151.             {
  152.                 err += string.Format("\n {0}", exp.Message);
  153.                 exp = exp.InnerException;
  154.             }
  155.             err += string.Format("\n {0}", e.StackTrace);
  156.             Log.LogError(err);
  157.         }
  158.  
  159.         public static string GetNullableString(this DbDataReader reader, int index)
  160.         {
  161.             return reader.GetString(index);
  162.         }
  163.  
  164.         public static DateTime? GetNullableTime(this DbDataReader reader, int index)
  165.         {
  166.             return reader.GetDateTime(index);
  167.         }
  168.  
  169.         public static int GetTimeTick(this DbDataReader reader, int index)
  170.         {
  171.             var datetime = reader.GetDateTime(index);
  172.             var value = datetime.Subtract(new DateTime(1900, 1, 1));
  173.             long num2 = value.Ticks - value.Days * 864000000000;
  174.             if (num2 < 0)
  175.                 num2 += 864000000000;
  176.             int num3 = (int)(num2 / 10000.0 * 0.3 + 0.5);
  177.             if (num3 > 300 * 60 * 60 * 24 - 1)
  178.                 num3 = 0;
  179.             return num3;
  180.         }
  181.  
  182.         public static object GetSqlValue(this DbDataReader reader, int index)
  183.         {
  184.             var mq = reader as MySqlDataReader;
  185.             if (mq != null)
  186.             {
  187.                 return mq.GetValue(index);
  188.             }
  189.             return "";
  190.         }
  191.     }
  192.  
  193. }
Advertisement
Add Comment
Please, Sign In to add comment