Advertisement
Guest User

Untitled

a guest
May 27th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Linq;
  5.  
  6. namespace EyeRisk.Tools
  7. {
  8.     public class DbHandler
  9.     {
  10.         private SqlConnection _conn;
  11.  
  12.         private static DbHandler instance = null;
  13.         private static readonly object padlock = new object();
  14.  
  15.         private DbHandler()
  16.         {
  17.             _conn = new SqlConnection(Startup.connectionString);
  18.         }
  19.  
  20.         public static DbHandler Instance
  21.         {
  22.             get
  23.             {
  24.                 lock (padlock)
  25.                 {
  26.                     if (instance == null)
  27.                     {
  28.                         instance = new DbHandler();
  29.                     }
  30.                     return instance;
  31.                 }
  32.             }
  33.         }
  34.  
  35.  
  36.         public void Close()
  37.         {
  38.             _conn.Close();
  39.         }
  40.  
  41.  
  42.         public DbHandler OpenConnection()
  43.         {
  44.             _conn.Open();
  45.             return this;
  46.         }
  47.  
  48.         public SqlCommand setCommand(string ps)
  49.         {
  50.             SqlCommand cmd = new SqlCommand(ps, _conn);
  51.             cmd.CommandType = CommandType.StoredProcedure;
  52.             return cmd;
  53.         }
  54.  
  55.         public static DataTable getDataTable<T>(T model) where T : new()
  56.         {
  57.             DataTable dt = new DataTable();
  58.             dt.Columns.Add("ColumnName");
  59.             dt.Columns.Add("ColumnValue");
  60.             //System.Reflection.MemberInfo info = typeof(T);
  61.             foreach (var attr in typeof(T).GetProperties())
  62.             {
  63.  
  64.                 dt.Rows.Add(attr.Name, typeof(T).GetProperty(attr.Name).GetValue(model));
  65.             }
  66.  
  67.             return dt;
  68.         }
  69.  
  70.         public static SqlParameter[] getParamsFromModel<T>(T model) where T : new()
  71.         {
  72.             List<SqlParameter> parameters = new List<SqlParameter>();
  73.             for (int i = 0; i < typeof(T).GetProperties().Count(); i++)
  74.             {
  75.                 if (!(typeof(T).GetProperties()[i].SetMethod is null) && typeof(T).GetProperties()[i].SetMethod.IsPublic)
  76.                 {
  77.                     parameters.Add(new SqlParameter(
  78.                     typeof(T).GetProperties()[i].Name, typeof(T).GetProperty(typeof(T).GetProperties()[i].Name).GetValue(model)));
  79.                 }
  80.             }
  81.  
  82.             return parameters.ToArray();
  83.         }
  84.  
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement