using Npgsql;
|
using System;
|
using System.Collections.Generic;
|
using System.Configuration;
|
using System.Data.Common;
|
using System.Linq;
|
using System.Reflection;
|
using System.Text;
|
using System.Threading.Tasks;
|
using System.Windows;
|
|
namespace DataLoader.Model
|
{
|
public class Tool
|
{
|
public static MainWindow Owner = null;
|
|
public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;
|
|
public static int UserId = 0;
|
|
public static string Uname = "";
|
|
public static string Token = "";
|
|
public static string FormatBytes(long bytes)
|
{
|
string[] Suffix = { "Byte", "KB", "MB", "GB", "TB" };
|
|
int i = 0;
|
double dblSByte = bytes;
|
if (bytes > 1024)
|
for (i = 0; (bytes / 1024) > 0; i++, bytes /= 1024)
|
dblSByte = bytes / 1024.0;
|
|
return String.Format("{0:0.##}{1}", dblSByte, Suffix[i]);
|
}
|
|
public static string GetSetting(string key)
|
{
|
if (!ConfigurationManager.AppSettings.AllKeys.Contains(key))
|
{
|
return null;
|
}
|
|
return ConfigurationManager.AppSettings[key];
|
}
|
|
public static List<DbParameter> GetParams<T>(string sql, T t)
|
{
|
List<DbParameter> list = new List<DbParameter>();
|
Type tType = typeof(T);
|
BindingFlags flags = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance;
|
|
int start = sql.IndexOf("@");
|
while (start != -1)
|
{
|
int end = sql.IndexOf(",", start);
|
if (end == -1) end = sql.IndexOf(")", start);
|
if (end == -1) end = sql.IndexOf(" ", start);
|
if (end == -1) end = sql.Length;
|
|
string name = sql.Substring(start + 1, end - start - 1);
|
PropertyInfo pi = tType.GetProperty(name, flags);
|
if (pi != null)
|
{
|
object value = pi.GetValue(t, null);
|
DbParameter dp = new NpgsqlParameter("@" + name, value);
|
list.Add(dp);
|
}
|
|
start = sql.IndexOf("@", end);
|
}
|
|
return list;
|
}
|
}
|
}
|