first significant successgit status Shakagit status

This commit is contained in:
2021-11-24 13:55:33 +01:00
parent 1bc9349f60
commit 66d222e0bf
3 changed files with 21 additions and 10 deletions

View File

@ -1,20 +1,21 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using MySqlConnector;
using de.hottis.genericdatabaseapiservice.Models;
namespace de.hottis.genericdatabaseapiservice.Services {
public interface IDbService {
void JustDoSomething<T>(string msg);
Task<List<T>> JustDoSomething<T>(string msg);
}
public class DbService : IDbService {
async public void JustDoSomething<T>(string msg) {
async public Task<List<T>> JustDoSomething<T>(string msg) {
Console.WriteLine(msg);
var item = Activator.CreateInstance<T>();
var itemList = new List<T>();
using (var conn = new MySqlConnection("Server=172.16.10.18;User ID=apiservicetestdb;Password=geheim123;Database=apiservicetestdb")) {
await conn.OpenAsync();
@ -27,17 +28,26 @@ namespace de.hottis.genericdatabaseapiservice.Services {
cmd.CommandText = "SELECT id, txt, nr FROM test1";
using (var reader = await cmd.ExecuteReaderAsync()) {
while (await reader.ReadAsync()) {
var item = Activator.CreateInstance<T>();
foreach (var propertyInfo in typeof(T).GetProperties()) {
Console.WriteLine("Property name: {0}", propertyInfo);
Console.WriteLine("Property name: {0} {1} ", propertyInfo.Name, propertyInfo.PropertyType);
var attributes = propertyInfo.GetCustomAttributes(typeof(DataMemberAttribute), true);
var dma = (DataMemberAttribute)attributes[0];
Console.WriteLine("DataMember name: {0} {1} ", dma.Name, dma.TypeId);
if (propertyInfo.PropertyType == typeof(System.String)) {
propertyInfo.SetValue(item, reader.GetString(dma.Name));
Console.WriteLine("Value:{0}", reader.GetString(dma.Name));
} else if (propertyInfo.PropertyType == typeof(System.Int32)) {
propertyInfo.SetValue(item, reader.GetInt32(dma.Name));
Console.WriteLine("Value:{0}", reader.GetInt32(dma.Name));
}
}
Console.WriteLine(reader.GetString("txt"));
itemList.Add(item);
}
}
}
}
return itemList;
}
}
}