Update 20231016

This commit is contained in:
2023-10-16 18:21:29 +02:00
parent e83b2e6f35
commit 376f51db74
442 changed files with 11311 additions and 7581 deletions

View File

@@ -1,127 +1,124 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;
namespace DPMService.Models
{
public class Crypto
{
//public static void Main()
//{
// try
// {
// string original = "Here is some data to encrypt!";
// // Create a new instance of the RijndaelManaged
// // class. This generates a new key and initialization
// // vector (IV).
// using (RijndaelManaged myRijndael = new RijndaelManaged())
// {
// myRijndael.GenerateKey();
// myRijndael.GenerateIV();
// // Encrypt the string to an array of bytes.
// byte[] encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);
// // Decrypt the bytes to a string.
// string roundtrip = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV);
// //Display the original data and the decrypted data.
// Console.WriteLine("Original: {0}", original);
// Console.WriteLine("Round Trip: {0}", roundtrip);
// }
// }
// catch (Exception e)
// {
// Console.WriteLine("Error: {0}", e.Message);
// }
//}
public byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
byte[] encrypted;
// Create an RijndaelManaged object
// with the specified key and IV.
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Key = Key;
rijAlg.IV = IV;
// Create an encryptor to perform the stream transform.
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}
public string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an RijndaelManaged object
// with the specified key and IV.
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Key = Key;
rijAlg.IV = IV;
// Create a decryptor to perform the stream transform.
ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
}
}
using System;
using System.IO;
using System.Security.Cryptography;
namespace DPMService.Models
{
public class Crypto
{
//public static void Main()
//{
// try
// {
// string original = "Here is some data to encrypt!";
// // Create a new instance of the RijndaelManaged
// // class. This generates a new key and initialization
// // vector (IV).
// using (RijndaelManaged myRijndael = new RijndaelManaged())
// {
// myRijndael.GenerateKey();
// myRijndael.GenerateIV();
// // Encrypt the string to an array of bytes.
// byte[] encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);
// // Decrypt the bytes to a string.
// string roundtrip = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV);
// //Display the original data and the decrypted data.
// Console.WriteLine("Original: {0}", original);
// Console.WriteLine("Round Trip: {0}", roundtrip);
// }
// }
// catch (Exception e)
// {
// Console.WriteLine("Error: {0}", e.Message);
// }
//}
public byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
byte[] encrypted;
// Create an RijndaelManaged object
// with the specified key and IV.
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Key = Key;
rijAlg.IV = IV;
// Create an encryptor to perform the stream transform.
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}
public string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an RijndaelManaged object
// with the specified key and IV.
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Key = Key;
rijAlg.IV = IV;
// Create a decryptor to perform the stream transform.
ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
}
}

View File

@@ -1,48 +1,44 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace BWPMService.Models
{
public class ForgotPasswordInputModel
{
[Required]
[EmailAddress]
public string Email { get; set; }
}
public class LoginInputModel
{
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
public class RegisterInputModel
{
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
}
using System.ComponentModel.DataAnnotations;
namespace BWPMService.Models
{
public class ForgotPasswordInputModel
{
[Required]
[EmailAddress]
public string Email { get; set; }
}
public class LoginInputModel
{
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
public class RegisterInputModel
{
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
}

View File

@@ -1,39 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DPMService.Models
{
public class PatCharge
{
public int ID { get; set; } = 0;
public int? PatientID { get; set; } = 0;
public string CharcheCode { get; set; } = "";
public DateTime? erstellt_am { get; set; } = DateTime.Now;
public DateTime? mutiert_am { get; set; } = DateTime.Now;
public string mutierer { get; set; } = "";
public bool aktiv { get; set; } = true;
}
public class ViewPatCharche
{
public int id { get; set; } = 0;
public int? patid { get; set; } = 0;
public string charge { get; set; } = "";
public string datum { get; set; } = "";
public bool aktiv { get; set; } = true;
}
}
using System;
namespace DPMService.Models
{
public class PatCharge
{
public int ID { get; set; } = 0;
public int? PatientID { get; set; } = 0;
public string CharcheCode { get; set; } = "";
public DateTime? erstellt_am { get; set; } = DateTime.Now;
public DateTime? mutiert_am { get; set; } = DateTime.Now;
public string mutierer { get; set; } = "";
public bool aktiv { get; set; } = true;
}
public class ViewPatCharche
{
public int id { get; set; } = 0;
public int? patid { get; set; } = 0;
public string charge { get; set; } = "";
public string datum { get; set; } = "";
public bool aktiv { get; set; } = true;
}
}

View File

@@ -1,20 +1,95 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DPMService.Models
{
public class Patient
{
public int ID { get; set; } = 0;
public string Pat { get; set; } = "";
public DateTime? TransferDateiTime { get; set; } = DateTime.Now;
public int? Transferdirection { get; set; } = 0;
}
}
using System;
namespace DPMService.Models
{
public class Patient
{
public int ID { get; set; } = 0;
public string Pat { get; set; } = "";
public DateTime? TransferDateiTime { get; set; } = DateTime.Now;
public int? Transferdirection { get; set; } = 0;
}
public class privat
{
public string ahvnr { get; set; } = "";
public bool? aktiv { get; set; } = true;
public bool? behandler { get; set; } = true;
public string bemerkung { get; set; } = "";
public string beruf { get; set; } = "";
public bool? betreibung { get; set; } = true;
public string briefanrede { get; set; } = "";
public string coadresse { get; set; } = "";
public bool? dhpat { get; set; } = true;
public int? dhrecall { get; set; } = 0;
public string dhrecallbemerkung { get; set; } = "";
public float? dhrecallfixmonat { get; set; } = 0;
public bool? dhrecalltelefon { get; set; } = true;
public int? dhrecalltyp { get; set; } = 0;
public string E_Mail { get; set; } = "";
public DateTime? erstellt_am { get; set; } = DateTime.Now;
public int? estyp { get; set; } = 0;
public string faxg { get; set; } = "";
public string faxp { get; set; } = "";
public string fsnr { get; set; } = "";
public bool? garant { get; set; } = true;
public DateTime? gebdat { get; set; } = DateTime.Now;
public int? geschlecht { get; set; } = 0;
public bool? gesvertreter { get; set; } = true;
public string GLN { get; set; } = "";
public DateTime? gueltigab { get; set; } = DateTime.Now;
public DateTime? gueltigbis { get; set; } = DateTime.Now;
public bool? hausarzt { get; set; } = true;
public string ivnr { get; set; } = "";
public string Kanton { get; set; } = "";
public string kknr { get; set; } = "";
public string korranrede { get; set; } = "";
public string korrcoadresse { get; set; } = "";
public string korrname { get; set; } = "";
public string korrort { get; set; } = "";
public string korrplz { get; set; } = "";
public string korrstrasse { get; set; } = "";
public string korrtitel { get; set; } = "";
public string korrvorname { get; set; } = "";
public bool? Mail_Kommunikation { get; set; } = true;
public int? mandant { get; set; } = 0;
public int? mutierer { get; set; } = 0;
public DateTime? mutiert_am { get; set; } = DateTime.Now;
public string name { get; set; } = "";
public string natel { get; set; } = "";
public bool? nichtannehmen { get; set; } = true;
public bool? nichtaufbieten { get; set; } = true;
public int? nranrede { get; set; } = 0;
public int? nrarbeitgeber { get; set; } = 0;
public int? nrbehandler { get; set; } = 0;
public int? nrdh { get; set; } = 0;
public int? nrfs { get; set; } = 0;
public int? nrgarant { get; set; } = 0;
public int? nrgesvertreter { get; set; } = 0;
public int? nrhausarzt { get; set; } = 0;
public int? nriv { get; set; } = 0;
public int? nrkk { get; set; } = 0;
public int nrprivat { get; set; } = 0; public int? nrrgtyp { get; set; } = 0;
public int? nrtitel { get; set; } = 0;
public int? nrvs { get; set; } = 0;
public int? nrzahnarzt { get; set; } = 0;
public string ort { get; set; } = "";
public bool? patient { get; set; } = true;
public string plz { get; set; } = "";
public float? rabatt { get; set; } = 0;
public string sprache { get; set; } = "";
public int? status { get; set; } = 0; public string strasse { get; set; } = "";
public int? taxpunkttyp { get; set; } = 0;
public string telg { get; set; } = "";
public string telp { get; set; } = "";
public string vorname { get; set; } = ""; public string vsnr { get; set; } = "";
public string web { get; set; } = "";
public bool? zahnarzt { get; set; } = true; public int? zazrecall { get; set; } = 0; public string zazrecallbemerkung { get; set; } = "";
public float? zazrecallfixmonat { get; set; } = 0;
public bool? zazrecalltelefon { get; set; } = true; public int? zazrecalltyp { get; set; } = 0; public string ZSR { get; set; } = "";
}
}

View File

@@ -1,23 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DPMService.Models
{
public class Service_View_Pat
{
public int ID
{
get;
set;
}
public string Pat { get; set; } = "";
public Service_View_Pat()
{
}
}
}
namespace DPMService.Models
{
public class Service_View_Pat
{
public int ID
{
get;
set;
}
public string Pat { get; set; } = "";
public Service_View_Pat()
{
}
}
}

View File

@@ -1,18 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BWPMModels
{
public class UserModel
{
public int Id { get; set; }
public string PassWord { get; set; }
public string FullName { get; set; }
public string Email { get; set; }
public DateTime CreatedDate { get; set; }
public string Avatar { get; set; }
}
}
using System;
namespace BWPMModels
{
public class UserModel
{
public int Id { get; set; }
public string PassWord { get; set; }
public string FullName { get; set; }
public string Email { get; set; }
public DateTime CreatedDate { get; set; }
public string Avatar { get; set; }
}
}

View File

@@ -1,279 +1,281 @@
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace DPMService.Models
{
public class dbhelper
{
//SqlConnection con;
string connectionstring;
public DataSet dsdaten = new DataSet();
private SqlDataAdapter dadaten;
public dbhelper()
{
var configuation = GetConfiguration();
connectionstring = configuation.GetSection("ConnectionStrings").GetSection("DBConnection").Value;
}
public static DataTable ObjectToDataTable(object o)
{
DataTable dt = new DataTable();
List<PropertyInfo> properties = o.GetType().GetProperties().ToList();
foreach (PropertyInfo prop in properties)
dt.Columns.Add(prop.Name, prop.PropertyType);
dt.TableName = o.GetType().Name;
return dt;
}
public IConfigurationRoot GetConfiguration()
{
var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
return builder.Build();
}
public DataTable Get_Tabledata(string Tablename, bool StoredProc = false, bool is_SQL_String = false)
{
SqlConnection sqlconnect = new SqlConnection();
DataSet ds = new DataSet();
ds.Tables.Clear();
sqlconnect.ConnectionString = this.connectionstring;
sqlconnect.Open();
SqlDataAdapter da = new SqlDataAdapter("", sqlconnect);
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.Connection = sqlconnect;
if (StoredProc == true)
{
sqlcmd.CommandType = CommandType.StoredProcedure;
if (Tablename.IndexOf("@@Mandantnr@@") > 0)
Tablename = Tablename.Replace("@@Mandantnr@@", "");
sqlcmd.CommandText = Tablename;
}
else
{
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = "Select * from " + Tablename;
}
if (is_SQL_String == true)
sqlcmd.CommandText = Tablename;
da.SelectCommand = sqlcmd;
da.Fill(dsdaten, "Daten");
sqlconnect.Close();
return dsdaten.Tables[0];
}
public void Get_Tabeldata_for_Update(string Tablename, bool StoredProc = false, bool is_SQL_String = false)
{
dsdaten.Clear();
dsdaten.Tables.Clear();
dadaten = new SqlDataAdapter(Tablename, this.connectionstring);
dadaten.Fill(dsdaten, Tablename);
}
public void Update_Tabeldata()
{
SqlCommandBuilder cb = new SqlCommandBuilder(dadaten);
dadaten.Update(dsdaten, dsdaten.Tables[0].TableName);
}
public string Get_TablePraefix(string apikey)
{
Get_Tabledata("select tablepraefix from patchargeapi where apikey='" + apikey + "'", false, true);
if (this.dsdaten.Tables[0].Rows.Count == 0)
{
return "";
}
else
{
return this.dsdaten.Tables[0].Rows[0][0].ToString();
}
}
public Dictionary<string, List<object>> DatatableToDictionary(DataTable dataTable)
{
var dict = new Dictionary<string, List<object>>();
foreach (DataColumn dataColumn in dataTable.Columns)
{
var columnValueList = new List<object>();
foreach (DataRow dataRow in dataTable.Rows)
{
columnValueList.Add(dataRow[dataColumn.ColumnName]);
}
dict.Add(dataColumn.ColumnName, columnValueList);
}
return dict;
}
#region "Converters"
public List<T> ConvertDataTable<T>(DataTable dt)
{
List<T> data = new List<T>();
foreach (DataRow row in dt.Rows)
{
T item = GetItem<T>(row);
data.Add(item);
}
return data;
}
private T GetItem<T>(DataRow dr)
{
Type temp = typeof(T);
T obj = Activator.CreateInstance<T>();
foreach (DataColumn column in dr.Table.Columns)
{
foreach (PropertyInfo pro in temp.GetProperties())
{
if (pro.Name == column.ColumnName)
pro.SetValue(obj, dr[column.ColumnName], null/* TODO Change to default(_) if this is not a reference type */);
else
continue;
}
}
return obj;
}
public IEnumerable<T> GetEntities<T>(DataTable dt)
{
if (dt == null)
{
return null;
}
List<T> returnValue = new List<T>();
List<string> typeProperties = new List<string>();
T typeInstance = Activator.CreateInstance<T>();
foreach (DataColumn column in dt.Columns)
{
var prop = typeInstance.GetType().GetProperty(column.ColumnName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
if (prop != null)
{
typeProperties.Add(column.ColumnName);
}
}
foreach (DataRow row in dt.Rows)
{
T entity = Activator.CreateInstance<T>();
foreach (var propertyName in typeProperties)
{
if (row[propertyName] != DBNull.Value)
{
string str = row[propertyName].GetType().FullName;
if (entity.GetType().GetProperty(propertyName).PropertyType == typeof(System.String))
{
object Val = row[propertyName].ToString();
entity.GetType().GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).SetValue(entity, Val, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public, null, null, null);
}
else if (entity.GetType().GetProperty(propertyName).PropertyType == typeof(System.Guid))
{
object Val = Guid.Parse(row[propertyName].ToString());
entity.GetType().GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).SetValue(entity, Val, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public, null, null, null);
}
else
{
entity.GetType().GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).SetValue(entity, row[propertyName], BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public, null, null, null);
}
}
else
{
entity.GetType().GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).SetValue(entity, null, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public, null, null, null);
}
}
returnValue.Add(entity);
}
return returnValue.AsEnumerable();
}
public string DataTableToJSONWithStringBuilder(DataTable table)
{
var JSONString = new StringBuilder();
if (table.Rows.Count > 0)
{
JSONString.Append("[");
for (int i = 0; i < table.Rows.Count; i++)
{
JSONString.Append("{");
for (int j = 0; j < table.Columns.Count; j++)
{
if (j < table.Columns.Count - 1)
{
JSONString.Append("\"" + table.Columns[j].ColumnName.ToString() + "\":" + "\"" + table.Rows[i][j].ToString() + "\",");
}
else if (j == table.Columns.Count - 1)
{
JSONString.Append("\"" + table.Columns[j].ColumnName.ToString() + "\":" + "\"" + table.Rows[i][j].ToString() + "\"");
}
}
if (i == table.Rows.Count - 1)
{
JSONString.Append("}");
}
else
{
JSONString.Append("},");
}
}
JSONString.Append("]");
}
return JSONString.ToString();
}
public string ConvertDataTableToString(DataTable table)
{
int iColumnCount = table.Columns.Count;
int iRowCount = table.Rows.Count;
int iTempRowCount = 0;
string strColumName = table.Columns[0].ColumnName;
string strOut = "{";
foreach (DataRow row in table.Rows)
{
strOut = strOut + "{";
foreach (DataColumn col in table.Columns)
{
string val = row.Field<string>(col.ColumnName);
strOut = strOut + col.ColumnName + ":" + val;
if (col.Ordinal != iColumnCount - 1)
{
strOut = strOut + ",";
}
}
strOut = strOut + "}";
iTempRowCount++;
if (iTempRowCount != iRowCount)
{
strOut = strOut + ",";
}
}
strOut = strOut + "}";
return strOut;
}
#endregion
}
}
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
namespace DPMService.Models
{
public class dbhelper
{
//SqlConnection con;
private string connectionstring;
public DataSet dsdaten = new DataSet();
private SqlDataAdapter dadaten;
public dbhelper()
{
var configuation = GetConfiguration();
connectionstring = configuation.GetSection("ConnectionStrings").GetSection("DBConnection").Value;
}
public static DataTable ObjectToDataTable(object o)
{
DataTable dt = new DataTable();
List<PropertyInfo> properties = o.GetType().GetProperties().ToList();
foreach (PropertyInfo prop in properties)
dt.Columns.Add(prop.Name, prop.PropertyType);
dt.TableName = o.GetType().Name;
return dt;
}
public IConfigurationRoot GetConfiguration()
{
var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
return builder.Build();
}
public DataTable Get_Tabledata(string Tablename, bool StoredProc = false, bool is_SQL_String = false)
{
SqlConnection sqlconnect = new SqlConnection();
DataSet ds = new DataSet();
ds.Tables.Clear();
sqlconnect.ConnectionString = this.connectionstring;
sqlconnect.Open();
SqlDataAdapter da = new SqlDataAdapter("", sqlconnect);
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.Connection = sqlconnect;
if (StoredProc == true)
{
sqlcmd.CommandType = CommandType.StoredProcedure;
if (Tablename.IndexOf("@@Mandantnr@@") > 0)
Tablename = Tablename.Replace("@@Mandantnr@@", "");
sqlcmd.CommandText = Tablename;
}
else
{
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = "Select * from " + Tablename;
}
if (is_SQL_String == true)
sqlcmd.CommandText = Tablename;
da.SelectCommand = sqlcmd;
da.Fill(dsdaten, "Daten");
sqlconnect.Close();
return dsdaten.Tables[0];
}
public void Get_Tabeldata_for_Update(string Tablename, bool StoredProc = false, bool is_SQL_String = false)
{
dsdaten.Clear();
dsdaten.Tables.Clear();
dadaten = new SqlDataAdapter(Tablename, this.connectionstring);
dadaten.Fill(dsdaten, Tablename);
}
public void Update_Tabeldata()
{
SqlCommandBuilder cb = new SqlCommandBuilder(dadaten);
dadaten.Update(dsdaten, dsdaten.Tables[0].TableName);
}
public string Get_TablePraefix(string apikey)
{
Get_Tabledata("select tablepraefix from patchargeapi where apikey='" + apikey + "'", false, true);
if (this.dsdaten.Tables[0].Rows.Count == 0)
{
return "";
}
else
{
return this.dsdaten.Tables[0].Rows[0][0].ToString();
}
}
public Dictionary<string, List<object>> DatatableToDictionary(DataTable dataTable)
{
var dict = new Dictionary<string, List<object>>();
foreach (DataColumn dataColumn in dataTable.Columns)
{
var columnValueList = new List<object>();
foreach (DataRow dataRow in dataTable.Rows)
{
columnValueList.Add(dataRow[dataColumn.ColumnName]);
}
dict.Add(dataColumn.ColumnName, columnValueList);
}
return dict;
}
#region "Converters"
public List<T> ConvertDataTable<T>(DataTable dt)
{
List<T> data = new List<T>();
foreach (DataRow row in dt.Rows)
{
T item = GetItem<T>(row);
data.Add(item);
}
return data;
}
private T GetItem<T>(DataRow dr)
{
Type temp = typeof(T);
T obj = Activator.CreateInstance<T>();
foreach (DataColumn column in dr.Table.Columns)
{
foreach (PropertyInfo pro in temp.GetProperties())
{
if (pro.Name == column.ColumnName)
pro.SetValue(obj, dr[column.ColumnName], null/* TODO Change to default(_) if this is not a reference type */);
else
continue;
}
}
return obj;
}
public IEnumerable<T> GetEntities<T>(DataTable dt)
{
if (dt == null)
{
return null;
}
List<T> returnValue = new List<T>();
List<string> typeProperties = new List<string>();
T typeInstance = Activator.CreateInstance<T>();
foreach (DataColumn column in dt.Columns)
{
var prop = typeInstance.GetType().GetProperty(column.ColumnName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
if (prop != null)
{
typeProperties.Add(column.ColumnName);
}
}
foreach (DataRow row in dt.Rows)
{
T entity = Activator.CreateInstance<T>();
foreach (var propertyName in typeProperties)
{
if (row[propertyName] != DBNull.Value)
{
string str = row[propertyName].GetType().FullName;
if (entity.GetType().GetProperty(propertyName).PropertyType == typeof(System.String))
{
object Val = row[propertyName].ToString();
entity.GetType().GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).SetValue(entity, Val, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public, null, null, null);
}
else if (entity.GetType().GetProperty(propertyName).PropertyType == typeof(System.Guid))
{
object Val = Guid.Parse(row[propertyName].ToString());
entity.GetType().GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).SetValue(entity, Val, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public, null, null, null);
}
else
{
entity.GetType().GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).SetValue(entity, row[propertyName], BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public, null, null, null);
}
}
else
{
entity.GetType().GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).SetValue(entity, null, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public, null, null, null);
}
}
returnValue.Add(entity);
}
return returnValue.AsEnumerable();
}
public string DataTableToJSONWithStringBuilder(DataTable table)
{
var JSONString = new StringBuilder();
if (table.Rows.Count > 0)
{
JSONString.Append("[");
for (int i = 0; i < table.Rows.Count; i++)
{
JSONString.Append("{");
for (int j = 0; j < table.Columns.Count; j++)
{
if (j < table.Columns.Count - 1)
{
JSONString.Append("\"" + table.Columns[j].ColumnName.ToString() + "\":" + "\"" + table.Rows[i][j].ToString() + "\",");
}
else if (j == table.Columns.Count - 1)
{
JSONString.Append("\"" + table.Columns[j].ColumnName.ToString() + "\":" + "\"" + table.Rows[i][j].ToString() + "\"");
}
}
if (i == table.Rows.Count - 1)
{
JSONString.Append("}");
}
else
{
JSONString.Append("},");
}
}
JSONString.Append("]");
}
return JSONString.ToString();
}
public string ConvertDataTableToString(DataTable table)
{
int iColumnCount = table.Columns.Count;
int iRowCount = table.Rows.Count;
int iTempRowCount = 0;
string strColumName = table.Columns[0].ColumnName;
string strOut = "{";
foreach (DataRow row in table.Rows)
{
strOut = strOut + "{";
foreach (DataColumn col in table.Columns)
{
string val = row.Field<string>(col.ColumnName);
strOut = strOut + col.ColumnName + ":" + val;
if (col.Ordinal != iColumnCount - 1)
{
strOut = strOut + ",";
}
}
strOut = strOut + "}";
iTempRowCount++;
if (iTempRowCount != iRowCount)
{
strOut = strOut + ",";
}
}
strOut = strOut + "}";
return strOut;
}
#endregion "Converters"
}
}