Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.Data;
- using System.Data.SqlClient;
- using System.Linq;
- namespace EyeRisk.Tools
- {
- public class DbHandler
- {
- private SqlConnection _conn;
- private static DbHandler instance = null;
- private static readonly object padlock = new object();
- private DbHandler()
- {
- _conn = new SqlConnection(Startup.connectionString);
- }
- public static DbHandler Instance
- {
- get
- {
- lock (padlock)
- {
- if (instance == null)
- {
- instance = new DbHandler();
- }
- return instance;
- }
- }
- }
- public void Close()
- {
- _conn.Close();
- }
- public DbHandler OpenConnection()
- {
- _conn.Open();
- return this;
- }
- public SqlCommand setCommand(string ps)
- {
- SqlCommand cmd = new SqlCommand(ps, _conn);
- cmd.CommandType = CommandType.StoredProcedure;
- return cmd;
- }
- public static DataTable getDataTable<T>(T model) where T : new()
- {
- DataTable dt = new DataTable();
- dt.Columns.Add("ColumnName");
- dt.Columns.Add("ColumnValue");
- //System.Reflection.MemberInfo info = typeof(T);
- foreach (var attr in typeof(T).GetProperties())
- {
- dt.Rows.Add(attr.Name, typeof(T).GetProperty(attr.Name).GetValue(model));
- }
- return dt;
- }
- public static SqlParameter[] getParamsFromModel<T>(T model) where T : new()
- {
- List<SqlParameter> parameters = new List<SqlParameter>();
- for (int i = 0; i < typeof(T).GetProperties().Count(); i++)
- {
- if (!(typeof(T).GetProperties()[i].SetMethod is null) && typeof(T).GetProperties()[i].SetMethod.IsPublic)
- {
- parameters.Add(new SqlParameter(
- typeof(T).GetProperties()[i].Name, typeof(T).GetProperty(typeof(T).GetProperties()[i].Name).GetValue(model)));
- }
- }
- return parameters.ToArray();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement